COMP2396
Loading...
Searching...
No Matches
Restaurant.java
Go to the documentation of this file.
1package tutorial4.part3.q4;
2
3import java.util.ArrayList;
4
5class Table {
6 private final String name;
7 private final int capacity;
8 private int reserved = 0;
9
10 public Table(String name, int capacity) {
11 this.name = name;
12 this.capacity = capacity;
13 }
14
15 public String getName() {
16 return name;
17 }
18
19 public int getCapacity() {
20 return capacity;
21 }
22
23 public int getReserved() {
24 return reserved;
25 }
26
27 public int getAvailable() {
28 return getCapacity() - getReserved();
29 }
30
31 public boolean make_reservation(int n) {
32 if (reserved + n <= capacity) {
33 reserved += n;
34 return true;
35 } else {
36 return false;
37 }
38 }
39}
40
41public class Restaurant {
42 private final String name;
43 private final ArrayList<Table> tables = new ArrayList<>();
44
45 public Restaurant(String name) {
46 this.name = name;
47 }
48
49 public String getName() {
50 return name;
51 }
52
53 public void add_table(String name, int capacity) {
54 tables.add(new Table(name, capacity));
55 }
56
57 private String _make_reservation_formatter(Table table, int n) {
58 return String.format("Table %s for %d guest(s).", table.getName(), n);
59 }
60 public String make_reservation(int n) {
61 for (Table table : tables) {
62 // if table fits all people perfectly
63 if (table.getCapacity() == n && table.make_reservation(n)) {
64 return _make_reservation_formatter(table, n);
65 }
66 }
67 // Finds table that don't have anyone
68 for (Table table : tables) {
69 if (table.getReserved() == 0 && table.make_reservation(n)) {
70 return _make_reservation_formatter(table, n);
71 }
72 }
73 // Finds table that does not fit all people perfectly
74 for (Table table : tables) {
75 if (table.getAvailable() >= n && table.make_reservation(n)) {
76 return _make_reservation_formatter(table, n);
77 }
78 }
79 return String.format("No table available for %s guest(s).", n);
80 }
81}
void add_table(String name, int capacity)
String _make_reservation_formatter(Table table, int n)
final ArrayList< Table > tables