Java代写:CSE214-Restaurants-Menu

Requirement

In this request, you will write a system to keep track of a restaurant’s menu and the orders placed at the restaurant. A menu consists of up to 50 items, each of which has a name, description, and price. You will implement a class called Menu to store the menu. A driver class will be used to interact with a Menu (add/remove items, change name/description/price, etc.) and build up an “order” of items from the menu (like a shopping cart).

Write a fully-documented class named MenuItem which contains the item’s name (String), description (String), and price (double). You should provide accessor and mutator methods for each variable, as well as a constructor for the class. The mutator method for the price variable should throw an exception if the new price is nonpositive.

Write a fully-documented class named Menu which stores a list of items in an array and provides an interface to interact with this list. Note that although arrays in Java are indexed starting with 0, the items in a Menu will be indexed starting with 1. A Menu can hold up to 50 items at a time, so use the final variable MAX_ITEMS = 50. The class will be based on the following ADT specification:

public class Menu

The Menu class implements an abstract data type for a list of menu items supporting some common operations on such lists.
Constructor for Menu

1
2
3
4
public Menu()
Construct an instance of the Menu class with no MenuItem objects in it.
Postcondition:
This Menu has been initialized to an empty list of MenuItems.

clone

1
2
3
4
public Object clone()
Generate a copy of this Menu.
Returns:
The return value is a copy of this Menu. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be typecast to an Menu before it can be used.

equals

1
2
3
4
5
6
7
8
9
10
public boolean equals (Object obj)
Compare this Menu to another object for equality.
Parameters:
obj - an object to which this Menu is compared
Returns:
A return value of true indicates that obj refers to a Menu object with the same MenuItems in the same order as this Menu. Otherwise, the return value is false.
Note:
If obj is null or it is not a Menu object, then the return value is false.
Note:
When comparing equality between two MenuItem objects, you must verify that their names, descriptions, and prices are all the same. Using the == operator will simply check to see if the two variables refer to the same Menu object, which does not take into consideration that two different Menu objects can actually represent the same item. To solve this problem, you can either check that each of the properties of the two objects are the same (name, description, price) inside of this method, or you may simplify this process by implementing an equals method (similar to this one) for the MenuItem class.

size

int size()
1
2
3
4
5
Determines the number of items currently in this Menu.
Preconditions:
This Menu object has been instantiated.
Returns:
The number of MenuItems in this Menu.

addItem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void addItem(MenuItem item, int position)
Parameters:
item - the new MenuItem object to add to this Menu
position - the position in the Menu where item will be inserted
Preconditions:
This Menu object has been instantiated and 1 <= position <= items_currently\_in\_list + 1. The number of MenuItem objects in this Menu is less than MAX\_ITEMS.
Postcondition:
The new MenuItem is now stored at the desired position in the Menu. All MenuItems that were originally in positions greater than or equal to position are moved back one position. (Ex: If there are 5 MenuItems in an Menu, positions 1-5, and you insert a new item at position 4, the new ite will now be at position 4, the item that was at position 4 will be moved to position 5, and the item that was at position 5 will be moved to position 6).
Throws:
IllegalArgumentException
Indicates that position is not within the valid range.
FullListException
Indicates that there is no more room inside of the Menu to store the new MenuItem object.
Note 1:
position refers to the position in the Menu and not the position inside the array.
Note 2:
Inserting an item to position (items_currently_in_list + 1) is effectively the same as a

Write a fully documented class named MenuOperations that is based on the following specification:
public class MenuOperations
The MenuOperations Java application tests data structure classes designed above and the operations defined on them.

1
public static void main(String[] args)

The main method runs a menu driven application which first creates an empty Menu, and then prompts the user for a command selecting the operation. Once an operation is selected, the program prompts for any additional information required to perform the operation, and then actually performs the operation. The operations and additional information required are listed below. To store the items in the current order, you can either use an array or a separate instance of the Menu class.

