-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBookingManager
More file actions
391 lines (350 loc) · 15.4 KB
/
BookingManager
File metadata and controls
391 lines (350 loc) · 15.4 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
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BookingManager
{
Scanner scanner = new Scanner(System.in);
private static int bookingIdCounter = 1;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
//create booking method
public void bookingCreate()
{
Scanner bookingCreateInput = new Scanner(System.in);
System.out.println("\nEnter booking details");
String name = "";
boolean validName = false;
while (!validName)
{
System.out.print("Enter your name: ");
name = bookingCreateInput.nextLine();
if (!name.matches("[0-9]+"))
{
validName = true;
} else
{
System.out.println("Wrong format. Name cannot contain numerical characters. Please re-enter.");
}
}
String date = "";
boolean validDate = false;
while (!validDate)
{
System.out.print("Enter date (yyyy-MM-dd): ");
date = bookingCreateInput.nextLine();
try
{
DATE_FORMAT.parse(date);
validDate = true;
} catch (ParseException e)
{
System.out.println("Wrong format. Please enter date in format yyyy-MM-dd");
}
}
String time = "";
boolean validTime = false;
while (!validTime)
{
System.out.print("Enter time (HH:mm): ");
time = bookingCreateInput.nextLine();
try
{
TIME_FORMAT.parse(time);
validTime = true;
} catch (ParseException e)
{
System.out.println("Wrong format. Please enter time in format HH:mm");
}
}
File driverFile = new File("driver.txt");
try
{
Scanner driverFileReader = new Scanner(driverFile);
while (driverFileReader.hasNextLine())
{
String driverData = driverFileReader.nextLine();
String[] driverDataArray = driverData.split(",");
String driverName = driverDataArray[0];
String driverTotalRating = driverDataArray[1];
String driverRatingNum = driverDataArray[2];
double aDriverTotalRating = Double.parseDouble(driverTotalRating); //convert string to double
int aDriverRatingNum = Integer.parseInt(driverRatingNum);
double avgRating = aDriverTotalRating/aDriverRatingNum;
System.out.println("\nDriver Name: " + driverName);
System.out.println("Driver average rating: " + avgRating + "(" + aDriverRatingNum + ")");
}
driverFileReader.close();
//input to choose driver
}catch (IOException e)
{
System.out.println("An error occured");
}
int paymentChoice = 0;
boolean validPaymentChoice = false;
while (!validPaymentChoice)
{
System.out.println("\nChoose payment method:");
System.out.println("1. QR Payment");
System.out.println("2. Online Banking");
String paymentInput = bookingCreateInput.nextLine();
if (paymentInput.equals("1") || paymentInput.equals("2"))
{
paymentChoice = Integer.parseInt(paymentInput);
validPaymentChoice = true;
} else
{
System.out.println("Wrong format. Please enter 1 or 2 for payment method.");
}
}
String paymentMethod = (paymentChoice == 1) ? "QR Payment" : "Online Banking";
String departureLocation = "";
boolean validDepartureLocation = false;
while (!validDepartureLocation)
{
System.out.print("Enter departure location: ");
departureLocation = bookingCreateInput.nextLine();
if (!departureLocation.matches("[0-9]+"))
{
validDepartureLocation = true;
} else
{
System.out.println("Wrong format. Departure location cannot contain numerical characters. Please re-enter.");
}
}
String destination = "";
boolean validDestination = false;
while (!validDestination)
{
System.out.print("Enter destination: ");
destination = bookingCreateInput.nextLine();
if (!destination.matches("[0-9]+"))
{
validDestination = true;
} else
{
System.out.println("Wrong format. Destination cannot contain numerical characters. Please re-enter.");
}
}
bookingCreateInput.close();
// Write booking details to file
try (BufferedWriter writer = new BufferedWriter(new FileWriter("booking.txt", true)))
{
//Increment and format booking ID
File bookingFile = new File("booking.txt");
Scanner bookingReader = new Scanner(bookingFile);
while (bookingReader.hasNextLine())
{
bookingIdCounter++;
bookingReader.nextLine();
}
bookingReader.close();
String bookingId = "B" + String.format("%04d", bookingIdCounter);
writer.write(bookingId + "|" + name + "|" + date + "|" + time + "|" + paymentMethod + "|" + departureLocation + "|" + destination);
writer.newLine();
System.out.println("Booking successful. Your booking ID is: " + bookingId);
} catch (IOException e)
{
System.out.println("Error writing to file: " + e.getMessage());
}
}
//view booking method
public void bookingView(String bookingIDInput)
{
try
{
String bookingDirectory = "booking.txt";
File bookingFile = new File(bookingDirectory);
Scanner bookingReader = new Scanner(bookingFile);
while (bookingReader.hasNextLine())
{
String bookingData = bookingReader.nextLine();
String dataArray[] = bookingData.contains("|") ? bookingData.split("\\|") : bookingData.split(",");
if (dataArray[0].equals(bookingIDInput))
{
System.out.println("\n\n\n");
System.out.println("Booking ID: " + dataArray[0]);
System.out.println("Name: " + dataArray[1]);
System.out.println("Date: " + dataArray[2]);
System.out.println("Time: " + dataArray[3]);
System.out.println("Payment Method: " + dataArray[4]);
System.out.println("Departure location: " + dataArray[5]);
System.out.println("Destination: " + dataArray[6]);
}
//bookingReader.close();
}
String bookingViewOption;
Scanner bookingViewInput = new Scanner(System.in);
do
{
System.out.println("\n\nDo you want to: ");
System.out.println("<1> Update booking");
System.out.println("<2> Delete booking");
System.out.println("<3> Quit");
System.out.print("---> ");
//Scanner bookingViewInput = new Scanner(System.in);
bookingViewOption = bookingViewInput.nextLine();
//bookingViewInput.close();
}while (!bookingViewOption.equals("1") && !bookingViewOption.equals("2") && !bookingViewOption.equals("3"));
if (bookingViewOption.equals("1")) // Update booking
{
System.out.println("Update Booking");
updateBooking(bookingIDInput);
}
else if (bookingViewOption.equals("2")) // Delete booking
{
System.out.println("Delete Booking");
deleteBooking(bookingIDInput);
}
else // Quit program
{
System.out.print("\033[H\033[2J");
}
}catch (IOException e)
{
System.out.println("An error has occured (booking view)");
}
}
//delete booking method
public void deleteBooking(String bookingID)
{
String bookingIdToDelete = bookingID;
String bookingDirectory = "booking.txt";
try
{
File inputFile = new File(bookingDirectory);
File tempFile = new File("temp.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)))
{
String line;
boolean found = false;
while ((line = reader.readLine()) != null)
{
String[] bookingDetails = line.split("\\|");
if (bookingDetails.length > 0 && bookingDetails[0].equalsIgnoreCase(bookingIdToDelete))
{
found = true;
} else
{
writer.write(line + "\n");
}
}
if (!found)
{
System.out.println("No booking found for Booking ID: " + bookingIdToDelete);
}
}
if (inputFile.delete() && tempFile.renameTo(inputFile))
{
System.out.println("Booking deleted successfully.");
}
else
{
System.out.println("Failed to delete booking.");
}
} catch (IOException e)
{
System.err.println("Error deleting booking: " + e.getMessage());
}
}
//update booking method
public void updateBooking(String bookingIdToUpdate) {
String bookingDirectory = "booking.txt";
System.out.println("Enter booking ID to update:");
bookingIdToUpdate =scanner.nextLine().trim();
try (BufferedReader reader = new BufferedReader(new FileReader(bookingDirectory))) {
List<String[]> dataArray = new ArrayList<>();
// Read lines and populate dataArray with split values
String line;
while ((line = reader.readLine()) != null) {
String[] bookingDetails = line.split("\\|", -1); // Use "|" as delimiter
dataArray.add(bookingDetails);
}
System.out.println("DataArray contents before update:");
for (String[] booking : dataArray) {
System.out.println(Arrays.toString(booking));
}
boolean found = false;
for (int i = 0; i < dataArray.size(); i++) {
String[] bookingDetails = dataArray.get(i);
if (bookingDetails.length >= 1 && bookingDetails[0].equals(bookingIdToUpdate)) {
System.out.println("Current Booking Details:");
displayBookingDetails(bookingDetails);
String updatedDetails = enterUpdatedBookingDetails(bookingDetails);
String[] updatedBooking = updatedDetails.split("\\|", -1); // Update booking details
// Ensure the updated booking retains the original bookingId and name
updatedBooking[0] = bookingIdToUpdate;
updatedBooking[1] = bookingDetails[1]; // Keep the original name
// Update dataArray with the modified booking details
dataArray.set(i, updatedBooking);
System.out.println("Booking details updated successfully.");
found = true;
break; // Exit the loop once booking is updated
}
}
if (!found) {
System.out.println("Booking not found for ID: " + bookingIdToUpdate);
}
// Write the updated dataArray back to the file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(bookingDirectory))) {
for (String[] booking : dataArray) {
StringBuilder lineBuilder = new StringBuilder();
for (int j = 0; j < booking.length; j++) {
lineBuilder.append(booking[j]);
if (j < booking.length - 1) {
lineBuilder.append("|");
}
}
writer.write(lineBuilder.toString());
writer.newLine();
}
System.out.println("Data written back to file successfully.");
} catch (IOException e) {
System.err.println("Error writing updated booking data to file: " + e.getMessage());
}
} catch (IOException e) {
System.err.println("Error updating booking: " + e.getMessage());
}
}
private String enterUpdatedBookingDetails(String[] currentBookingDetails) {
System.out.println("Enter new details (date|time|payment|departure|destination):");
// Display current details except for the booking ID and name
System.out.println("Current Name: " + currentBookingDetails[1]);
System.out.println("Current Date: " + currentBookingDetails[2]);
System.out.println("Current Time: " + currentBookingDetails[3]);
System.out.println("Current Payment Method: " + currentBookingDetails[4]);
System.out.println("Current Departure Location: " + currentBookingDetails[5]);
System.out.println("Current Destination: " + currentBookingDetails[6]);
// Prompt for new details and return as a formatted string
System.out.print("New Date: ");
String newDate = scanner.nextLine().trim();
System.out.print("New Time: ");
String newTime = scanner.nextLine().trim();
System.out.print("New Payment Method: ");
String newPaymentMethod = scanner.nextLine().trim();
System.out.print("New Departure Location: ");
String newDeparture = scanner.nextLine().trim();
System.out.print("New Destination: ");
String newDestination = scanner.nextLine().trim();
// Construct the updated details string
return currentBookingDetails[0] + "|" +currentBookingDetails[1]+"|"+ newDate + "|" + newTime + "|" + newPaymentMethod + "|" + newDeparture + "|" + newDestination;
}
private void displayBookingDetails(String[] bookingDetails) {
System.out.println("Booking ID: " + bookingDetails[0]);
System.out.println("Name: " + bookingDetails[1]);
System.out.println("Date: " + bookingDetails[2]);
System.out.println("Time: " + bookingDetails[3]);
System.out.println("Payment Method: " + bookingDetails[4]);
System.out.println("Departure Location: " + bookingDetails[5]);
System.out.println("Destination: " + bookingDetails[6]);
System.out.println();
}
}