-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableManager.java
More file actions
261 lines (244 loc) · 8.82 KB
/
Copy pathTableManager.java
File metadata and controls
261 lines (244 loc) · 8.82 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
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Represents a TableManager that will be in charge of the
* operations done on Table objects.
* @author Benjamin Chun Wei Jie
* @version 13
* @since 2021-11-13
*/
public class TableManager {
/**
* ArrayList of Table objects
*/
private ArrayList<Table> tableList;
/**
* Maximum capacity for a group of customer
*/
private static int maxCapacity = 10;
/**
* Creates a TableManager object that has an ArrayList
* of Table objects.
*/
public TableManager() {
ArrayList<Table> tableList = new ArrayList<Table>();
tableList.add(new Table(1, 2, Status.AVAILABLE));
tableList.add(new Table(2, 4, Status.AVAILABLE));
tableList.add(new Table(3, 6, Status.AVAILABLE));
tableList.add(new Table(4, 6, Status.AVAILABLE));
tableList.add(new Table(5, 8, Status.AVAILABLE));
tableList.add(new Table(6, 8, Status.AVAILABLE));
tableList.add(new Table(7, 10, Status.AVAILABLE));
tableList.add(new Table(8, 10, Status.AVAILABLE));
tableList.add(new Table(9, 10, Status.AVAILABLE));
this.tableList = tableList;
}
/**
* Creates a TableManager with an input Table ArrayList
* @param tableList This TableManager's tableList
*/
public TableManager(ArrayList<Table> tableList) {
this.tableList = tableList;
}
/**
* Finds the tables that have Status.AVAILABLE and add
* them into a Table ArrayList to be returned.
* @return The Table ArrayList of available tables.
*/
public ArrayList<Table> getAvailableTables() {
ArrayList<Table> toReturnList = new ArrayList<Table>();
//loop thru all tables to get available ones
for (int i=0; i<tableList.size(); i++) {
if (tableList.get(i).getStatus() == Status.AVAILABLE) {
toReturnList.add(tableList.get(i));
}
}
return toReturnList;
}
/**
* Finds the tables that have Status.OCCUPIED and add
* them into a Table ArrayList to be returned.
* @return The Table ArrayList of occupied tables.
*/
public ArrayList<Table> getOccupiedTables() {
ArrayList<Table> toReturnList = new ArrayList<Table>();
//loop thru all tables to get available ones
for (int i=0; i<tableList.size(); i++) {
if (tableList.get(i).getStatus() == Status.OCCUPIED) {
toReturnList.add(tableList.get(i));
}
}
return toReturnList;
}
/**
* Finds the tables that have Status.RESERVED and add
* them into a Table ArrayList to be returned.
* @return The Table ArrayList of reserved tables.
*/
public ArrayList<Table> getReservedTables() {
ArrayList<Table> toReturnList = new ArrayList<Table>();
//loop thru all tables to get available ones
for (int i=0; i<tableList.size(); i++) {
if (tableList.get(i).getStatus() == Status.RESERVED) {
toReturnList.add(tableList.get(i));
}
}
return toReturnList;
}
/**
* Gets the user input for the number of seats required
* when they come to the restaurant.
* @return The minimum number of seats to fulfil
* the number of pax.
*/
public int getUserInput(){
System.out.println("How many seats required?");
Scanner sc = new Scanner(System.in);
int minSeats = sc.nextInt();
while(minSeats > 10){
System.out.println("Sorry at most 10 seats only.");
minSeats = sc.nextInt();
}
return minSeats;
}
/**
* Find an available Table that matches the minimum number of seats
* required by a customer.
* @param minSeats The minimum number of seats required.
* @return The tableID of the table found.
*/
public int findSuitableTableFromAvailable(int minSeats) {
ArrayList<Table> availableTables = getAvailableTables();
int chosenTableNo = -1; //set this to be updated, and returned
if (minSeats > maxCapacity) {
System.out.println("No suitable Table found, max Capacity per table is only 10!");
return -1;
}
for (int i = 0; i < availableTables.size(); i++) {
if (availableTables.get(i).getSeatingCapacity() >= minSeats) {
if (chosenTableNo == -1) {//guard against chosen == -1
chosenTableNo = i;
}
else if (availableTables.get(i).getSeatingCapacity() < availableTables.get(chosenTableNo).getSeatingCapacity()){
chosenTableNo = i; //it is more than seats required and less than prev chosen tables
}
}
}
if (chosenTableNo == -1) {
System.out.println("No suitable Table for " + minSeats);
return chosenTableNo;
}
System.out.println("Table " + availableTables.get(chosenTableNo).getId() + " found " + "with " + availableTables.get(chosenTableNo).getSeatingCapacity() + " seats");
return availableTables.get(chosenTableNo).getId(); //returns tableNo or -1 when no tables are found
}
/**
* Find a reserved Table that matches the minimum
* number of seats stated.
* @param minSeats The minimum number of seats.
* @return The tableID of the table found.
*/
public int findSuitableTableFromReserved(int minSeats) {
ArrayList<Table> reservedTables = getReservedTables();
int chosenTableNo = -1; //set this to be updated, and returned
if (minSeats > maxCapacity) {
System.out.println("No suitable Table found, max Capacity per table is only 10!");
return -1;
}
for (int i = 0; i < reservedTables.size(); i++) {
int seatingCapacity = reservedTables.get(i).getSeatingCapacity();
if (seatingCapacity >= minSeats) {
if (chosenTableNo == -1) {//guard against chosen == -1
chosenTableNo = i;
}
else {
int chosenTableSeatingCapacity = reservedTables.get(chosenTableNo).getSeatingCapacity();
if (seatingCapacity < chosenTableSeatingCapacity) {
chosenTableNo = i;
}
}
}
}
if (chosenTableNo == -1) {
System.out.println("No suitable Table for " + minSeats);
return chosenTableNo;
}
else {
System.out.println("Table " + reservedTables.get(chosenTableNo).getId() + " found " + "with " + reservedTables.get(chosenTableNo).getSeatingCapacity() + " seats");
int tableID = reservedTables.get(chosenTableNo).getId(); //returns tableNo or -1 when no tables are found
return tableID;
}
}
/**
* Find an occupied Table that matches the minimum
* number of seats stated.
* @param minSeats The minimum number of seats.
* @return The tableID of the table found.
*/
public int findSuitableTableFromOccupied(int minSeats) {
ArrayList<Table> occupiedTables = getOccupiedTables();
int chosenTableNo = -1; //set this to be updated, and returned
if (minSeats > maxCapacity) {
System.out.println("No suitable Table found, max Capacity per table is only 10!");
return -1;
}
for (int i = 0; i < occupiedTables.size(); i++) {
if (occupiedTables.get(i).getSeatingCapacity() >= minSeats) {
if (chosenTableNo == -1) {//guard against chosen == -1
chosenTableNo = i;
}
else if (occupiedTables.get(i).getSeatingCapacity() < occupiedTables.get(chosenTableNo).getSeatingCapacity()){
chosenTableNo = i; //it is more than seats required and less than prev chosen tables
}
}
}
if (chosenTableNo == -1) {
System.out.println("No suitable Table for " + minSeats);
return chosenTableNo;
}
System.out.println("Table " + occupiedTables.get(chosenTableNo).getId() + " found " + "with " + occupiedTables.get(chosenTableNo).getSeatingCapacity() + " seats");
return occupiedTables.get(chosenTableNo).getId(); //returns tableNo or -1 when no tables are found
}
/**
* Set the Table's status to available.
* @param tableID The ID of the table to be changed to available.
*/
public void setTableToAvailable(int tableID) {
Table table = tableList.get(tableID-1);
table.setStatus(Status.AVAILABLE);
System.out.println("Table "+ tableID + " set to Available");
}
/**
* Set the Table's status to occupied.
* @param tableID The ID of the table to be changed to occupied.
*/
public void setTableToOccupied(int tableID) {
tableList.get(tableID-1).setStatus(Status.OCCUPIED);
System.out.println("Table "+ tableID + " set to Occupied");
}
/**
* Set the Table's status to reserved.
* @param tableID The ID of the table to be changed to reserved.
*/
public void setTableToReserved(int tableID) {
tableList.get(tableID-1).setStatus(Status.RESERVED);
System.out.println("Table "+ tableID + " set to Reserved");
}
/**
* Prints the Tables in the tableList.
* @param tableList The tableList to be printed.
*/
public void printTables(ArrayList<Table> tableList){
if (tableList.isEmpty()){
System.out.println("No Tables found\n");
return;
}
else{
System.out.println("\nHere is the list of " + tableList.get(0).getStatus() + " tables : ");
for (int i=0; i<tableList.size(); i++){
System.out.println("Table " + tableList.get(i).getId() + " seats = " + tableList.get(i).getSeatingCapacity());
}
}
System.out.println("\n");
}
}