AddPerson.java 2.9 KB

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