-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReservationManager.java
More file actions
218 lines (194 loc) · 6.33 KB
/
Copy pathReservationManager.java
File metadata and controls
218 lines (194 loc) · 6.33 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
import java.util.ArrayList;
import java.util.Scanner;
/**
* Represents a Reservation Manager in the restaurant.
* A Reservation Manager can create, check and cancel reservations.
* @author Tan Zheng Kai
* @version 11
* @since 2021-11-13
*/
public class ReservationManager {
/**
* The list of all reservations made.
*/
private ArrayList<Reservation> reservationList;
/**
* Create ReservationManager with a Reservation ArrayList
*/
public ReservationManager() {
reservationList = new ArrayList<Reservation>();
}
/**
* Prints a selection of choices for user to pick.
* User can either create a reservation, check their reservation status
* or cancel their current reservation.
* Reservation manager can also remove expired reservations present in the list.
*/
public void printInterface(){
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\nReservation Manager Class");
System.out.println("(1) Create Reservation");
System.out.println("(2) Check Reservation");
System.out.println("(3) Remove Reservation");
System.out.println("(4) Remove Expired Reservation(s)");
System.out.println("(5) Print Reservation List");
System.out.println("(6) Exit");
System.out.print("Enter the number of your choice: ");
choice = sc.nextInt();
System.out.println();
sc.nextLine();
switch (choice) {
case 1:
this.createReservation();
break;
case 2:
this.checkReservation();
break;
case 3:
this.removeReservation();
break;
case 4:
this.checkAndRemoveExpired();
break;
case 5:
this.printReservationList();
}
} while (choice < 6);
}
/**
* Creating a Reservation with a customer's contact number,
* date of reservation to be made, expected time of arrival and
* number of pax.
* Checks for available table with right number of seats to be reserved.
* Reservation will fail if a table cannot be found.
*/
public void createReservation() {
Scanner sc = new Scanner(System.in);
System.out.println("Creating Reservation ..");
System.out.print("Please enter name: ");
String name = sc.nextLine();
System.out.print("Please enter contact number: ");
int contactNumber = sc.nextInt();
System.out.print("Please enter date (dd-MM-yyyy): ");
sc.nextLine();
String date = sc.nextLine();
System.out.print("Please enter time (HH:mm): ");
String time = sc.nextLine();
int pax = RestaurantApp.globalTableManager.getUserInput();
int tableID = RestaurantApp.globalTableManager.findSuitableTableFromAvailable(pax);
if (tableID == -1){
System.out.println("Sorry, there are no more tables available for " + pax + "pax.");
return;
}
else {
RestaurantApp.globalTableManager.setTableToReserved(tableID);
}
Reservation reservation = new Reservation(contactNumber, date + " " + time, pax, name);
reservationList.add(reservation);
System.out.println("Reservation created successfully.");
}
/**
* Checking reservation using contact number of person who booked it.
*/
public void checkReservation(){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter contact number to check reservation: ");
int contactNumber = sc.nextInt();
Reservation reservationFound=checkExist(contactNumber);
System.out.print("\n");
if(reservationFound==null){
System.out.println("No such reservation found!");
}
else{
reservationFound.printReservation();
}
}
/** Checking if reservation exists.
* @param contactNumber Contact number to be checked with reservationList.
* @return The Reservation that is being searched for.
*/
public Reservation checkExist(int contactNumber){
Reservation contactFound = null;
boolean contactMatch;
for(Reservation res : reservationList){
contactMatch=res.checkContact(contactNumber);
if(contactMatch==true){
contactFound=res;
break;
}
}
return contactFound;
}
/**
* Removing a reservation from reservationList with an input contactNumber.
*/
public void removeReservation() {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter contact number to remove reservation: ");
int contactNumber = sc.nextInt();
Reservation reservationFound=checkExist(contactNumber);
if(reservationFound==null){
System.out.println("No such reservation found!");
}
else{
System.out.println("Changing reserved table to available...");
int minSeats = reservationFound.getNumPax();
int tableID = RestaurantApp.globalTableManager.findSuitableTableFromReserved(minSeats);
if (tableID != -1){
RestaurantApp.globalTableManager.setTableToAvailable(tableID);
}
reservationList.remove(reservationFound);
}
System.out.println("Reservation removed successfully.");
}
/**
* Checks and remove expired Reservations.
*/
public void checkAndRemoveExpired() {
int reservationListSize = reservationList.size();
boolean expired = false;
int currentIndex = 0;
Reservation currentReservation;
for (int counter = 0; counter < reservationListSize; counter++)
{
currentReservation = reservationList.get(currentIndex);
expired = currentReservation.getIsExpired();
if (expired == true){
System.out.println("Changing reserved table to available...");
int tableID = RestaurantApp.globalTableManager.findSuitableTableFromReserved(currentReservation.getNumPax());
if (tableID != -1){
RestaurantApp.globalTableManager.setTableToAvailable(tableID);
}
reservationList.remove(currentIndex);
continue;
}
currentIndex++;
}
System.out.println("Expired reservation(s) have been removed.");
}
/**
* Getting Reservation object.
* @param contact The reservation made by this contact number.
* @return Reservation object.
*/
public Reservation getReservation(int contact) {
for (Reservation res : reservationList){
if(res.getContact()==contact){
return res;
}
}
System.out.println("Reservation not found");
return null;
}
/**
* Prints the reservationList.
*/
public void printReservationList(){
System.out.println("Reservation List");
for (Reservation reservation : reservationList){
reservation.printReservation();
}
}
}