C#代碼:用接口模擬實現存取款及銀行之間的轉賬


1.首先定義一個接口:聲明存取金額的方法

        /// <summary>
        /// 接口
        /// </summary>
        public interface IBankAccount
        {
            /// <summary>
            /// 存入金額
            /// </summary>
            /// <param name="amount"></param>
            void PayIn(decimal amount);
            /// <summary>
            /// 取出金額
            /// </summary>
            /// <param name="amount"></param>
            /// <returns></returns>
            bool WithDraw(decimal amount);
            /// <summary>
            /// 交易金額
            /// </summary>
            decimal Balance
            {
                get;
            }
        }

 

2.存取款SaverAccount實現接口:

        /// <summary>
        /// 類實現接口
        /// </summary>
        public class SaverAccount : IBankAccount
        {
            private decimal balance;
            /// <summary>
            /// 存款
            /// </summary>
            /// <param name="amount"></param>
            public void PayIn(decimal amount)
            {
                balance += amount;
            }

            /// <summary>
            /// 取款
            /// </summary>
            /// <param name="amount"></param>
            /// <returns></returns>
            public bool WithDraw(decimal amount)
            {
                if (balance > amount)
                {
                    balance -= amount;
                    return true;
                }
                MessageBox.Show("取款金額大於賬戶余額");
                return false;
            }

            /// <summary>
            /// 賬戶當前金額
            /// </summary>
            public decimal Balance
            {
                get
                {
                    return balance;
                }
            }

            /// <summary>
            /// 格式化輸出結果
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return string.Format("Venus Bank Saver:Balance={0,6:C}", balance);
            }
        }

 

3.定義一個轉賬的接口:

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

4.實現轉賬接口轉賬的類:

 public class CurrentAccoount : ITransferBankAccount
        {
            public void PayIn(decimal amount)
            {
                balance += amount;
            }

            public bool WithDraw(decimal amount)
            {
                if (balance > amount)
                {
                    balance -= amount;
                    return true;
                }
                MessageBox.Show("取款金額大於賬戶余額");
                return false;
            }

            private decimal balance;
            /// <summary>
            /// 當前余額
            /// </summary>
            public decimal Balance
            {
                get
                {
                    return balance;
                }
            }

            public override string ToString()
            {
                return string.Format("YMW Bank Saver:Balance={0,6:C}", balance);
            }

            /// <summary>
            /// 轉賬 從當前賬戶轉入指定賬戶
            /// </summary>
            /// <param name="description">轉入指定帳戶</param>
            /// <param name="amount">轉入的金額</param>
            /// <returns>bool</returns>
            public bool TransferTo(IBankAccount description, decimal amount)
            {
                bool result = true;
                if ((result == WithDraw(amount)) == true)
                {
                    description.PayIn(amount);
                }
                return false;
            }
        }

5.測試存取款:如下

     IBankAccount iba = new SaverAccount();
     iba.PayIn(200);                                    //存入200元
     iba.WithDraw(100);                              //取出100元  
     MessageBox.Show(iba.ToString());        //賬戶余額100元
     MessageBox.Show("賬戶余額:" + iba.Balance.ToString());

6.測試轉賬:如下

            IBankAccount IBA = new SaverAccount();
            ITransferBankAccount ITB = new CurrentAccoount();
            IBA.PayIn(200);  //A賬戶存入200元
            ITB.PayIn(500);  //B賬戶存入500元
            ITB.TransferTo(IBA, 400);  //B賬戶向A賬戶轉入400元
            MessageBox.Show(IBA.ToString());  //A賬戶余額600元
            MessageBox.Show(ITB.ToString());  //B賬戶余額100元


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM