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

C#编程

开发平台:

Others

  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. namespace Wrox.ProCSharp.SocialSecurityNumbers
  5. {
  6.    class MainEntryPoint
  7.    {
  8.       static void Main()
  9.       {
  10.          TestHarness harness = new TestHarness();
  11.          harness.Run();
  12.       }
  13.    }
  14.    class TestHarness
  15.    {
  16.       Hashtable employees = new Hashtable(31);
  17.       public void Run()
  18.       {
  19.          EmployeeID idMortimer = new EmployeeID("B001");
  20.          EmployeeData mortimer = new EmployeeData(idMortimer, "Mortimer", 100000.00M);
  21.          EmployeeID idArabel = new EmployeeID("W234");
  22.          EmployeeData arabel= new EmployeeData(idArabel, "Arabel Jones", 10000.00M);
  23.          employees.Add(idMortimer, mortimer);
  24.          employees.Add(idArabel, arabel);
  25.          while (true)
  26.          {
  27.             try
  28.             {
  29.                Console.Write("Enter employee ID (format:A999, X to exit)> ");
  30.                string userInput = Console.ReadLine();
  31.                userInput = userInput.ToUpper();
  32.                if (userInput == "X")
  33.                   return;
  34.                EmployeeID id = new EmployeeID(userInput);
  35.                DisplayData(id);
  36.             }
  37.             catch (Exception e)
  38.             {
  39.                Console.WriteLine("Exception occurred. Did you use the correct format for the employee ID?");
  40.                Console.WriteLine(e.Message);
  41.                Console.WriteLine();
  42.             }
  43.             Console.WriteLine();
  44.          }
  45.       }
  46.       private void DisplayData(EmployeeID id)
  47.       {
  48.          object empobj = employees[id];
  49.          if (empobj != null)
  50.          {
  51.             EmployeeData employee = (EmployeeData)empobj;
  52.             Console.WriteLine("Employee: " + employee.ToString());
  53.          }
  54.          else
  55.             Console.WriteLine("Employee not found: ID = " + id);
  56.       }
  57.    }
  58.    class EmployeeData
  59.    {
  60.       private string name;
  61.       private decimal salary;
  62.       private EmployeeID id;
  63.       public EmployeeData(EmployeeID id, string name, decimal salary)
  64.       {
  65.          this.id = id;
  66.          this.name = name;
  67.          this.salary = salary;
  68.       }
  69.       public override string ToString()
  70.       {
  71.          StringBuilder sb = new StringBuilder(id.ToString(), 100);
  72.          sb.Append(": ");
  73.          sb.Append(string.Format("{0,-20}", name));
  74.          sb.Append(" ");
  75.          sb.Append(string.Format("{0:C}", salary));
  76.          return sb.ToString();
  77.       }
  78.    }
  79.    class EmployeeID
  80.    {
  81.       private readonly char prefix;
  82.       private readonly int number;
  83.       public EmployeeID(string id)
  84.       {
  85.          prefix = (id.ToUpper())[0];
  86.          number = int.Parse(id.Substring(1,3));
  87.       }
  88.       public override string ToString()
  89.       {
  90.          return prefix.ToString() + string.Format("{0,3:000}", number);
  91.       }
  92.       public override int GetHashCode()
  93.       {
  94.          string str = this.ToString();
  95.          return str.GetHashCode();
  96.       }
  97.       public override bool Equals(object obj)
  98.       {
  99.          EmployeeID rhs = obj as EmployeeID;
  100.          if (rhs == null)
  101.             return false;
  102.          if (prefix == rhs.prefix && number == rhs.number)
  103.             return true;
  104.          return false;
  105.       }
  106.    }
  107. }