MortimerPhones2.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 class Customer
  9.    {
  10.       private string name;
  11.       protected decimal balance;
  12.       public string GetFunnyString()
  13.       {
  14.          return "Plain ordinary customer. Kaark!";
  15.       }
  16.       public string Name
  17.       {
  18.          get
  19.          {
  20.             return name;
  21.          }
  22.          set
  23.          {
  24.             name = value;
  25.          }
  26.       }
  27.       public decimal Balance
  28.       {
  29.          get
  30.          {
  31.             return balance;
  32.          }
  33.       }
  34.       public void RecordPayment(decimal amountPaid)
  35.       {
  36.          balance -= amountPaid;
  37.       }
  38.       public virtual void RecordCall(TypeOfCall callType, uint nMinutes)
  39.       {
  40.          switch (callType)
  41.          {
  42.             case TypeOfCall.CallToLandline:
  43.                balance += (0.02M * nMinutes);
  44.                break;
  45.             case TypeOfCall.CallToCellPhone:
  46.                balance += (0.30M * nMinutes);
  47.                break;
  48.             default:
  49.                break;
  50.          }
  51.       }
  52.    }
  53.    public class Nevermore60Customer : Customer
  54.    {
  55.       private uint highCostMinutesUsed;
  56.       public new string GetFunnyString()
  57.       {
  58.          return "Nevermore60. Nevermore!";
  59.       }
  60.       public override void RecordCall(TypeOfCall callType, uint nMinutes)
  61.       {
  62.          switch (callType)
  63.          {
  64.             case TypeOfCall.CallToLandline:
  65.                balance += (0.02M * nMinutes);
  66.                break;
  67.             case TypeOfCall.CallToCellPhone:
  68.          uint highCostMinutes, lowCostMinutes;
  69.          uint highCostMinutesToGo = 
  70.             (highCostMinutesUsed < 60) ? 60 - highCostMinutesUsed : 0;
  71.          if (nMinutes > highCostMinutesToGo)
  72.          {
  73.             highCostMinutes = highCostMinutesToGo;
  74.             lowCostMinutes = nMinutes - highCostMinutes;
  75.          }
  76.          else
  77.          {
  78.             highCostMinutes = nMinutes;
  79.             lowCostMinutes = 0;
  80.          }
  81.          highCostMinutesUsed += highCostMinutes;
  82.          balance += (0.50M * highCostMinutes + 0.20M * 
  83.             lowCostMinutes);
  84.          break;
  85.             default:
  86.                break;
  87.          }
  88.       }
  89.    }
  90.    public class MainEntryPoint
  91.    {
  92.       public static void Main()
  93.       {
  94.          Customer cust1;
  95.          Nevermore60Customer cust2;
  96.          cust1 = new Customer();
  97.          Console.WriteLine("Customer referencing Customer: " 
  98.             + cust1.GetFunnyString());
  99.          cust1 = new Nevermore60Customer();
  100.          Console.WriteLine("Customer referencing Nevermore60Customer: " 
  101.             + cust1.GetFunnyString());
  102.          cust2 = new Nevermore60Customer();
  103.          Console.WriteLine("Nevermore60Customer referencing: " 
  104.             + cust2.GetFunnyString());   
  105.       }
  106.    }
  107. }