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