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

C#编程

开发平台:

Others

  1. using System;
  2. using System.IO;
  3. namespace Wrox.ProCSharp.AdvancedCSharp
  4. {
  5.    class MainEntryPoint
  6.    {
  7.       static void Main()
  8.       {
  9.          string fileName;
  10.          Console.Write("Please type in the name of the file " +
  11.             "containing the names of the people to be cold-called > ");
  12.          fileName = Console.ReadLine();
  13.          ColdCallFileReader peopleToRing = new ColdCallFileReader();
  14.          try
  15.          {
  16.             peopleToRing.Open(fileName);
  17.             for (int i=0 ; i<peopleToRing.NPeopleToRing; i++)
  18.             {
  19.                peopleToRing.ProcessNextPerson();
  20.             }
  21.             Console.WriteLine("All callees processed correctly");
  22.          }
  23.          catch(FileNotFoundException e)
  24.          {
  25.             Console.WriteLine("The file {0} does not exist", fileName);
  26.          }
  27.          catch(ColdCallFileFormatException e)
  28.          {
  29.             Console.WriteLine(
  30.           "The file {0} appears to have been corrupted", fileName);
  31.             Console.WriteLine("Details of problem are: {0}", e.Message);
  32.             if (e.InnerException != null)
  33.                Console.WriteLine(
  34.                   "Inner exception was: {0}", e.InnerException.Message);
  35.          }
  36.          catch(Exception e)
  37.          {
  38.             Console.WriteLine("Exception occurred:n" + e.Message);
  39.          }         
  40.          finally
  41.          {
  42.             peopleToRing.Dispose();
  43.          }
  44.          Console.ReadLine();
  45.       }
  46.    }
  47.    class ColdCallFileReader :IDisposable
  48.    {
  49.       FileStream fs;
  50.       StreamReader sr;
  51.       uint nPeopleToRing;
  52.       bool isDisposed = false;
  53.       bool isOpen = false;
  54.       public void Open(string fileName)
  55.       {
  56.          if (isDisposed)
  57.             throw new ObjectDisposedException("peopleToRing");
  58.          fs = new FileStream(fileName, FileMode.Open);
  59.          sr = new StreamReader(fs);
  60.          try 
  61.          {
  62.             string firstLine = sr.ReadLine();
  63.             nPeopleToRing = uint.Parse(firstLine);
  64.             isOpen = true;
  65.          }
  66.          catch (FormatException e)
  67.          {
  68.             throw new ColdCallFileFormatException(
  69.                "First line isn't an integer", e);
  70.          }
  71.       }
  72.       public uint NPeopleToRing
  73.       {
  74.          get
  75.          {
  76.             if (isDisposed)
  77.                throw new ObjectDisposedException("peopleToRing");
  78.             if (!isOpen)
  79.                throw new UnexpectedException(
  80.                   "Attempt to access cold call file that is not open");
  81.             return nPeopleToRing;
  82.          }
  83.       }
  84.       public void Dispose()
  85.       {
  86.          if (isDisposed)
  87.             return;
  88.          isDisposed = true;
  89.          isOpen = false;
  90.          if (fs != null)
  91.          {
  92.             fs.Close();
  93.             fs = null;
  94.          }
  95.       }
  96.       public void ProcessNextPerson()
  97.       {
  98.          if (isDisposed)
  99.             throw new ObjectDisposedException("peopleToRing");
  100.          if (!isOpen)
  101.             throw new UnexpectedException(
  102.                "Attempt to access cold call file that is not open");
  103.          try
  104.          {
  105.             string name;
  106.             name = sr.ReadLine();
  107.             if (name == null)
  108.                throw new ColdCallFileFormatException("Not enough names");
  109.             if (name[0] == 'Z')
  110.             {
  111.                throw new LandLineSpyFoundException(name);
  112.             }
  113.             Console.WriteLine(name);
  114.          }
  115.          catch(LandLineSpyFoundException e)
  116.          {
  117.             Console.WriteLine(e.Message);
  118.          }
  119.          finally
  120.          {
  121.          }
  122.       }
  123.    }
  124.    class LandLineSpyFoundException : ApplicationException
  125.    {
  126.       public LandLineSpyFoundException(string spyName)
  127.          :   base("LandLine spy found, with name " + spyName)
  128.       {
  129.       }
  130.       public LandLineSpyFoundException(string spyName, Exception innerException)
  131.          :   base("LandLine spy found, with name " + spyName, innerException)
  132.       {
  133.       }
  134.    }
  135.    class ColdCallFileFormatException : ApplicationException
  136.    {
  137.       public ColdCallFileFormatException(string message)
  138.          :   base(message)
  139.       {
  140.       }
  141.       public ColdCallFileFormatException(string message, Exception innerException)
  142.          :   base(message, innerException)
  143.       {
  144.       }
  145.    }
  146.    class UnexpectedException : ApplicationException
  147.    {
  148.       public UnexpectedException(string message)
  149.          :   base(message)
  150.       {
  151.       }
  152.       public UnexpectedException(string message, Exception innerException)
  153.          :   base(message, innerException)
  154.       {
  155.       }
  156.    }
  157. }