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