public class FullListException extends Exception {
/**
* Default constructor
*/
public FullListException() {
super();
}
/**
* param message
*/
public FullListException(String message) {
super(message);
}
}
public class NonPositivePriceException extends Exception{
/**
* Default constructor
*/
public NonPositivePriceException() {
super();
}
/**
* param message
*/
public NonPositivePriceException(String message) {
super(message);
}
}
public class MenuItem {
//Attributes
private String name;
private String description;
private double price;
/**
* Default constructor
*/
public MenuItem() {
this.name = \"\";
this.description = \"\";
this.price = 0.0;
}
/**
* Parameterized Constructor
* param name
* param description
* param price
*/
public MenuItem(String name, String description, double price) {
this.name = name;
this.description = description;
this.price = price;
}
/**
* return the name
*/
public String getName() {
return name;
}
/**
* param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* return the description
*/
public String getDescription() {
return description;
}
/**
* param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* return the price
*/
public double getPrice() {
return price;
}
/**
* param price the price to set
* throws NonPositivePriceException
*/
public void setPrice(double price) throws NonPositivePriceException {
if(price <= 0)
throw new NonPositivePriceException(\"Price cannot be set to a non positive value\");
this.price = price;
}
/**
* Checks whether this MenuItem has the same attribute values as obj
*/
Override
public boolean equals(Object obj) {
MenuItem item = (MenuItem)obj;
if((this.name.equalsIgnoreCase(item.name)) &&
(this.description.equalsIgnoreCase(item.description)) &&
(this.price == item.price))
return true;
else
return false;
}
Override
public String toString() {
System.out.printf(\"\ %2s %-25s %-75s %-10s\", \"#\", \"Item Name\", \"Item Description\",
\"Item Price\");
System.out.printf(\"\ %50s\", new String(new char[115]).replace(\'\\0\', \'-\'));
System.out.printf(\"\ %2s %-25s %-75s %10.2f\", \"1\", this.name, this.description, this.price);
return \"\";
}
}
public class Menu{
static final int MAX_ITEMS = 50;
//Attributes
private MenuItem[] list;
/**
* Default Constructor
* 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.
*/
public Menu() {
list = new MenuItem[MAX_ITEMS];
}
/**
* Generates a copy of this Menu.
* return 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 is typecast to an Menu before it can be used.
*/
Override
public Object clone() {
Menu clone = new Menu();
if(this.size() > 0) {
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null)
clone.list[i] = this.list[i];
}
}
return cl.
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
public class FullListException extends Exception { Default .pdf
1. public class FullListException extends Exception {
/**
* Default constructor
*/
public FullListException() {
super();
}
/**
* param message
*/
public FullListException(String message) {
super(message);
}
}
public class NonPositivePriceException extends Exception{
/**
* Default constructor
*/
public NonPositivePriceException() {
super();
}
/**
* param message
*/
public NonPositivePriceException(String message) {
super(message);
}
}
public class MenuItem {
//Attributes
private String name;
private String description;
private double price;
/**
2. * Default constructor
*/
public MenuItem() {
this.name = "";
this.description = "";
this.price = 0.0;
}
/**
* Parameterized Constructor
* param name
* param description
* param price
*/
public MenuItem(String name, String description, double price) {
this.name = name;
this.description = description;
this.price = price;
}
/**
* return the name
*/
public String getName() {
return name;
}
/**
* param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* return the description
*/
public String getDescription() {
return description;
}
3. /**
* param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* return the price
*/
public double getPrice() {
return price;
}
/**
* param price the price to set
* throws NonPositivePriceException
*/
public void setPrice(double price) throws NonPositivePriceException {
if(price <= 0)
throw new NonPositivePriceException("Price cannot be set to a non positive value");
this.price = price;
}
/**
* Checks whether this MenuItem has the same attribute values as obj
*/
Override
public boolean equals(Object obj) {
MenuItem item = (MenuItem)obj;
if((this.name.equalsIgnoreCase(item.name)) &&
(this.description.equalsIgnoreCase(item.description)) &&
(this.price == item.price))
return true;
else
return false;
}
Override
4. public String toString() {
System.out.printf(" %2s %-25s %-75s %-10s", "#", "Item Name", "Item Description",
"Item Price");
System.out.printf(" %50s", new String(new char[115]).replace('0', '-'));
System.out.printf(" %2s %-25s %-75s %10.2f", "1", this.name, this.description, this.price);
return "";
}
}
public class Menu{
static final int MAX_ITEMS = 50;
//Attributes
private MenuItem[] list;
/**
* Default Constructor
* 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.
*/
public Menu() {
list = new MenuItem[MAX_ITEMS];
}
/**
* Generates a copy of this Menu.
* return 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 is typecast to an Menu before it can be used.
*/
Override
public Object clone() {
Menu clone = new Menu();
if(this.size() > 0) {
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null)
5. clone.list[i] = this.list[i];
}
}
return clone;
}
/**
* Compare this Menu to another object for equality
* return 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. If obj is null or it is not a Menu object, then the return
value is false.
*/
Override
public boolean equals(Object obj) {
if((obj == null) || !(obj instanceof Menu)) {
return false;
} else {
Menu menu = (Menu) obj;
if(this.size() != menu.size())
return false;
else {
boolean equal = true;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(!this.list[i].equals(menu.list[i])) {
equal = false;
break;
}
}
return equal;
}
}
6. }
/**
* Returns the number of MenuItems in this Menu
* Preconditions: This Menu object has been instantiated.
* return : The number of MenuItems in this Menu
*/
public int size() {
int count = 0;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null)
count += 1;
}
return count;
}
/**
* Adds an item at a position
* 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 item 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).
*
* param item : the new MenuItem object to add to this Menu
* param position : the position in the Menu where item will be inserted
*
7. * 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.
*/
public void addItem(MenuItem item, int position) throws FullListException,
IllegalArgumentException{
if(this.list != null) {
if(this.size() == MAX_ITEMS) {
throw new FullListException("No more room inside of the Menu to store the new
MenuItem");
} else if((0 < position) && (position < (MAX_ITEMS + 1))) {
//Shift menuitems one position forward
for(int i = MAX_ITEMS - 1 ; i >= position ; i--) {
this.list[i] = this.list[i - 1];
}
//Add new item at position
this.list[position - 1] = item;
} else {
throw new IllegalArgumentException("Position not within valid range");
}
}
}
/**
* Removes the first menuitem that matches the name
* param name - the name of the Menu to be removed
* throws IllegalArgumentException
*/
public void removeItem(String name) throws IllegalArgumentException {
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(!found) {
if(this.list[i].getName().equalsIgnoreCase(name))
found = true;
8. } else {
//Shift menuitems one position backwards
this.list[i - 1] = this.list[i];
}
}
this.list[MAX_ITEMS - 1] = null;
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
/**
* 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).
*
* param position - the position in the Menu where the MenuItem will be removed from.
*
* throws: IllegalArgumentException Indicates that position is not within the valid range.
*/
public void removeItem(int position) throws IllegalArgumentException {
if(this.list != null) {
if((0 < position) && (position < (MAX_ITEMS + 1))) {
//Shift menuitems one position backwards
for(int i = (position - 1) ; i < (MAX_ITEMS - 1) ; i++) {
this.list[i] = this.list[i + 1];
}
this.list[MAX_ITEMS - 1] = null;
} else
throw new IllegalArgumentException("Position is not within the valid range");
9. }
}
/**
* Returns MenuItem at the specified position in this Menu object.
* Precondition : This Menu object has been instantiated and 1 < position <
items_currently_in_list
* param position - position of the MenuItem to retrieve
* return
* throws: IllegalArgumentException Indicates that position is not within the valid range.
*/
public MenuItem getItem(int position) throws IllegalArgumentException{
if(this.list != null) {
if((0 < position) && (position < (MAX_ITEMS + 1))) {
return this.list[position - 1];
} else
throw new IllegalArgumentException("Position is not within the valid range");
}
return null;
}
/**
* Return the MenuItem with the given name
* Preconditions:This Menu object has been instantiated
* param name - name of the item to retrieve
* return
* throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
*/
public MenuItem getItemByName(String name) throws IllegalArgumentException{
if(this.list != null) {
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i].getName().equalsIgnoreCase(name))
return this.list[i];
}
}
10. throw new IllegalArgumentException("Item does not exist in this Menu");
}
/**
* Updates the first MenuItem that matches with name with the given description
* Preconditions:This Menu object has been instantiated
* param name - name of the item
* param desc - new description of the menu with Name name
* throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
*/
public void updateDescription(String name, String newDesc) throws
IllegalArgumentException{
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(this.list[i].getName().equalsIgnoreCase(name)) {
found = true;
this.list[i].setDescription(newDesc);
break;
}
}
}
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
/**
* Updates the first MenuItem that matches with name with the given price
* Preconditions:This Menu object has been instantiated
* param name - name of the item
* param price - new price of the menu with Price price
11. * throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
*/
public void updatePrice(String name, double newPrice) throws NonPositivePriceException,
IllegalArgumentException{
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(this.list[i].getName().equalsIgnoreCase(name)) {
found = true;
this.list[i].setPrice(newPrice);
break;
}
}
}
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
/**
* 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.
*/
public void printAllItems() {
this.toString();
}
Override
public String toString() {
if(this.list != null) {
12. int count = 0;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(count == 0) {
System.out.printf(" %2s %-25s %-75s %-10s", "#", "Item Name", "Item Description",
"Item Price");
System.out.printf(" %50s", new String(new char[115]).replace('0', '-'));
}
count += 1;
System.out.printf(" %2s %-25s %-75s %10.2f", count, this.list[i].getName(),
this.list[i].getDescription(), this.list[i].getPrice());
}
}
}
return "";
}
}
import java.util.Scanner;
public class MenuOperations {
/**
* Clear keyboard buffer
*/
public static void clearBuf(Scanner scanner) {
scanner.nextLine();
}
/**
* Prints the menu
*/
public static void displayMenu() {
System.out.println(" Main menu:");
System.out.println(" A) Add Item " +
" G) Get Item " +
" R) Remove Item " +
" P) Print All Items " +
" S) Size " +
13. " D) Update description " +
" C) Update price " +
" O) Add to order " +
" I) Remove from order " +
" V) View order " +
" Q) Quit ");
System.out.println(" Select an operation : ");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Menu menu = new Menu(); //Holds the menu
Menu order = new Menu(); //Holds the order
int orderCount = 0; //Counts the number of items in the order
char choice = ' ';
while (true) {
displayMenu();
choice = Character.toUpperCase(scanner.next().charAt(0));
clearBuf(scanner);
switch (choice) {
case 'A': //Add the item to the menu
System.out.println(" Enter the name : ");
String name = scanner.nextLine();
if(name.length() > 25) {
System.out.println("Item name cannot be more than 25.");
} else {
System.out.println(" Enter the description : ");
String desc = scanner.nextLine();
if(desc.length() > 75) {
System.out.println("Item description cannot be more than 75.");
} else {
System.out.println(" Enter the price : ");
double price = scanner.nextDouble();
14. clearBuf(scanner);
System.out.println(" Enter the position : ");
int position = scanner.nextInt();
clearBuf(scanner);
MenuItem item = new MenuItem(name, desc, price);
try {
menu.addItem(item, position);
System.out.println("Added "" + name + ": " + desc + """ +
" for $" + price + " at position " + position);
} catch (FullListException fle) {
System.out.println(fle.getMessage());
}
}
}
break;
case 'G': //Print out the name, description, and price of the item at the specified position in the
menu
if(menu.size() > 0) {
System.out.println(" Enter position : ");
int position = scanner.nextInt();
clearBuf(scanner);
MenuItem item = menu.getItem(position);
if(item != null)
System.out.println(item);
else
System.out.println("No item found at the specified position");
} else
System.out.println("No items in the menu");
break;
case 'R': //Remove the item with the given name in the menu
if(menu.size() > 0) {
System.out.println(" Enter the Name: ");
15. name = scanner.nextLine();
try {
menu.removeItem(name);
System.out.println("Removed "" + name + """);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else
System.out.println("No item");
break;
case 'P': //Print the list of all items on the menu
menu.printAllItems();
break;
case 'S': //Print the number of items on the menu
System.out.println("There are " + menu.size() + " items in the menu");
break;
case 'D': //Update the description of the named item
if(menu.size() > 0) {
System.out.println(" Enter the name of the item: ");
name = scanner.nextLine();
System.out.println(" Enter the new description: ");
String desc = scanner.nextLine();
if(desc.length() > 75) {
System.out.println("Item description cannot be more than 75.");
} else {
try {
menu.updateDescription(name, desc);
System.out.println("New description set.");
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
}
16. } else
System.out.println("No items in the menu");
break;
case 'C': //Update the price of the named item
if(menu.size() > 0) {
System.out.println(" Enter the name of the item: ");
name = scanner.nextLine();
System.out.println(" Enter the new price: ");
double price = scanner.nextDouble();
try {
menu.updatePrice(name, price);
System.out.println("Changed the price of "" + name + "" to " + price);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
} catch(NonPositivePriceException nppe) {
System.out.println(nppe.getMessage());
}
} else
System.out.println("No items in the menu");
break;
case 'O': //Add the item at the specified position in the menu to the order
if(menu.size() > 0) {
System.out.println("Enter position of item to add to order: ");
int position = scanner.nextInt();
clearBuf(scanner);
try {
MenuItem item = menu.getItem(position);
if(item == null)
System.out.println("No menu at position : " + position);
else {
orderCount += 1;
17. order.addItem(item, orderCount);
System.out.println("Added "" + item.getName() + "" to order");
}
} catch (FullListException fle) {
System.out.println(fle.getMessage());
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else {
System.out.println("No menu available to add to order");
}
break;
case 'I': //Remove the item at the specified position in the order
if(order.size() > 0) {
System.out.println(" Enter the position : ");
int position = scanner.nextInt();
clearBuf(scanner);
try {
MenuItem item = order.getItem(position);
order.removeItem(position);
orderCount -= 1;
System.out.println("Removed "" + item.getName() + "" from order");
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else
System.out.println("No items in the order");
break;
case 'V': //Print item in current order
System.out.println("ORDER");
order.printAllItems();
break;
case 'Q':
18. // Close scanner
scanner.close();
System.exit(0);;
break;
default:
System.out.println("No such operation ");
}
}
}
}
SAMPLE OUTPUT
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 :
8.5
Enter the position :
1
Added "Chicken Parmesan: Breaded chicken, tomato sauce, and cheese" for $8.5 at position 1
19. 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 :
Hot dog
Enter the description :
Beef sausage in a bun with ketchup
Enter the price :
4.5
Enter the position :
1
Added "Hot dog: Beef sausage in a bun with ketchup" for $4.5 at position 1
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
20. Select an operation :
p
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Hot dog Beef sausage in a bun with ketchup 4.50
2 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
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 :
o
Enter position of item to add to order:
2
Added "Chicken Parmesan" to order
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
21. Q) Quit
Select an operation :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
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 :
o
Enter position of item to add to order:
1
Added "Hot dog" to order
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
22. V) View order
Q) Quit
Select an operation :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
2 Hot dog Beef sausage in a bun with ketchup 4.50
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 :
i
Enter the position :
1
Removed "Chicken Parmesan" from order
Main menu:
A) Add Item
G) Get Item
R) Remove Item
P) Print All Items
S) Size
D) Update description
C) Update price
23. O) Add to order
I) Remove from order
V) View order
Q) Quit
Select an operation :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Hot dog Beef sausage in a bun with ketchup 4.50
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 :
q
Solution
public class FullListException extends Exception {
/**
* Default constructor
*/
public FullListException() {
super();
}
/**
* param message
24. */
public FullListException(String message) {
super(message);
}
}
public class NonPositivePriceException extends Exception{
/**
* Default constructor
*/
public NonPositivePriceException() {
super();
}
/**
* param message
*/
public NonPositivePriceException(String message) {
super(message);
}
}
public class MenuItem {
//Attributes
private String name;
private String description;
private double price;
/**
* Default constructor
*/
public MenuItem() {
this.name = "";
this.description = "";
this.price = 0.0;
}
/**
* Parameterized Constructor
* param name
25. * param description
* param price
*/
public MenuItem(String name, String description, double price) {
this.name = name;
this.description = description;
this.price = price;
}
/**
* return the name
*/
public String getName() {
return name;
}
/**
* param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* return the description
*/
public String getDescription() {
return description;
}
/**
* param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* return the price
*/
public double getPrice() {
26. return price;
}
/**
* param price the price to set
* throws NonPositivePriceException
*/
public void setPrice(double price) throws NonPositivePriceException {
if(price <= 0)
throw new NonPositivePriceException("Price cannot be set to a non positive value");
this.price = price;
}
/**
* Checks whether this MenuItem has the same attribute values as obj
*/
Override
public boolean equals(Object obj) {
MenuItem item = (MenuItem)obj;
if((this.name.equalsIgnoreCase(item.name)) &&
(this.description.equalsIgnoreCase(item.description)) &&
(this.price == item.price))
return true;
else
return false;
}
Override
public String toString() {
System.out.printf(" %2s %-25s %-75s %-10s", "#", "Item Name", "Item Description",
"Item Price");
System.out.printf(" %50s", new String(new char[115]).replace('0', '-'));
System.out.printf(" %2s %-25s %-75s %10.2f", "1", this.name, this.description, this.price);
return "";
}
}
public class Menu{
static final int MAX_ITEMS = 50;
27. //Attributes
private MenuItem[] list;
/**
* Default Constructor
* 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.
*/
public Menu() {
list = new MenuItem[MAX_ITEMS];
}
/**
* Generates a copy of this Menu.
* return 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 is typecast to an Menu before it can be used.
*/
Override
public Object clone() {
Menu clone = new Menu();
if(this.size() > 0) {
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null)
clone.list[i] = this.list[i];
}
}
return clone;
}
/**
* Compare this Menu to another object for equality
* return A return value of true indicates that obj refers to a Menu object with the same
28. MenuItems in the same order as this Menu.
* Otherwise, the return value is false. If obj is null or it is not a Menu object, then the return
value is false.
*/
Override
public boolean equals(Object obj) {
if((obj == null) || !(obj instanceof Menu)) {
return false;
} else {
Menu menu = (Menu) obj;
if(this.size() != menu.size())
return false;
else {
boolean equal = true;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(!this.list[i].equals(menu.list[i])) {
equal = false;
break;
}
}
return equal;
}
}
}
/**
* Returns the number of MenuItems in this Menu
* Preconditions: This Menu object has been instantiated.
* return : The number of MenuItems in this Menu
*/
public int size() {
int count = 0;
29. for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null)
count += 1;
}
return count;
}
/**
* Adds an item at a position
* 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 item 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).
*
* param item : the new MenuItem object to add to this Menu
* param position : the position in the Menu where item will be inserted
*
* 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.
*/
public void addItem(MenuItem item, int position) throws FullListException,
IllegalArgumentException{
if(this.list != null) {
if(this.size() == MAX_ITEMS) {
throw new FullListException("No more room inside of the Menu to store the new
MenuItem");
30. } else if((0 < position) && (position < (MAX_ITEMS + 1))) {
//Shift menuitems one position forward
for(int i = MAX_ITEMS - 1 ; i >= position ; i--) {
this.list[i] = this.list[i - 1];
}
//Add new item at position
this.list[position - 1] = item;
} else {
throw new IllegalArgumentException("Position not within valid range");
}
}
}
/**
* Removes the first menuitem that matches the name
* param name - the name of the Menu to be removed
* throws IllegalArgumentException
*/
public void removeItem(String name) throws IllegalArgumentException {
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(!found) {
if(this.list[i].getName().equalsIgnoreCase(name))
found = true;
} else {
//Shift menuitems one position backwards
this.list[i - 1] = this.list[i];
}
}
this.list[MAX_ITEMS - 1] = null;
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
31. /**
* 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).
*
* param position - the position in the Menu where the MenuItem will be removed from.
*
* throws: IllegalArgumentException Indicates that position is not within the valid range.
*/
public void removeItem(int position) throws IllegalArgumentException {
if(this.list != null) {
if((0 < position) && (position < (MAX_ITEMS + 1))) {
//Shift menuitems one position backwards
for(int i = (position - 1) ; i < (MAX_ITEMS - 1) ; i++) {
this.list[i] = this.list[i + 1];
}
this.list[MAX_ITEMS - 1] = null;
} else
throw new IllegalArgumentException("Position is not within the valid range");
}
}
/**
* Returns MenuItem at the specified position in this Menu object.
* Precondition : This Menu object has been instantiated and 1 < position <
items_currently_in_list
* param position - position of the MenuItem to retrieve
* return
* throws: IllegalArgumentException Indicates that position is not within the valid range.
32. */
public MenuItem getItem(int position) throws IllegalArgumentException{
if(this.list != null) {
if((0 < position) && (position < (MAX_ITEMS + 1))) {
return this.list[position - 1];
} else
throw new IllegalArgumentException("Position is not within the valid range");
}
return null;
}
/**
* Return the MenuItem with the given name
* Preconditions:This Menu object has been instantiated
* param name - name of the item to retrieve
* return
* throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
*/
public MenuItem getItemByName(String name) throws IllegalArgumentException{
if(this.list != null) {
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i].getName().equalsIgnoreCase(name))
return this.list[i];
}
}
throw new IllegalArgumentException("Item does not exist in this Menu");
}
/**
* Updates the first MenuItem that matches with name with the given description
* Preconditions:This Menu object has been instantiated
* param name - name of the item
* param desc - new description of the menu with Name name
* throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
33. */
public void updateDescription(String name, String newDesc) throws
IllegalArgumentException{
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(this.list[i].getName().equalsIgnoreCase(name)) {
found = true;
this.list[i].setDescription(newDesc);
break;
}
}
}
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
/**
* Updates the first MenuItem that matches with name with the given price
* Preconditions:This Menu object has been instantiated
* param name - name of the item
* param price - new price of the menu with Price price
* throws - IllegalArgumentException - Indicates that the given item does not exist in this Menu
*/
public void updatePrice(String name, double newPrice) throws NonPositivePriceException,
IllegalArgumentException{
if(this.list != null) {
boolean found = false;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(this.list[i].getName().equalsIgnoreCase(name)) {
34. found = true;
this.list[i].setPrice(newPrice);
break;
}
}
}
if(!found)
throw new IllegalArgumentException("Item does not exist in this Menu");
}
}
/**
* 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.
*/
public void printAllItems() {
this.toString();
}
Override
public String toString() {
if(this.list != null) {
int count = 0;
for(int i = 0 ; i < MAX_ITEMS ; i++) {
if(this.list[i] != null) {
if(count == 0) {
System.out.printf(" %2s %-25s %-75s %-10s", "#", "Item Name", "Item Description",
"Item Price");
System.out.printf(" %50s", new String(new char[115]).replace('0', '-'));
}
count += 1;
System.out.printf(" %2s %-25s %-75s %10.2f", count, this.list[i].getName(),
35. this.list[i].getDescription(), this.list[i].getPrice());
}
}
}
return "";
}
}
import java.util.Scanner;
public class MenuOperations {
/**
* Clear keyboard buffer
*/
public static void clearBuf(Scanner scanner) {
scanner.nextLine();
}
/**
* Prints the menu
*/
public static void displayMenu() {
System.out.println(" Main menu:");
System.out.println(" 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 ");
System.out.println(" Select an operation : ");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
36. Menu menu = new Menu(); //Holds the menu
Menu order = new Menu(); //Holds the order
int orderCount = 0; //Counts the number of items in the order
char choice = ' ';
while (true) {
displayMenu();
choice = Character.toUpperCase(scanner.next().charAt(0));
clearBuf(scanner);
switch (choice) {
case 'A': //Add the item to the menu
System.out.println(" Enter the name : ");
String name = scanner.nextLine();
if(name.length() > 25) {
System.out.println("Item name cannot be more than 25.");
} else {
System.out.println(" Enter the description : ");
String desc = scanner.nextLine();
if(desc.length() > 75) {
System.out.println("Item description cannot be more than 75.");
} else {
System.out.println(" Enter the price : ");
double price = scanner.nextDouble();
clearBuf(scanner);
System.out.println(" Enter the position : ");
int position = scanner.nextInt();
clearBuf(scanner);
MenuItem item = new MenuItem(name, desc, price);
try {
menu.addItem(item, position);
System.out.println("Added "" + name + ": " + desc + """ +
37. " for $" + price + " at position " + position);
} catch (FullListException fle) {
System.out.println(fle.getMessage());
}
}
}
break;
case 'G': //Print out the name, description, and price of the item at the specified position in the
menu
if(menu.size() > 0) {
System.out.println(" Enter position : ");
int position = scanner.nextInt();
clearBuf(scanner);
MenuItem item = menu.getItem(position);
if(item != null)
System.out.println(item);
else
System.out.println("No item found at the specified position");
} else
System.out.println("No items in the menu");
break;
case 'R': //Remove the item with the given name in the menu
if(menu.size() > 0) {
System.out.println(" Enter the Name: ");
name = scanner.nextLine();
try {
menu.removeItem(name);
System.out.println("Removed "" + name + """);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else
System.out.println("No item");
38. break;
case 'P': //Print the list of all items on the menu
menu.printAllItems();
break;
case 'S': //Print the number of items on the menu
System.out.println("There are " + menu.size() + " items in the menu");
break;
case 'D': //Update the description of the named item
if(menu.size() > 0) {
System.out.println(" Enter the name of the item: ");
name = scanner.nextLine();
System.out.println(" Enter the new description: ");
String desc = scanner.nextLine();
if(desc.length() > 75) {
System.out.println("Item description cannot be more than 75.");
} else {
try {
menu.updateDescription(name, desc);
System.out.println("New description set.");
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
}
} else
System.out.println("No items in the menu");
break;
case 'C': //Update the price of the named item
if(menu.size() > 0) {
System.out.println(" Enter the name of the item: ");
name = scanner.nextLine();
System.out.println(" Enter the new price: ");
39. double price = scanner.nextDouble();
try {
menu.updatePrice(name, price);
System.out.println("Changed the price of "" + name + "" to " + price);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
} catch(NonPositivePriceException nppe) {
System.out.println(nppe.getMessage());
}
} else
System.out.println("No items in the menu");
break;
case 'O': //Add the item at the specified position in the menu to the order
if(menu.size() > 0) {
System.out.println("Enter position of item to add to order: ");
int position = scanner.nextInt();
clearBuf(scanner);
try {
MenuItem item = menu.getItem(position);
if(item == null)
System.out.println("No menu at position : " + position);
else {
orderCount += 1;
order.addItem(item, orderCount);
System.out.println("Added "" + item.getName() + "" to order");
}
} catch (FullListException fle) {
System.out.println(fle.getMessage());
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else {
System.out.println("No menu available to add to order");
40. }
break;
case 'I': //Remove the item at the specified position in the order
if(order.size() > 0) {
System.out.println(" Enter the position : ");
int position = scanner.nextInt();
clearBuf(scanner);
try {
MenuItem item = order.getItem(position);
order.removeItem(position);
orderCount -= 1;
System.out.println("Removed "" + item.getName() + "" from order");
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
} else
System.out.println("No items in the order");
break;
case 'V': //Print item in current order
System.out.println("ORDER");
order.printAllItems();
break;
case 'Q':
// Close scanner
scanner.close();
System.exit(0);;
break;
default:
System.out.println("No such operation ");
}
}
}
41. }
SAMPLE OUTPUT
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 :
8.5
Enter the position :
1
Added "Chicken Parmesan: Breaded chicken, tomato sauce, and cheese" for $8.5 at position 1
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
42. V) View order
Q) Quit
Select an operation :
a
Enter the name :
Hot dog
Enter the description :
Beef sausage in a bun with ketchup
Enter the price :
4.5
Enter the position :
1
Added "Hot dog: Beef sausage in a bun with ketchup" for $4.5 at position 1
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 :
p
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Hot dog Beef sausage in a bun with ketchup 4.50
2 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
Main menu:
A) Add Item
G) Get Item
43. 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 :
o
Enter position of item to add to order:
2
Added "Chicken Parmesan" to order
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 :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
Main menu:
A) Add Item
44. 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 :
o
Enter position of item to add to order:
1
Added "Hot dog" to order
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 :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Chicken Parmesan Breaded chicken, tomato sauce, and cheese
8.50
2 Hot dog Beef sausage in a bun with ketchup 4.50
45. 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 :
i
Enter the position :
1
Removed "Chicken Parmesan" from order
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 :
v
ORDER
# Item Name Item Description Item Price
-------------------------------------------------------------------------------------------------------------------
1 Hot dog Beef sausage in a bun with ketchup 4.50
46. 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 :
q