-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain
More file actions
239 lines (199 loc) · 9.45 KB
/
Main
File metadata and controls
239 lines (199 loc) · 9.45 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
import java.util.Scanner;
import java.time.format.DateTimeFormatter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.io.File;
public class Main
{
Scanner inputLoginUI = new Scanner(System.in);
Scanner inputSystemUI = new Scanner(System.in);
public int loginUI()
{
// Display welcome message and options
int loginOption = 0;
do
{
System.out.println("\n\nWelcome to the e-Hailing Booking System!");
System.out.println("1. Register a new account");
System.out.println("2. Client login");
System.out.println("3. Administrator login");
System.out.print("\nChoose an option: ");
loginOption = inputLoginUI.nextInt();
if (loginOption != 1 && loginOption != 2 && loginOption != 3)
{
System.out.println("Invalid operation (loginUI)");
}
}while (loginOption != 1 && loginOption != 2 && loginOption != 3);
return loginOption;
}
private int systemUI(String username)
{
int systemOption = 0; //initialize systemOption with a value
do
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("\n\nDear user\t\t\t\t\tCurrent Date and Time " + dtf.format(now)); //get username from login
System.out.println("Welcome to e-Hailing Booking System");
System.out.println("_____________________________________________________________________________________________");
System.out.println("Do you want to: ");
System.out.println("<1> Create/Request Booking(s)");
System.out.println("<2> View Booking(s)");
System.out.println("<3> Quit");
systemOption = inputSystemUI.nextInt();
inputSystemUI.nextLine();
if (systemOption != 1 && systemOption != 2 && systemOption != 3)
{
System.out.println("Invalid operation(system UI)");
}
} while (systemOption != 1 && systemOption != 2 && systemOption != 3);
return systemOption;
}
private String bookingUI() {
String username; // Assume username is obtained from a session or entered by the user
System.out.println("Please enter your username:");
Scanner inputScanner = new Scanner(System.in);
username = inputScanner.nextLine(); // Read username
String bookingIDInput = "0"; //initialize the variable with a temporary value
System.out.println("Booking ID\tUsername\tDate\t\tTime\tPayment Method");
System.out.println("__________________________________________________________________________________");
try {
String bookingDirectory = "booking.txt";
File bookingFile = new File(bookingDirectory);
Scanner bookingDataReader = new Scanner(bookingFile);
while (bookingDataReader.hasNextLine()) {
String bookingData = bookingDataReader.nextLine();
// Handle different delimiters in the data file
String[] dataArray = bookingData.contains("|") ? bookingData.split("\\|") : bookingData.split(",");
if (dataArray[1].equals(username)) { // Filter by username
System.out.println(dataArray[0] + "\t\t" + dataArray[1] + "\t\t" + dataArray[2] + "\t" + dataArray[3] + "\t" + dataArray[4]);
if (bookingIDInput.equals("0")) {
bookingIDInput = dataArray[0]; // Set to the first matching booking ID
}
}
}
bookingDataReader.close();
System.out.println("Please enter the respective Booking ID:");
bookingIDInput = inputScanner.nextLine(); // Read selected booking ID
} catch (IOException e) {
System.out.println("An error occurred during the booking file opening.");
} finally {
//inputScanner.close(); // Ensure the scanner is closed after use
}
return bookingIDInput;
}
public void adminSystemUI()
{
String bookingID = "0";
String bookingName;
String bookingDate;
String bookingTime;
String bookingPaymentMethod;
String bookingDepartureLocation;
String bookingDestination;
System.out.print("\033[H\033[2J");
System.out.println("Dear Admin, Welcome back to e-Hailing Booking System");
System.out.println("______________________________________________________________________________________________________________________________________");
//shows all booking ID with indexes at the front, let the admin choose the booking with its index no
System.out.println("Index No.\tBooking ID\tName\tDate\t\tTime\tPayment Method\t\tDeparture Location\t\tDestination");
int numBooking = 0; // check how many lines are there in the booking text file
File bookingFile = new File("booking.txt");
try
{
Scanner readerBooking = new Scanner(bookingFile);
while (readerBooking.hasNextLine()) {
numBooking++;
readerBooking.nextLine(); // Move scanner to the next line
}
readerBooking.close();
// Reopen the scanner to read from the beginning of the file
readerBooking = new Scanner(bookingFile);
for (int i = 1; i <= numBooking; i++)
{ // Start from 1 and use <= to include all lines
String data = readerBooking.nextLine();
String[] dataArrayBooking = data.split("\\|");
bookingID = dataArrayBooking[0];
bookingName = dataArrayBooking[1];
bookingDate = dataArrayBooking[2];
bookingTime = dataArrayBooking[3];
bookingPaymentMethod = dataArrayBooking[4];
bookingDepartureLocation = dataArrayBooking[5];
bookingDestination = dataArrayBooking[6];
System.out.println(i + ".\t\t" + bookingID + "\t\t" + bookingName + "\t" + bookingDate + "\t" + bookingTime + "\t" + bookingPaymentMethod + "\t\t" + bookingDepartureLocation + "\t\t\t" + bookingDestination);
}
readerBooking.close();
} catch(IOException e)
{
System.out.println("An error has occured (admin system UI)");
e.getMessage();
}
}
public static void main(String[] args)
{
Main main = new Main();
BookingManager book = new BookingManager();
LoginAndRegister lar = new LoginAndRegister();
Client c = new Client();
int loginOption, systemOption;
int loginValidateClient; //1 = true, 2 = false
int loginValidateAdmin;
String bookingIDInput;
String username = c.getUsername();
do
{
loginOption = main.loginUI();
try
{
if (loginOption == 1) //client register
{
lar.clientRegister();
}
else if (loginOption == 2) //client login
{
loginValidateClient = lar.clientLogin();
if (loginValidateClient == 1)
{
systemOption = main.systemUI(username);
if (systemOption == 1) //Create bookings
{
//create bookings and save the details in respective user text file (make a method in client class and save the booking details in text file)
book.bookingCreate();
}
else if (systemOption == 2) //View bookings
{
//view all bookings and a new interface for the user to update, cancel and search for certain booking
bookingIDInput = main.bookingUI();
book.bookingView(bookingIDInput);
}
else if (systemOption == 3) //Quit program
{
System.out.println("\033[H\033[2J"); //clear screen
System.out.println("Account logout successfully.");
}
}
else
{
System.out.println("Incorrect username and password. Please try again.");
}
}
else if (loginOption == 3) //admin login
{
loginValidateAdmin = lar.adminLogin();
if (loginValidateAdmin == 1) //admin login succeed
{
main.adminSystemUI();
}
else if (loginValidateAdmin == 2) //admin login failed
{
System.out.println("Incorrect username and password. Please try again.");
main.loginUI();
}
}
} catch (Exception e)
{
System.out.println("\n\nAn error has occured. Please try again (main method)\n");
e.printStackTrace();
}
} while (loginOption != 1 && loginOption != 2);
}
}