2012年10月1日 星期一

c# interface example (complex)

Usage1:

IBankAccount venusAccount = new SaverAccount();
            ITransferBankAccount jupiterAccount = new CurrentAccount();
            venusAccount.PayIn(200);
            jupiterAccount.PayIn(500);
            jupiterAccount.TransferTo(venusAccount, 100);
            Console.WriteLine(venusAccount.ToString());
            Console.WriteLine(jupiterAccount.ToString());
Output
Payin balance 200
Payin balance 300
Venus Bank Saver: Balance = NT$300.00
Jupiter Bank Current Account: Balance = NT$400.00

Usage2:

            //balance 結餘的意思
            IBankAccount venusAccount = new SaverAccount();
            IBankAccount jupiterAccount = new GoldAccount();
            venusAccount.PayIn(200);//存入銀行
            venusAccount.Withdraw(100);//取出
            Console.WriteLine(venusAccount.ToString());
            jupiterAccount.PayIn(500);
            jupiterAccount.Withdraw(600);
            jupiterAccount.Withdraw(100);
            Console.WriteLine(((GoldAccount)jupiterAccount).TryIt());
Output
Payin balance 200
Withdraw 100
Venus Bank Saver: Balance = NT$100.00
Payin balance 500
Withdrawal attempt failed.
Withdraw 100
TryIt

Code:

public interface IBankAccount
        {
            void PayIn(decimal amount);
            bool Withdraw(decimal amount);
            decimal Balance
            {
                get;
            }
        }

        public class SaverAccount : IBankAccount
        {
            private decimal balance;
            public void PayIn(decimal amount)
            {
                balance += amount;
                Console.WriteLine("Payin balance " + balance);
            }
            public bool Withdraw(decimal amount)
            {
                if (balance >= amount)
                {
                    balance -= amount;
                    Console.WriteLine("Withdraw " + amount);
                    return true;
                }
                Console.WriteLine("Withdrawal attempt failed.");
                return false;
            }
            public decimal Balance
            {
                get
                {
                    return balance;
                }
            }
            public override string ToString()
            {
                return String.Format("Venus Bank Saver: Balance = {0,6:C}", balance);
            }
        }

        public class GoldAccount : IBankAccount
        {
            private decimal balance;
            public void PayIn(decimal amount)
            {
                balance += amount;
                Console.WriteLine("Payin balance " + balance);
            }
            public bool Withdraw(decimal amount)
            {
                if (balance >= amount)
                {
                    balance -= amount;
                    Console.WriteLine("Withdraw " + amount);
                    return true;
                }
                Console.WriteLine("Withdrawal attempt failed.");
                return false;
            }
            public decimal Balance
            {
                get
                {
                    return balance;
                }
            }
            public string TryIt()
            {
                return "TryIt";
            }
        }

        public interface ITransferBankAccount : IBankAccount
        {
            bool TransferTo(IBankAccount destination, decimal amount);
        }

        public class CurrentAccount : ITransferBankAccount
        {
            private decimal balance;
            public void PayIn(decimal amount)
            {
                balance += amount;
            }
            public bool Withdraw(decimal amount)
            {
                if (balance >= amount)
                {
                    balance -= amount;
                    return true;
                }
                Console.WriteLine("Withdrawal attempt failed.");
                return false;
            }
            public decimal Balance
            {
                get
                {
                    return balance;
                }
            }
            public bool TransferTo(IBankAccount destination, decimal amount)
            {
                bool result;
                if ((result = Withdraw(amount)) == true)
                    destination.PayIn(amount);
                return result;
            }
            public override string ToString()
            {
                return String.Format("Jupiter Bank Current Account: Balance = {0,6:C}",
                balance);
            }
        }

沒有留言: