ListPeople.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // See README.txt for information and build instructions.
  2. import com.example.tutorial.protos.AddressBook;
  3. import com.example.tutorial.protos.Person;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.PrintStream;
  7. class ListPeople {
  8. // Iterates though all people in the AddressBook and prints info about them.
  9. static void Print(AddressBook addressBook) {
  10. for (Person person: addressBook.getPeopleList()) {
  11. System.out.println("Person ID: " + person.getId());
  12. System.out.println(" Name: " + person.getName());
  13. if (!person.getEmail().isEmpty()) {
  14. System.out.println(" E-mail address: " + person.getEmail());
  15. }
  16. for (Person.PhoneNumber phoneNumber : person.getPhonesList()) {
  17. switch (phoneNumber.getType()) {
  18. case MOBILE:
  19. System.out.print(" Mobile phone #: ");
  20. break;
  21. case HOME:
  22. System.out.print(" Home phone #: ");
  23. break;
  24. case WORK:
  25. System.out.print(" Work phone #: ");
  26. break;
  27. default:
  28. System.out.println(" Unknown phone #: ");
  29. break;
  30. }
  31. System.out.println(phoneNumber.getNumber());
  32. }
  33. }
  34. }
  35. // Main function: Reads the entire address book from a file and prints all
  36. // the information inside.
  37. public static void main(String[] args) throws Exception {
  38. if (args.length != 1) {
  39. System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE");
  40. System.exit(-1);
  41. }
  42. // Read the existing address book.
  43. AddressBook addressBook =
  44. AddressBook.parseFrom(new FileInputStream(args[0]));
  45. Print(addressBook);
  46. }
  47. }