• BankAccounts.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();
             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(jupiterAccount.ToString());
          }
       }
    }


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

    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 GoldAccount : 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("Jupiter Bank Saver: Balance = {0,6:C}", balance);
          }
            }
     
    }
  • 相关阅读:
    hdu 1174
    计算几何模板
    又是一年博客记
    hdu 1225 Football Score
    与逆序数有关的
    hdu 2844 Coins
    hdu 1171 Big Event in HDU
    UVA Exponentiation
    UVA Summation of Four Primes
    Linux:设置alias永久生效
  • 原文地址:https://www.cnblogs.com/shihao/p/2490247.html
Copyright © 2020-2023  润新知