• 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元

  • 相关阅读:
    第二次结对编程作业
    第5组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    51 Nod 1024 Set
    51 Nod 1007 dp
    YY的GCD 数学
    选课 树形背包dp
    运输问题 费用流
    分配问题 费用流
  • 原文地址:https://www.cnblogs.com/_ymw/p/2846588.html
Copyright © 2020-2023  润新知