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