-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineReservationSystem.java
162 lines (140 loc) · 6.87 KB
/
OnlineReservationSystem.java
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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class OnlineReservationSystem {
private Map<String, String> users; // Store username-password pairs
private Map<String, String> reservations; // Store reservation data
public OnlineReservationSystem() {
users = new HashMap<>();
reservations = new HashMap<>();
}
public void run() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("------------------------------------------------");
System.out.println("----- WELCOME TO ONLINE RESERVATION SYSTEM -----");
System.out.println("------------------------------------------------");
System.out.println("Please Select One Options..." + "\n");
System.out.println("1.>>> Register >>>");
System.out.println("2.>>> Login >>>");
System.out.println("3.>>> Exit >>>");
System.out.println("------------------------------------------------");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
register(sc);
break;
case 2:
login(sc);
break;
case 3:
System.out.println("------------------------------------------------");
System.out.println("\n" + "Exiting...");
System.out.println("\n" + "------------------------------------------------");
System.out.println("\n" + "Thank You.!!! Please Visit Again...");
System.out.println("\n" + "------------------------------------------------");
return;
default:
System.out.println("------------------------------------------------");
System.out.println("\n" + "Invalid choice... Please Try again...");
break;
}
System.out.println();
}
}
private void register(Scanner sc) {
System.out.println("------------------------------------------------");
System.out.println("--------------- REGISTRATION PAGE --------------");
System.out.println("------------------------------------------------");
System.out.print("Enter username: ");
String username = sc.nextLine();
if (users.containsKey(username)) {
System.out.println("\n" + "Username already exists... Try again...");
return;
}
System.out.print("Enter password: ");
String password = sc.nextLine();
users.put(username, password);
System.out.println("\n" + "Registration successful... You can now log in...");
}
private void login(Scanner sc) {
System.out.println("------------------------------------------------");
System.out.println("------------------ LOGIN PAGE ------------------");
System.out.println("------------------------------------------------");
System.out.print("Enter username: ");
String username = sc.nextLine();
System.out.print("Enter password: ");
String password = sc.nextLine();
if (users.containsKey(username) && users.get(username).equals(password)) {
System.out.println("\n" + "Login successful...");
reservationMenu(sc, username);
} else {
System.out.println("\n" + "Invalid username or password...");
}
}
private void reservationMenu(Scanner sc, String username) {
while (true) {
System.out.println("------------------------------------------------");
System.out.println("------------------- HOME PAGE ------------------");
System.out.println("------------------------------------------------");
System.out.println("Please Select One Options..." + "\n");
System.out.println("1.>>> Make a reservation >>>");
System.out.println("2.>>> Cancel a reservation >>>");
System.out.println("3.>>> Logout");
System.out.println("------------------------------------------------");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
makeReservation(sc, username);
break;
case 2:
cancelReservation(sc, username);
break;
case 3:
System.out.println("------------------------------------------------");
System.out.println("\n" + "Logging out...");
return;
default:
System.out.println("------------------------------------------------");
System.out.println("\n" + "Invalid choice. Try again.");
break;
}
System.out.println();
}
}
private void makeReservation(Scanner sc, String username) {
System.out.println("------------------------------------------------");
System.out.print("Enter reservation details: ");
String reservationDetails = sc.nextLine();
if (reservations.containsKey(username)) {
System.out.println("\n" + "You already have a reservation. Cancel it first to make a new one...");
return;
}
reservations.put(username, reservationDetails);
System.out.println("\n" + "Reservation created successfully...");
}
private void cancelReservation(Scanner sc, String username) {
if (reservations.containsKey(username)) {
System.out.println("------------------------------------------------");
System.out.println("Your current reservation: " + reservations.get(username));
System.out.print("Do you want to cancel this reservation? (Y/N): ");
String confirmation = sc.nextLine();
if (confirmation.equalsIgnoreCase("Y")) {
reservations.remove(username);
System.out.println("\n" + "Reservation cancelled successfully...");
} else {
System.out.println("\n" + "Reservation not cancelled...");
}
} else {
System.out.println("\n" + "You don't have any reservations...");
}
}
public static void main(String[] args) {
OnlineReservationSystem system = new OnlineReservationSystem();
system.run();
}
}