ListPeople.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // See CSHARP-README.txt for information and build instructions.
  2. //
  3. using System;
  4. using System.IO;
  5. using Tutorial;
  6. class ListPeople {
  7. // Iterates though all people in the AddressBook and prints info about them.
  8. static void Print(AddressBook addressBook) {
  9. foreach (Person person in addressBook.PersonList) {
  10. Console.WriteLine("Person ID: {0}", person.Id);
  11. Console.WriteLine(" Name: {0}", person.Name);
  12. if (person.HasEmail) {
  13. Console.WriteLine(" E-mail address: {0}", person.Email);
  14. }
  15. foreach (Person.Types.PhoneNumber phoneNumber in person.PhoneList) {
  16. switch (phoneNumber.Type) {
  17. case Person.Types.PhoneType.MOBILE:
  18. Console.Write(" Mobile phone #: ");
  19. break;
  20. case Person.Types.PhoneType.HOME:
  21. Console.Write(" Home phone #: ");
  22. break;
  23. case Person.Types.PhoneType.WORK:
  24. Console.Write(" Work phone #: ");
  25. break;
  26. }
  27. Console.WriteLine(phoneNumber.Number);
  28. }
  29. }
  30. }
  31. // Main function: Reads the entire address book from a file and prints all
  32. // the information inside.
  33. public static int Main(string[] args) {
  34. if (args.Length != 1) {
  35. Console.Error.WriteLine("Usage: ListPeople ADDRESS_BOOK_FILE");
  36. return -1;
  37. }
  38. // Read the existing address book.
  39. using (Stream stream = File.OpenRead(args[0])) {
  40. AddressBook addressBook = AddressBook.ParseFrom(stream);
  41. Print(addressBook);
  42. }
  43. return 0;
  44. }
  45. }