C# Program to Demonstrate Transactions using Interface

This is a C# Program to demonstrate transactions using interface.

Problem Description

This C# Program Demonstrates Transactions using Interface.

Problem Solution

Here interfaces define properties, methods and events, which are the members of the interface and contains only the declaration of the members and has the responsibility of the deriving class to define the members.

Program/Source Code

Here is source code of the C# Program to Demonstrate Transactions using Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Demonstrate Transactions using Interface
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace sample
{
 
    public interface ITransactions
    {
        void showTransaction();
        double getamnt();
    }
    public class Transaction : ITransactions
    {
        private string Code;
        private string date;
        private double amnt;
        public Transaction()
        {
            Code = " ";
            date = " ";
            amnt = 0.0;
        }
        public Transaction(string c, string d, double a)
        {
            Code = c;
            date = d;
            amnt = a;
        }
        public double getamnt()
        {
            return amnt;
        }
        public void showTransaction()
        {
            Console.WriteLine("Transaction: {0}", Code);
            Console.WriteLine("Date: {0}", date);
            Console.WriteLine("amnt: {0}", getamnt());
 
        }
 
    }
    class example
    {
        static void Main(string[] args)
        {
            Transaction t1 = new Transaction("001", "24/06/2014", 87900.00);
            Transaction t2 = new Transaction("002", "25/06/2014", 51900.00);
            t1.showTransaction();
            t2.showTransaction();
            Console.ReadKey();
        }
    }
}
Program Explanation

This C# we are creating two object variables t1 and t2 for Transaction class. The showTransaction() and getamt() functions are defined as ITransactions Interface.

advertisement
advertisement

Hence interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members and it is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.

Runtime Test Cases
 
Transaction: 001
Date: 24/06/2014
amnt: 87900
Transaction: 002
Date: 25/06/2014
amnt: 51900

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.