MortimerPhones4.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:3k
源码类别:

C#编程

开发平台:

Others

  1. namespace Wrox.ProCSharp.OOProg
  2. {
  3.    using System;
  4.    public enum TypeOfCall 
  5.    {
  6.       CallToCellPhone, CallToLandline
  7.    } 
  8.    public abstract class GenericCustomer
  9.    {
  10.       private string name;
  11.       protected decimal balance;
  12.       public string Name
  13.       {
  14.          get
  15.          {
  16.             return name;
  17.          }
  18.          set
  19.          {
  20.             name = value;
  21.          }
  22.       }
  23.       public decimal Balance
  24.       {
  25.          get
  26.          {
  27.             return balance;
  28.          }
  29.       }
  30.       public void RecordPayment(decimal amountPaid)
  31.       {
  32.          balance -= amountPaid;
  33.       }
  34.       public abstract void RecordCall(TypeOfCall callType, uint nMinutes);
  35.    }
  36.    public class Nevermore60Customer : GenericCustomer
  37.    {
  38.       private uint highCostMinutesUsed;
  39.       public override void RecordCall(TypeOfCall callType, uint nMinutes)
  40.       {
  41.          switch (callType)
  42.          {
  43.             case TypeOfCall.CallToLandline:
  44.                balance += (0.02M * nMinutes);
  45.                break;
  46.             case TypeOfCall.CallToCellPhone:
  47.          uint highCostMinutes, lowCostMinutes;
  48.          uint highCostMinutesToGo = 
  49.             (highCostMinutesUsed < 60) ? 60 - highCostMinutesUsed : 0;
  50.          if (nMinutes > highCostMinutesToGo)
  51.          {
  52.             highCostMinutes = highCostMinutesToGo;
  53.             lowCostMinutes = nMinutes - highCostMinutes;
  54.          }
  55.          else
  56.          {
  57.             highCostMinutes = nMinutes;
  58.             lowCostMinutes = 0;
  59.          }
  60.          highCostMinutesUsed += highCostMinutes;
  61.          balance += (0.50M * highCostMinutes + 0.20M * 
  62.             lowCostMinutes);
  63.          break;
  64.             default:
  65.                break;
  66.          }
  67.       }
  68.    }
  69.    public class PayAsYouGoCustomer : GenericCustomer
  70.    {
  71.       public override void RecordCall(TypeOfCall callType, uint nMinutes)
  72.       {
  73.          switch (callType)
  74.          {
  75.             case TypeOfCall.CallToLandline:
  76.                balance += (0.02M * nMinutes);
  77.                break;
  78.             case TypeOfCall.CallToCellPhone:
  79.                balance += (0.30M * nMinutes);
  80.                break;
  81.             default:
  82.                break;
  83.          }
  84.       }
  85.    }
  86.    public class MainEntryPoint
  87.    {
  88.       public static void Main()
  89.       {
  90.          GenericCustomer arabel = new Nevermore60Customer();
  91.          arabel.Name = "Arabel Jones";
  92.          GenericCustomer mrJones = new PayAsYouGoCustomer();
  93.          mrJones.Name = "Ben Jones";                
  94.          GenericCustomer [] customers = new GenericCustomer[2];
  95.          customers[0] = arabel;
  96.          customers[0].RecordCall(TypeOfCall.CallToLandline, 20);
  97.          customers[0].RecordCall(TypeOfCall.CallToCellPhone, 5);
  98.          customers[1] = mrJones;
  99.          customers[1].RecordCall(TypeOfCall.CallToLandline, 10);
  100.          foreach (GenericCustomer nextCustomer in customers)
  101.          {
  102.             Console.WriteLine("{0,-20} owes ${1:F2}", nextCustomer.Name, nextCustomer.Balance);
  103.          }
  104.       }
  105.    }
  106. }