1
2
3
4
5
6
7
8
9
10
11
Add Item:               A  <Name> <Description> <Price> <Position> (add the item to the menu)
Get Item: G <Position> (Print out the name, description, and price of the item at the specified position in the menu)
Remove Item: R <Name> (Remove the item with the given name in the menu)
Print All Items: P (print the list of all items on the menu)
Size: S (print the number of items on the menu)
Update description: D <Name> <New description> (update the description of the named item)
Update price: C <Name> <New price> (update the price of the named item)
Add to order: O <Position> (Add the item at the specified position in the menu to the order)
Remove from order: I <Position> (Remove the item at the specified position in the order)
View order: V (print the items in the current order)
Quit: Q (terminate the program gracefully)

You will also need classes to handle the exceptions thrown (see class specifications above for the exception classes you need).

Note: You may include additional methods in any class as necessary or as you find convenient.

HINTS:

Remember that the position parameter to all of the methods listed in the Menu class refers to the player at a given item within a Menu (starting at position 1) and not the position inside of the array (which starts at position 0). There are two ways that you can handle this issue:

  • Store item 1 in array position 0, item 2 in array position 1, and so on and so forth. Inside each method, subtract one from the position given by the parameter to find the appropriate position within the array.
  • Define your array such that it is of size MAX_ITEMS + 1 instead of MAX_ITEMS. Store item 1 in array position 1, item 2 in array position 2, and so on and so forth. Position 0 of the array will not be used.

SAMPLE INPUT/OUTPUT

Output shown in black. User input shown in red. Comments shown in green.

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
Main menu:

A) Add Item
G) Get Item
R) Remove Item
P) Print All Items
S) Size
D) Update description
C) Update price
O) Add to order
I) Remove from order
V) View order
Q) Quit

Select an operation: A

Enter the name: Chicken Parmesan

Enter the description: Breaded chicken, tomato sauce, and cheese

Enter the price: 9.50

Enter the position: 1

Added "Chicken Parmesan: Breaded chicken, tomato sauce, and cheese" for $9.50 at position 1

// Menu not shown in sample input/output

Select an operation: A

Enter the name: Hot Dog

Enter the description: Beef sausage in a bun with ketchup

Enter the price: 4.50

Enter the position: 1

Added "Hot Dog: Beef sausage in a bun with ketchup" for $4.50 at position 1

// Menu not shown in sample input/output

Select an operation: P

MENU:

# Name Description Price
---------------------------------------------------------------------------------
1 Hot Dog Beef sausage in a bun with ketchup $4.50
2 Chicken Parmesan Breaded chicken, tomato sauce, and cheese $9.50

// Menu not shown in sample input/output

Select an operation: C

Enter the name of the item: Chicken Parmesan

Enter the new price: 8.50

Changed the price of "Chicken Parmesan" to $8.50

// Menu not shown in sample input/output

Select an operation: P

MENU:

# Name Description Price
---------------------------------------------------------------------------------
1 Hot Dog Beef sausage in a bun with ketchup $4.50
2 Chicken Parmesan Breaded chicken, tomato sauce, and cheese $8.50

// Menu not shown in sample input/output

Select an operation: S

There are 2 items in the menu

// Menu not shown in sample input/output

Select an operation: O

Enter position of item to add to order: 2

Added "Chicken Parmesan" to order

// Menu not shown in sample input/output

Select an operation: V

ORDER:

# Name Description Price
---------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese $8.50

// Menu not shown in sample input/output

Select an operation: G

Enter the position: 2

# Name Description Price
---------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese $8.50

// Menu not shown in sample input/output

Select an operation: R

Enter the Name: Chicken Parmesan

Removed "Chicken Parmesan"

// Menu not shown in sample input/output

Select an operation: D

Enter the position: 1

Enter the new description: Beef sausage in a bun with ketchup and mustard

New description set.

// Menu not shown in sample input/output

Select an operation: P

MENU:

# Name Description Price
---------------------------------------------------------------------------------
1 Hot Dog Beef sausage in a bun with ketchup and mustard $4.50

// Menu not shown in sample input/output

Select an operation: I

Enter the position: 1

Removed "Chicken Parmesan" from order.

// Menu not shown in sample input/output

Select an operation: V

ORDER:

# Name Description Price
---------------------------------------------------------------------------------

// Examples of errors:

// Menu not shown in sample input/output

Select an operation: L

No such operation

// Menu not shown in sample input/output

Select an operation: R

Enter the position: 3

No item in position 3