|
2 | 2 |
|
3 | 3 | import java.util.Scanner; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Created by mausv on 7/27/2016. |
| 7 | + */ |
5 | 8 | public class Main { |
6 | | - |
7 | | - public static Scanner scanner = new Scanner(System.in); |
8 | | - |
9 | | - public static MobilePhone myPhone = new MobilePhone(); |
| 9 | + private static Scanner scanner = new Scanner(System.in); |
| 10 | + private static MobilePhone mobilePhone = new MobilePhone("0039 330 4404"); |
10 | 11 |
|
11 | 12 | public static void main(String[] args) { |
12 | | - |
13 | | - initializePhone(); |
14 | | - |
15 | | - } |
16 | | - |
17 | | - public static void initializePhone() { |
18 | | - printMenu(); |
19 | | - System.out.println("Choose an option: "); |
20 | | - |
21 | | - boolean flag = true; |
22 | | - |
23 | | - while(flag) { |
24 | | - int choice = scanner.nextInt(); |
| 13 | + boolean quit = false; |
| 14 | + startPhone(); |
| 15 | + printActions(); |
| 16 | + while(!quit) { |
| 17 | + System.out.println("\nEnter action: (6 to show available actions)"); |
| 18 | + int action = scanner.nextInt(); |
25 | 19 | scanner.nextLine(); |
26 | | - switch (choice) { |
| 20 | + |
| 21 | + switch (action) { |
27 | 22 | case 0: |
28 | | - printMenu(); |
| 23 | + System.out.println("\nShutting down..."); |
| 24 | + quit = true; |
29 | 25 | break; |
30 | 26 |
|
31 | 27 | case 1: |
32 | | - System.out.println("Enter name, press enter and then the phone number: "); |
33 | | - myPhone.addContact(scanner.nextLine(), scanner.nextLine()); |
| 28 | + mobilePhone.printContacts(); |
34 | 29 | break; |
35 | 30 |
|
36 | 31 | case 2: |
37 | | - System.out.println("Contacts: "); |
38 | | - myPhone.getContactList(); |
| 32 | + addNewContact(); |
39 | 33 | break; |
40 | 34 |
|
41 | 35 | case 3: |
42 | | - System.out.println("Enter contact name: "); |
43 | | - myPhone.updateContact(scanner.nextLine()); |
| 36 | + updateContact(); |
44 | 37 | break; |
45 | 38 |
|
46 | 39 | case 4: |
47 | | - System.out.println("Remove contact: "); |
48 | | - myPhone.removeContact(scanner.nextLine()); |
| 40 | + removeContact(); |
49 | 41 | break; |
50 | 42 |
|
51 | 43 | case 5: |
52 | | - System.out.println("Contact name: "); |
53 | | - myPhone.findContact(scanner.nextLine()); |
| 44 | + queryContact(); |
54 | 45 | break; |
55 | 46 |
|
56 | | - case 9: |
57 | | - System.out.println("Exiting phone.."); |
58 | | - flag = false; |
| 47 | + case 6: |
| 48 | + printActions(); |
59 | 49 | break; |
| 50 | + } |
60 | 51 |
|
61 | | - default: |
62 | | - System.out.println("Wrong option"); |
| 52 | + } |
63 | 53 |
|
64 | | - } |
| 54 | + } |
| 55 | + |
| 56 | + private static void addNewContact() { |
| 57 | + System.out.println("Enter new contact name: "); |
| 58 | + String name = scanner.nextLine(); |
| 59 | + System.out.println("Enter phone number: "); |
| 60 | + String phone = scanner.nextLine(); |
| 61 | + Contact newContact = Contact.createContact(name, phone); |
| 62 | + if(mobilePhone.addNewContact(newContact)) { |
| 63 | + System.out.println("New contact added: name = " + name + ", phone = "+ phone); |
| 64 | + } else { |
| 65 | + System.out.println("Cannot add, " + name + " already on file"); |
65 | 66 | } |
| 67 | + } |
| 68 | + |
| 69 | + private static void updateContact() { |
| 70 | + System.out.println("Enter existing contact name: "); |
| 71 | + String name = scanner.nextLine(); |
| 72 | + Contact existingContactRecord = mobilePhone.queryContact(name); |
| 73 | + if(existingContactRecord == null) { |
| 74 | + System.out.println("Contact not found."); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + System.out.print("Enter new contact name: "); |
| 79 | + String newName = scanner.nextLine(); |
| 80 | + System.out.print("Enter new contact phone number: "); |
| 81 | + String newNumber = scanner.nextLine(); |
| 82 | + Contact newContact = Contact.createContact(newName, newNumber); |
| 83 | + if(mobilePhone.updateContact(existingContactRecord, newContact)) { |
| 84 | + System.out.println("Successfully updated record"); |
| 85 | + } else { |
| 86 | + System.out.println("Error updating record."); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void removeContact() { |
| 91 | + System.out.println("Enter existing contact name: "); |
| 92 | + String name = scanner.nextLine(); |
| 93 | + Contact existingContactRecord = mobilePhone.queryContact(name); |
| 94 | + if (existingContactRecord == null) { |
| 95 | + System.out.println("Contact not found."); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if(mobilePhone.removeContact(existingContactRecord)) { |
| 100 | + System.out.println("Successfully deleted"); |
| 101 | + } else { |
| 102 | + System.out.println("Error deleting contact"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static void queryContact() { |
| 107 | + System.out.println("Enter existing contact name: "); |
| 108 | + String name = scanner.nextLine(); |
| 109 | + Contact existingContactRecord = mobilePhone.queryContact(name); |
| 110 | + if (existingContactRecord == null) { |
| 111 | + System.out.println("Contact not found."); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + System.out.println("Name: " + existingContactRecord.getName() + " phone number is " + existingContactRecord.getPhoneNumber()); |
| 116 | + } |
66 | 117 |
|
| 118 | + private static void startPhone() { |
| 119 | + System.out.println("Starting phone..."); |
67 | 120 | } |
68 | 121 |
|
69 | | - public static void printMenu() { |
70 | | - System.out.println("Options: \n" + |
71 | | - "\t 0 - Menu \n" + |
72 | | - "\t 1 - Add contact \n" + |
73 | | - "\t 2 - Print contact list \n" + |
74 | | - "\t 3 - Update contact \n" + |
75 | | - "\t 4 - Remove contact \n" + |
76 | | - "\t 5 - Search contact \n" + |
77 | | - "\t 9 - Exit"); |
| 122 | + private static void printActions() { |
| 123 | + System.out.println("\nAvailable actions:\npress"); |
| 124 | + System.out.println("0 - to shutdown\n" + |
| 125 | + "1 - to print contacts\n" + |
| 126 | + "2 - to add a new contact\n" + |
| 127 | + "3 - to update existing an existing contact\n" + |
| 128 | + "4 - to remove an existing contact\n" + |
| 129 | + "5 - query if an existing contact exists\n" + |
| 130 | + "6 - to print a list of available actions."); |
| 131 | + System.out.println("Choose your action: "); |
78 | 132 | } |
79 | 133 | } |
0 commit comments