Java代写:CSE214 Restaurant's Menu

Introduction

基础Java作业代写,实现一个类似ArrayList/Vector的ADT,然后实现一个餐馆菜单应用即可。

Requirement

In this assignment, 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

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

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

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

public int size()
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

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 adding a player to the end of the Menu.

removeItem

public void removeItem(int position)
Parameters:
position - the position in the Menu where the MenuItem will be removed from. 
Preconditions:
This Menu object has been instantiated and 1 <= position <= items\_currently\_in\_list. 
Postcondition:
The MenuItem at the desired position in the Menu has been removed. All MenuItems that were originally in positions greater than or equal to position are moved forward one position. (Ex: If there are 5 items in an Menu, positions 1-5, and you remove the item at position 4, the item that was at position 5 will be moved to position 4). 
Throws:
IllegalArgumentException
Indicates that position is not within the valid range.
Note:
position refers to the position in the Menu and not the position inside the array. 

getItem

public MenuItem getItem(int position)
Get the MenuItem at the given position in this Menu object.
Parameters:
position - position of the MenuItem to retrieve 
Preconditions:
This Menu object has been instantiated and 1 <= position <= items\_currently\_in\_list.
Returns:
The MenuItem at the specified position in this Menu object.
Throws:
IllegalArgumentException
Indicates that position is not within the valid range.
Note:
position refers to the position in the Menu and not the position inside the array. 

getItemByName

public MenuItem getItemByName(String name)
Return the MenuItem with the given name
Parameters:
name - name of the item to retrieve 
Preconditions:
This Menu object has been instantiated
Returns:
The MenuItem with the specified name
Throws:
IllegalArgumentException
Indicates that the given item does not exist in this Menu.

printAllItems

public void printAllItems()
Prints a neatly formatted table of each item in the Menu on its own line with its position number as shown in the sample output.
Preconditions:
This Menu object has been instantiated.
Postcondition:
A neatly formatted table of each MenuItem in the Menu on its own line with its position number has been displayed to the user.
Note:
position refers to the position in the Menu and not the position inside the array. 
Hint:
If your toString() method is implemented correctly as described below, you will simply need to call it and print the results to the user. 

toString

public String toString()
Gets the String representation of this Menu object, which is a neatly formatted table of each MenuItem in the Menu on its own line with its position number as shown in the sample output.
Returns:
The String representation of this Menu object. 

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.

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.

  • INPUT FORMAT:
  • Each menu operation is entered on its own line and should be case insensitive (i.e. ‘q’ and ‘Q’ are the same).
  • Check to make sure that the position, if required, is valid. If not, print an error message and return to the menu.
  • For the Add Item command, if the input information is valid, construct the object accordingly. Otherwise, print an error message and return to the menu.
    You may assume that the lengths of the input for the item names are at most 25 characters long, and that the descriptions are at most 75 characters long.
  • OUTPUT FORMAT:
  • Echo the input information for the Add Item command in the output.
  • All menu operations must be accompanied by a message indicating what operation was performed and whether or not it was successful.
  • All lists must be printed in a nice and tabular form as shown in the sample output. You may use C style formatting as shown in the following example. The example below shows two different ways of displaying the name and address at pre-specified positions 21, 26, 19, and 6 spaces wide. If the ‘-‘ flag is given, then it will be left-justified (padding will be on the right), else the region is right-justified. The ‘s’ identifier is for strings, the ‘d’ identifier is for integers . Giving the additional ‘0’ flag pads an integer with additional zeroes in front.
1
2
3
4
5
6
7
String name = "Doe Jane";
String address = "32 Bayview Dr.";
String city = "Fishers Island, NY";
int zip = 6390;

System.out.println(String.format("%-21s%-26s%19s%06d", name, address, city, zip));
System.out.printf("%-21s%-26s%19s%06d", name, address, city, zip);
Doe Jane             32 Bayview Dr.             Fishers Island, NY 06390
Doe Jane             32 Bayview Dr.             Fishers Island, NY 06390

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.

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