-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackRestaurant.java
More file actions
126 lines (115 loc) · 2.6 KB
/
StackRestaurant.java
File metadata and controls
126 lines (115 loc) · 2.6 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
import java.util.Arrays;
/**
* Lab 7
*
* Implementation of the Restaurant abstract class. Stores and completes tickets based on a stack data structure.
* Tickets are treated in a LIFO manner. That is, the last ticket (most recent) to be added to the restaurant is
* the first ticket to be completed. This is in contrast to the QueueRestaurant which operates how a restaurant
* normally would.
*
* @modified by Em Evans
* @version 2019-30-19 1
*/
public class StackRestaurant extends Restaurant {
private Order[] orderList;
private int topOfStack;
private int size = 10;
/**
* Create the stack restaurant. Initializes the Order storage variables.
*/
public StackRestaurant()
{
this.orderList = new Order[getSize()];
this.topOfStack = 0;
}
/**
* Resize the array when more space is needed.
*/
private void resize()
{
this.setSize(getSize() * 2);
Order[] temp = new Order[this.getSize()];
for (int i = 0; i < orderList.length; ++i)
{
temp[i] = orderList[i];
}
this.orderList = temp;
}
/**
* Add an order to the restaurant.
*
* @param order The order to be added.
* @return True. Because the StackRestaurant should resize if it runs out of room to store tickets, a ticket
* should always be added, and this method should always return true.
*/
/**
@Override
*/
public boolean addOrder(Order order)
{
//TODO:implement this
//resize if necessary and then order to stack
if(orderList.length >= getSize())
{
resize();
}
orderList[topOfStack] = order;
this.topOfStack++;
return true;
}
/**
* [Internal Code - This is not required by the specification but can be a useful construct.]
*
@Override
*/
protected Order completeOrder()
{
//TODO: implement this
//if no orders return -1
//otherwise update topOfStack and return order at topOfStack
if(topOfStack == 0)
{
return orderList[-1];
}
else
{
this.topOfStack--;
return orderList[topOfStack];
}
}
/**
* Computes the number of orders in the restaurant that have not been completed.
*
* @return the number of orders to complete.
*
@Override
*/
public int numberRemainingOrder()
{
int counter = 0;
for(int i = this.topOfStack; i >= 0; --i)
{
counter++;
}
return counter;
}
/**
* TODO
*
@Override
*/
protected Order checkNextCompletedOrder()
{
//TODO: return order the next order in line
this.topOfStack--;
return orderList[topOfStack];
}
public int getSize()
{
return this.size;
}
public void setSize(int sIZE)
{
this.size = sIZE;
}
}