-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCITmobile.java
More file actions
462 lines (270 loc) · 17.5 KB
/
FCITmobile.java
File metadata and controls
462 lines (270 loc) · 17.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Reem khalil - ID : 1505841 - section number : BAR - EMAIL : rkhalil0006@stu.kau.edu.sa */
package fcitmobile;
import java.io.*;
import java.util.*;
public class FCITmobile {
// ........ Data Members ........ //
// To be accessible for each method
static Scanner scan;
static PrintWriter print;
static FCITcustomer[] customers; // declear 1 large array, which will hold all of the FCIT customers
static FCITcustomer newCustomer;
// .......... Methods ........... //
public static void main(String[] args) throws FileNotFoundException {
// creating variables ..
File input = new File("FCITmobile.in.txt");
scan = new Scanner(input); // to read input from a inputfile
File output = new File("FCITmobile.out.txt");
print = new PrintWriter(output); // to write output in a output file
int maxCustomers = scan.nextInt(); // read the maximum number of customers that can be registered in the FCITMobile system.
customers = new FCITcustomer[maxCustomers]; // creating the customers array
boolean QUIT = false; // to help me in trace
while (scan.hasNext()) { // to read all lins .
switch (scan.next()) { // use switch to implemente The Commands
case "ADDCUSTOMER": // make a new FCITcustomer object if it's allowed then put it in suitable index & save this command in output file
if (FCITcustomer.getNumcustomers() < maxCustomers) {
ADDCUSTOMER();
print.println("Command: ADDCUSTOMER");
newCustomer.printInfo(print);
} else
scan.nextLine(); // Dispose of this object's information
break;
case "MAKECALL":
MAKECALL(scan.nextInt());
break;
case "SENDTEXT":
SENDTEXT(scan.nextInt());
break;
case "RECHARGE":
RECHARGE(scan.nextInt());
break;
case "SEARCH":
SEARCH(scan.nextInt());
break;
case "DISPLAYDETAILS":
DISPLAYDETAILS(scan.nextInt());
break;
case "QUIT":
QUIT = true;
print.println("Command: QUIT.");
print.println("\tExiting the FCIT Mobile System...");
print.println("\tGoodbye.");
}
if(QUIT)
break;
}
scan.close();
print.flush();
print.close();
}
public static void ADDCUSTOMER() // this method make a new FCITcustomer objectthen put it in suitable index
{
newCustomer = new FCITcustomer(); // make a new FCITcustomer object then scan from the file the customer ID, the first and last name of the customer, and the new phone number
newCustomer.setID(scan.nextInt());
newCustomer.setFirstName(scan.next());
newCustomer.setLastName(scan.next());
newCustomer.setPhoneNumber(scan.next());
// save the reference of this object inside the appropriate index of the customers array
if (FCITcustomer.getNumcustomers() == 0) { // Save the obj. in the first indix if the array is empty + increase the acounter ... then exit the method
customers[0] = newCustomer;
FCITcustomer.increaseNumcustomers();
return;
}
for (int i = 0; i < FCITcustomer.getNumcustomers(); i++) { // to find the correct insertion location if the array isn't empty
if (newCustomer.getID() > customers[i].getID() && i != (FCITcustomer.getNumcustomers() - 1)) {
continue; // to compare with a next customer .
} else if (newCustomer.getID() > customers[i].getID() && i == (FCITcustomer.getNumcustomers() - 1)) // This means that the ID of new customer is bigger than all IDs ,so i will store it in the latter
{
customers[FCITcustomer.getNumcustomers()] = newCustomer;
FCITcustomer.increaseNumcustomers();
return;
} // we use return; To leave the methode because we added the obj. in the array
else if (newCustomer.getID() < customers[0].getID()) { // This means that the ID of new customer is smaller than all IDs ,so i will SHIFT all objects to the right (one space to the right) & store it in the first
for (int r = (FCITcustomer.getNumcustomers()); r > 0; r--) {
customers[r] = customers[r - 1];
}
customers[0] = newCustomer;
FCITcustomer.increaseNumcustomers();
return;
} else if (newCustomer.getID() < customers[i].getID()) { // This means that the ID of new customer is smaller than specific IDs ,so i will do appropriate SHIFTING & store newCustomer in the empty indix
int helpCounter = FCITcustomer.getNumcustomers();
for (int j = 0; j <= (FCITcustomer.getNumcustomers() - i); j++) {
if (j == (FCITcustomer.getNumcustomers() - i)) {
customers[i] = newCustomer;
FCITcustomer.increaseNumcustomers();
return;
}
customers[helpCounter] = customers[(helpCounter - 1)];
helpCounter -= 1;
}
}
}
}
public static void MAKECALL(int ID) // this method process MAKECALL command
{
String callednumber = scan.next(); // scan date
int callDuration = scan.nextInt();
double callBalance = callDuration * 14; // calculate the coct of call & stor it in callBalance
boolean isfound = false; // to help me in trace
print.println("Command: MAKECALL"); // to save this command in output file
for (int i = 0; i < FCITcustomer.getNumcustomers(); i++) {
if (ID == customers[i].getID()) { // This means that record is found in the customers array
isfound = true;
if (callBalance <= customers[i].getBalance()) { //************* if he has enough balance *************//
customers[i].setBalance((customers[i].getBalance() - callBalance)); // 1 // We deduct the cost of the call from the balance
if (customers[i].getNumCalled() == 0) { // 2 // to add this call in index 0 Because the array is empty
customers[i].getCalledNumbers()[0] = callednumber;
customers[i].getCallDuration()[0] = callDuration;
customers[i].increaseNumCalled();
customers[i].printCallInfo(print, callednumber, callDuration, callBalance);
return;
} else {
for (int j = customers[i].getNumCalled(); j > 0; j--) { // 3 // to add last CalledNumber & callDuration in the first (in each array) & shift another objs to the rigth
if (j == 10) {
continue;
}
customers[i].getCalledNumbers()[j] = customers[i].getCalledNumbers()[j - 1];
customers[i].getCallDuration()[j] = customers[i].getCallDuration()[j - 1];
}
customers[i].getCalledNumbers()[0] = callednumber;
customers[i].getCallDuration()[0] = callDuration;
customers[i].increaseNumCalled();
customers[i].printCallInfo(print, callednumber, callDuration, callBalance);
return;
}
} else { //************** if has no balance or some balance, but not enough to complete the call *************//
callDuration = (int) customers[i].getBalance() / 14;
if (callDuration > 0) {
callBalance = callDuration * 14;
customers[i].setBalance((customers[i].getBalance() - callBalance)); // 1 // We deduct the cost of the call from the balance
for (int j = customers[i].getNumCalled(); j > 0; j--) { // 2 // to add this CalledNumber & callDuration in the well index (in CalledNumbers & CallDuration arrays) & shift another objs to the rigth
if (j == 10) {
continue; // to delete saved call .. since the FCITMobile system has limited memory
}
customers[i].getCalledNumbers()[j] = customers[i].getCalledNumbers()[j - 1];
customers[i].getCallDuration()[j] = customers[i].getCallDuration()[j - 1];
}
customers[i].getCalledNumbers()[0] = callednumber;
customers[i].getCallDuration()[0] = callDuration;
customers[i].increaseNumCalled();
customers[i].printCallInfo2(print, callednumber, callDuration, callBalance);
return;
} else { //************* if the customer has zero balance *************//
print.println("\tCannot perform MAKECALL. the customer has zero balance.");
return;
}
}
}
}
if (!isfound) // This means that record isn't found
{
print.println("\tCannot perform MAKECALL. Account does not exist in FCIT Mobile System.");
print.println();
}
}
public static void SENDTEXT(int ID) // this method process SENDTEXT command
{
String textedNumber = scan.next(); // scan date
boolean isfound = false; // to help me in trace
print.println("Command: SENDTEXT"); // to save this command in output file
for (int i = 0; i < FCITcustomer.getNumcustomers(); i++) {
if (ID == customers[i].getID()) { // This means that record is found in the customers array
isfound = true;
if (customers[i].getBalance() >= 8) { //************* if he has enough balance *************//
customers[i].setBalance((customers[i].getBalance() - 8)); // 1 // We deduct 8 halalas from the balance Because The text message rate is fixed at the rate of 8 halalas/message
if (customers[i].getNumTexted() == 0) { // 2 // to add this texted number in index 0 Because the array is empty
customers[i].getTextedNumbers()[0] = textedNumber;
customers[i].increaseNumTexted();
customers[i].printTextInfo(print, textedNumber);
return;
} else {
for (int j = customers[i].getNumTexted(); j > 0; j--) { // 3 // to add this texted number in the first in (textedNumbers array) & shift another objs to the rigth
if (j == 10) {
continue; // to delete saved NumTexted .. since the FCITMobile system has limited memory
}
customers[i].getTextedNumbers()[j] = customers[i].getTextedNumbers()[j - 1];
}
customers[i].getTextedNumbers()[0] = textedNumber;
customers[i].increaseNumTexted();
customers[i].printTextInfo(print, textedNumber);
return;
}
} else { //************** if has no balance or some balance, but not enough *************//
print.println("\tCannot perform SENDTEXT. the customer does not have enough balance.");
return;
}
}
}
if (!isfound) // This means that record isn't found
{
print.println("\tCannot perform SENDTEXT. Account does not exist in FCIT Mobile System.");
print.println();
}
}
public static void RECHARGE(int ID) // this method process RECHARGE command
{
boolean isfound = false; // to help me in trace
print.println("Command: RECHARGE"); // to save this command in output file
for (int i = 0; i < FCITcustomer.getNumcustomers(); i++) {
if (ID == customers[i].getID()) { // This means that record is found in the customers array
isfound = true;
double RechargeAmount = scan.nextDouble();
customers[i].setBalance((customers[i].getBalance() + (RechargeAmount * 100))); // it will be recharged by adding the amount to the existing balance & a message printed to output file
customers[i].printRechargeInfo(print, RechargeAmount);
return;
}
}
if (!isfound) // This means that record isn't found
{
print.println("\tCannot perform RECHARGE. Account does not exist in FCIT Mobile System.");
print.println();
}
}
public static void SEARCH(int ID) // this method process SEARCH command by using Linear search
{
boolean isfound = false; // to help me in trace
print.println("Command: SEARCH"); // to save this command in output file
for (int i = 0; i < FCITcustomer.getNumcustomers(); i++) {
if (ID == customers[i].getID()) { // This means that record is found in the customers array
isfound = true;
customers[i].printInfo(print); // to save this in output file
}
}
if (!isfound) // This means that record isn't found
{
print.println("\tCannot perform SEARCH. Account does not exist in FCIT Mobile System.");
print.println();
}
}
public static void DISPLAYDETAILS(int ID) // this method process DISPLAYDETAILS command by using Binary search
{
int low = 0;
int high = (FCITcustomer.getNumcustomers() - 1);
ArrayList<Integer> indices = new ArrayList(); // to save indices that are visited during the search then print it .
boolean isfound = false; // to help me in trace
print.println("Command: DISPLAYDETAILS"); // to save this command in output file
print.print("\tPerforming Binary Search...(Indices viewed: ");
while (low <= high) {
int mid = (low + high) / 2;
indices.add(mid);
if (ID == customers[mid].getID()) {
// meaning, “found” ... it will display last 10 call details (called number with duration) and last 10 numbers for sent messages.
//*****************************************************************************************************************************
isfound = true ;
for (int i = 0 ; i<indices.size() ; i++)
print.print(indices.get(i)+ " ");
print.println(")");
customers[mid].DISPLAYDETAILS(print);
break;
} else if (ID < customers[mid].getID())
high = mid - 1;
else // if ( ID > customers[mid].getID() )
low = mid + 1;
}
if ( ! isfound ) // this only happens if not found
{ print.println(")");
print.println("\tCannot perform DISPLAYDETAILS. Account does not exist in FCIT Mobile System.");
print.println();
}
}
}