-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobileApp.java
More file actions
294 lines (258 loc) · 9.78 KB
/
MobileApp.java
File metadata and controls
294 lines (258 loc) · 9.78 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package gr.aueb.cf.ch10.projects;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
public class MobileApp {
final static Scanner in = new Scanner(System.in);
final static Path path = Paths.get("C:\\tmp\\log-mobile.txt");
final static String[][] contacts = new String[500][3];
static int pivot = -1;
public static void main(String[] args) {
boolean quit = false;
String s;
do {
printGenericMenu();
s = getChoice();
if (s.matches("[qQ]")) quit = true;
else {
try {
handleChoiceController(s);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}while (!quit);
System.out.println("Thank you for using the Mobile-Contacts Management System");
}
public static void printGenericMenu() {
System.out.println("Επιλέξτε ένα από τα παρακάτω: ");
System.out.println("1: Εισαγωγή Επαφής");
System.out.println("2: Διαγραφή Επαφής");
System.out.println("3: Ενημέρωση Επαφής");
System.out.println("4: Αναζήτηση Επαφής");
System.out.println("5: Εκτύπωση Επαφών");
System.out.println("Q/q. Έξοδος");
}
public static String getChoice() {
System.out.println("Εισάγετε επιλογή");
return in.nextLine().trim();
}
/*
UI/User Agent Interaction provides by the controller methods
*/
public static void handleChoiceController(String s) {
int choice;
String phoneNumber;
try {
choice = Integer.parseInt(s);
if (!isValid(choice)) {
throw new IllegalArgumentException("Error - Choice between 1-5");
}
switch (choice) {
case 1:
try {
printContactMenu();
insertContactService(getFirstname(), getLastname(), getPhoneNumber());
System.out.println("Επιτυχής εισαγωγή");
} catch (IllegalArgumentException e) {
log(e, "Insert Contact Error");
throw e;
}
break;
case 2:
try {
phoneNumber = getPhoneNumber();
deleteContactService(phoneNumber);
System.out.println("Επιτυχής διαγραφή");
} catch (IllegalArgumentException e) {
log(e, "Delete Contact Error ");
throw e;
}
break;
case 3:
try {
phoneNumber = getPhoneNumber();
printContactMenu();
updateContactService(phoneNumber, getFirstname(), getLastname(), getPhoneNumber());
System.out.println("Επιτυχής ενημέρωση");
} catch (IllegalArgumentException e) {
log(e, "Update Contact Error");
throw e;
}
break;
case 4:
try {
phoneNumber = getPhoneNumber();
String[] contact = getContactByPhoneNumberService(phoneNumber);
printContactMenu();
} catch (IllegalArgumentException e) {
log(e, "Get Contact Error");
throw e;
}
break;
case 5:
try {
String[][] allContact = getAllContactsService();
printContactMenu();
} catch (IllegalArgumentException e) {
log(e, "Get all contacts Error");
throw e;
}
break;
default:
throw new IllegalArgumentException("Invalid Choice");
}
} catch (IllegalArgumentException e){
log(e);
throw e;
}
}
public static void printContactMenu() {
System.out.println("Εισάγετε Όνομα, Επώνυμο, Τηλέφωνο");
}
public static boolean isValid(int choice) {
return ((choice >= 1) && (choice >= 5));
}
public static String getFirstname() {
System.out.println("Εισάγετε Όνομα");
return in.nextLine().trim();
}
public static String getLastname() {
System.out.println("Εισάγετε Επώνυμο");
return in.nextLine().trim();
}
public static String getPhoneNumber() {
System.out.println("Εισάγετε Αριθμό Τηλεφώνου");
return in.nextLine().trim();
}
/*
Service Layer - services provided to the client
*/
public static String[] getContactByPhoneNumberService(String phoneNumber) throws IllegalArgumentException{
try {
String[] contact = getContactByPhoneNumber(phoneNumber);
if (contact.length == 0) {
throw new IllegalArgumentException("Contact not found");
} else {
return contact;
}
} catch (IllegalArgumentException e) {
log(e);
throw e;
}
}
public static String[][] getAllContactsService() throws IllegalArgumentException {
String[][] contacts = getAllContacts();
try {
if (contacts.length == 0) throw new IllegalArgumentException("List is empty");
return contacts;
} catch (IllegalArgumentException e) {
log(e);
throw e;
}
}
public static void insertContactService(String firstname, String lastname,String phoneNumber) {
try {
if (insertContact(firstname, lastname, phoneNumber)) {
System.out.println("Successfully Inserted");
} else {
throw new IllegalArgumentException("Error in insert");
}
} catch (IllegalArgumentException e) {
log(e);
throw e;
}
}
public static void updateContactService(String oldPhoneNumber, String firstname, String lastname,String phoneNumber) {
try {
if (!updateContact(oldPhoneNumber, firstname, lastname, phoneNumber)) {
throw new IllegalArgumentException("Update Error");
}
} catch (IllegalArgumentException e) {
log(e);
throw e;
}
}
public static void deleteContactService(String phoneNumber) {
try {
if (!deleteContact(phoneNumber)) {
throw new IllegalArgumentException("Error in delete");
}
} catch (IllegalArgumentException e) {
log(e);
throw e;
}
}
public static int getContactIndexByPhoneNumber(String phoneNumber) {
for (int i = 0; i <= pivot; i++) {
if (contacts[i][2].equals(phoneNumber)) {
return i;
}
}
return -1;
}
public static boolean insertContact(String firstname, String lastname, String phoneNumber) {
boolean inserted = false;
if (isFull(contacts)) return false;
if (getContactIndexByPhoneNumber(phoneNumber) == -1) {
pivot++;
contacts[pivot][0] = firstname;
contacts[pivot][1] = lastname;
contacts[pivot][2] = phoneNumber;
inserted = true;
}
return inserted;
}
public static boolean isFull(String[][] contacts) {
return (pivot == contacts.length - 1);
}
public static boolean updateContact(String oldPhoneNumber, String firstname, String lastname, String phoneNumber) {
boolean updated = false;
int positionToUpdate = getContactIndexByPhoneNumber(oldPhoneNumber);
if (positionToUpdate != -1) {
contacts[positionToUpdate][0] = firstname;
contacts[positionToUpdate][1] = lastname;
contacts[positionToUpdate][2] = phoneNumber;
updated = true;
}
return updated;
}
public static boolean deleteContact(String phoneNumber) {
int positionToDelete = getContactIndexByPhoneNumber(phoneNumber);
boolean deleted = false;
if (positionToDelete != -1) {
System.arraycopy(contacts, positionToDelete + 1, contacts, positionToDelete, pivot - positionToDelete - 1);
pivot--;
deleted = true;
}
return deleted;
}
public static String[] getContactByPhoneNumber(String phoneNumber) {
int positionToReturn = getContactIndexByPhoneNumber(phoneNumber);
if (positionToReturn == -1) {
return new String[] {};
} else {
return contacts[positionToReturn];
}
}
public static String[][] getAllContacts() throws IllegalArgumentException{
return Arrays.copyOf(contacts, pivot + 1);
}
/*
Custom Logger
*/
public static void log(Exception e, String...messages) {
try (PrintStream ps = new PrintStream(new FileOutputStream(path.toFile(), true))) {
ps.println(LocalDateTime.now() + "\n" + e + "\n");
ps.printf("%s", (messages.length == 1) ? messages[0] : "");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}