OOD-lintcode-709-Restaurant OO Design

描述

设计一个餐馆:

  • 不考虑预订座位

  • 不考虑订外卖

  • 餐馆的桌子有不同大小

  • 餐馆会优先选择适合当前Party最小的空桌

  • 请实现Restaurant Class, hints: findTable(), takeOrder(), checkOut().

思路

感觉OOD主要就是脑洞啊…尽量往实际场景中靠拢.

需要考虑实现的对象:

  • Meal
    -price (lunch or dinner)
    -available_time
  • Order
    • List<Meal> meals
    • mergeOrder() (客人要求加菜)
    • getMeals()
    • getBill()
  • Table
    • size(capacity)
    • available
    • getOrder (一张桌子对应一个订单)
    • setOrder
  • Party
    • size

实现的方法:

  • findTable() 返回最小的available table
  • checkOut(Table t)
  • takeOrder(Table t, Order o)

代码

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
class NoTableException extends Exception{

public NoTableException(Party p)
{
super("No table available for party size: " + p.getSize());
}
}

class Meal {
private float price;

public Meal(float price)
{
this.price = price;
}

public float getPrice()
{
return this.price;
}
}

class Order {
private List<Meal> meals;

public Order()
{
meals = new ArrayList<Meal>();
}

public List<Meal> getMeals()
{
return meals;
}

public void mergeOrder(Order order)
{
if(order != null)
{
for(Meal meal : order.getMeals())
{
meals.add(meal);
}
}
}

public float getBill()
{
int bill = 0;
for(Meal meal : meals)
{
bill += meal.getPrice();
}
return bill;
}
}

class Party {
private int size;

public Party(int size)
{
this.size = size;
}

public int getSize()
{
return this.size;
}
}

class Table implements Comparable<Table>{
private int capacity;
private boolean available;
private Order order;

public Table(int capacity)
{
this.capacity = capacity;
available = true;
order = null;
}

public int getCapacity()
{
return this.capacity;
}

public boolean isAvailable()
{
return this.available;
}

public void markAvailable()
{
this.available = true;
}

public void markUnavailable()
{
this.available = false;
}

public Order getCurrentOrder()
{
return this.order;
}

public void setOrder(Order o)
{
if(order == null)
{
this.order = o;
}
else
{
if(o != null)
{
this.order.mergeOrder(o);
} else {
this.order = o;
}
}
}

@Override
public int compareTo(Table compareTable) {
// TODO Auto-generated method stub
return this.capacity - compareTable.getCapacity();
}
}

public class Restaurant {
private List<Table> tables;
private List<Meal> menu;

public Restaurant()
{
// Write your code here
tables = new ArrayList<>();
menu = new ArrayList<>();
}

public void findTable(Party p) throws NoTableException
{
// Write your code here

for (Table t : tables) {
if (t.isAvailable() && t.getCapacity() >= p.getSize()) {
t.markUnavailable();
return;
}
}
throw new NoTableException(p);
}

public void takeOrder(Table t, Order o)
{
// Write your code here
t.setOrder(o);
}

public float checkOut(Table t)
{
// Write your code here
t.markAvailable();
float amount = 0;
if (t.getCurrentOrder() != null) {
amount = t.getCurrentOrder().getBill();
t.setOrder(null);
}
return amount;
}

public List<Meal> getMenu()
{
return menu;
}

public void addTable(Table t)
{
// Write your code here
tables.add(t);
Collections.sort(tables);
}

public String restaurantDescription()
{
// Keep them, don't modify.
String description = "";
for(int i = 0; i < tables.size(); i++)
{
Table table = tables.get(i);
description += ("Table: " + i + ", table size: " + table.getCapacity() + ", isAvailable: " + table.isAvailable() + ".");
if(table.getCurrentOrder() == null)
description += " No current order for this table";
else
description += " Order price: " + table.getCurrentOrder().getBill();

description += ".\n";
}
description += "*****************************************\n";
return description;
}
}