• CurrentAccounts.cs


    using System;
    using Wrox.ProCSharp;
    using Wrox.ProCSharp.VenusBank;
    using Wrox.ProCSharp.JupiterBank;

    namespace Wrox.ProCSharp
    {
       class MainEntryPoint
       {
          static void Main()
          {
            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());
          }
       }
    }


    namespace Wrox.ProCSharp
    {
       public interface IBankAccount
       {
          void PayIn(decimal amount);
          bool Withdraw(decimal amount);
          decimal Balance
          {
             get;
          }
       }
        
        
         public interface ITransferBankAccount : IBankAccount
       {
          bool TransferTo(IBankAccount destination, decimal amount);
       }
    }

    namespace Wrox.ProCSharp.VenusBank
    {
       public class SaverAccount : IBankAccount
       {
          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 override string ToString()
          {
             return String.Format("Venus Bank Saver: Balance = {0,6:C}", balance);
          }
       }
    }



    namespace Wrox.ProCSharp.JupiterBank
    {
       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);
       }   
     
    }


    }
  • 相关阅读:
    【转载】Android应用框架及常用工具类总结
    【Android】Android开发规范的一点小体会
    【Android】解决Android的ListView控件滚动时背景变黑
    【Android】侧边栏SlidingMenu
    【Android】解决RadioButton+FragmentPagerAdapter+Fragment切换页面数据加载的问题
    【Android】ListView动态视图显示不全
    Android编码规范
    【Android】两个日期相差几天和两个日期比较大小
    Linux基础之快照克隆、Xshell优化、Linux历史
    Android APP打包时,出错:"XXX" is not translated in "af" (Afrikaans), "am" (Amharic)
  • 原文地址:https://www.cnblogs.com/shihao/p/2506354.html
Copyright © 2020-2023  润新知