-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
123 lines (107 loc) · 4.17 KB
/
Copy pathMain.java
File metadata and controls
123 lines (107 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
private static MobilePhone mobilePhone = new MobilePhone("507 226 2266");
public static void main(String[] args) {
boolean quit = false;
startPhone();
printActions();
while(!quit) {
System.out.println("\nEnter action: (6 to show available actions)");
int action = scanner.nextInt();
switch (action) {
case 0:
System.out.println("\nShutting down...");
break;
case 1:
mobilePhone.printContacts();
break;
case 2:
addNewContact();
break;
case 3:
updateContact();
break;
case 4:
removeContact();
break;
case 5:
queryContact();
break;
case 6:
printActions();
break;
}
}
}
private static void addNewContact() {
System.out.println("Enter new contact name: ");
String name = scanner.next();
System.out.println("Enter phone number: ");
String phone = scanner.next();
Contact newContact = Contact.createContact(name, phone);
if(mobilePhone.addNewContact(newContact)) {
System.out.println("New contact added: \n name = " + name + ", phone = " + phone);
} else {
System.out.println("Cannot add, " + name + " already on file.");
}
}
private static void updateContact() {
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if(existingContactRecord == null) {
System.out.println("Contact not found.");
return;
}
System.out.println("Enter new contact name: ");
String newName = scanner.nextLine();
System.out.println("Enter new contact phone number: ");
String newNumber = scanner.nextLine();
Contact newContact = Contact.createContact(newName, newNumber);
if(mobilePhone.updateContact(existingContactRecord, newContact)) {
System.out.println("Successfully updated record");
} else {
System.out.println("Error updating record.");
}
}
private static void removeContact() {
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if(existingContactRecord == null) {
System.out.println("Contact not found.");
return;
}
if(mobilePhone.removeContact(existingContactRecord)) {
System.out.println("Successfully deleted!");
} else {
System.out.println("Error deleting contact");
}
}
private static void queryContact() {
System.out.println("Enter existing contact name: ");
String name = scanner.nextLine();
Contact existingContactRecord = mobilePhone.queryContact(name);
if(existingContactRecord == null) {
System.out.println("Contact not found.");
return;
}
System.out.println("Name " + existingContactRecord.getName() + " phone number is " +
existingContactRecord.getPhoneNumber());
}
private static void startPhone() {
System.out.println("Starting phone...");
}
private static void printActions() {
System.out.println("\nAvailable actions: \npress");
System.out.println("0 - to shutdown\n" +
"1 - print contacts\n" +
"2 - add a new contact\n" +
"3 - update existing contact\n" +
"4 - to remove an existing contact\n" +
"5 - query if an existing contact exists\n" +
"6 - to print a list of available actions.\n");
System.out.println("Choose your action: ");
}
}