temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // See README.txt for information and build instructions. |
| 2 | |
Chris Rebert | 9d17549 | 2021-03-31 17:13:01 -0700 | [diff] [blame] | 3 | import com.example.tutorial.protos.AddressBook; |
| 4 | import com.example.tutorial.protos.Person; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 5 | import java.io.BufferedReader; |
| 6 | import java.io.FileInputStream; |
| 7 | import java.io.FileNotFoundException; |
| 8 | import java.io.FileOutputStream; |
| 9 | import java.io.InputStreamReader; |
| 10 | import java.io.IOException; |
| 11 | import java.io.PrintStream; |
| 12 | |
| 13 | class AddPerson { |
| 14 | // This function fills in a Person message based on user input. |
| 15 | static Person PromptForAddress(BufferedReader stdin, |
| 16 | PrintStream stdout) throws IOException { |
| 17 | Person.Builder person = Person.newBuilder(); |
| 18 | |
| 19 | stdout.print("Enter person ID: "); |
| 20 | person.setId(Integer.valueOf(stdin.readLine())); |
| 21 | |
| 22 | stdout.print("Enter name: "); |
| 23 | person.setName(stdin.readLine()); |
| 24 | |
| 25 | stdout.print("Enter email address (blank for none): "); |
| 26 | String email = stdin.readLine(); |
| 27 | if (email.length() > 0) { |
| 28 | person.setEmail(email); |
| 29 | } |
| 30 | |
| 31 | while (true) { |
| 32 | stdout.print("Enter a phone number (or leave blank to finish): "); |
| 33 | String number = stdin.readLine(); |
| 34 | if (number.length() == 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | Person.PhoneNumber.Builder phoneNumber = |
| 39 | Person.PhoneNumber.newBuilder().setNumber(number); |
| 40 | |
| 41 | stdout.print("Is this a mobile, home, or work phone? "); |
| 42 | String type = stdin.readLine(); |
| 43 | if (type.equals("mobile")) { |
| 44 | phoneNumber.setType(Person.PhoneType.MOBILE); |
| 45 | } else if (type.equals("home")) { |
| 46 | phoneNumber.setType(Person.PhoneType.HOME); |
| 47 | } else if (type.equals("work")) { |
| 48 | phoneNumber.setType(Person.PhoneType.WORK); |
| 49 | } else { |
| 50 | stdout.println("Unknown phone type. Using default."); |
| 51 | } |
| 52 | |
Jan Tattermusch | 4d86c2b | 2015-07-20 14:51:09 -0700 | [diff] [blame] | 53 | person.addPhones(phoneNumber); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return person.build(); |
| 57 | } |
| 58 | |
| 59 | // Main function: Reads the entire address book from a file, |
| 60 | // adds one person based on user input, then writes it back out to the same |
| 61 | // file. |
| 62 | public static void main(String[] args) throws Exception { |
| 63 | if (args.length != 1) { |
| 64 | System.err.println("Usage: AddPerson ADDRESS_BOOK_FILE"); |
| 65 | System.exit(-1); |
| 66 | } |
| 67 | |
| 68 | AddressBook.Builder addressBook = AddressBook.newBuilder(); |
| 69 | |
| 70 | // Read the existing address book. |
| 71 | try { |
| 72 | FileInputStream input = new FileInputStream(args[0]); |
liujisi@google.com | 3239fec | 2011-06-07 03:51:33 +0000 | [diff] [blame] | 73 | try { |
| 74 | addressBook.mergeFrom(input); |
| 75 | } finally { |
| 76 | try { input.close(); } catch (Throwable ignore) {} |
| 77 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 78 | } catch (FileNotFoundException e) { |
| 79 | System.out.println(args[0] + ": File not found. Creating a new file."); |
| 80 | } |
| 81 | |
| 82 | // Add an address. |
Jan Tattermusch | b0e5ba6 | 2015-07-20 15:24:08 -0700 | [diff] [blame] | 83 | addressBook.addPeople( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 84 | PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), |
| 85 | System.out)); |
| 86 | |
| 87 | // Write the new address book back to disk. |
| 88 | FileOutputStream output = new FileOutputStream(args[0]); |
liujisi@google.com | 3239fec | 2011-06-07 03:51:33 +0000 | [diff] [blame] | 89 | try { |
| 90 | addressBook.build().writeTo(output); |
| 91 | } finally { |
| 92 | output.close(); |
| 93 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 94 | } |
| 95 | } |