Alright, this has to be done in java here, and I am going to list the code that
ID: 3854630 • Letter: A
Question
Alright, this has to be done in java here, and I am going to list the code that I already have completed first. These classes, the abstract MenuItems class, Desert Extends MenuItems class, Drink Extends MenuItems Class, Entree Extends MenuItems class, Menu class, and, Ticket class, are all all completed. What needs worked on is the Driver class, which I will have at the bottom, although I dont have any methods or anything at it. My questions will be down there, just above the Driver class, as well so you don't have to continue to scroll up and down through the entire page constantly.
abstract public class MenuItems{
private String name; //name to label all items
private String orderCode; //two letter code that selects an item
private double price; // price field
public String getName(){return name;}
public String getOrderCode(){return orderCode;}
public double getPrice(){return price;}
public void setName(String name){this.name = name;}
public void setPrice(double price){this.price = price;}
public MenuItems(String name, String orderCode, double price){
this.name = name;
this.orderCode = orderCode;
this.price = price;
}
//setOrder has an error catcher so program wont crash if input != 2 characters
public void setOrderCode(String orderCode){
if(orderCode.length() == 2)
this.orderCode = orderCode;
else
throw new IllegalArgumentException("Order code must have two "
+ "characters only!!");
}
@Override
public abstract String toString();
}
public class Drink extends MenuItems{
private char size;
private boolean checkID;
public void setSize(char size){this.size = size;}
public char getSize(){return size;}
public void setCheckID(boolean checkID){this.checkID = checkID;}
public boolean getCheckID(){return checkID;}
//constructor that extends from MenuItems
public Drink(char size, boolean checkID, String name, String orderCode, double price) {
super(name, orderCode, price);
this.size = size;
this.checkID = checkID;
}
@Override
public String toString(){
return "Drink{" + "Name=" + getName() + "Price=" + getPrice() + " OderCode="
+ getOrderCode() + "size=" + size + ", chekID=" + checkID + '}';
}
}
public class Desert extends MenuItems{
private boolean glutonFree;
public Desert(boolean glutonFree, String name, String orderCode, double price){
super(name, orderCode, price);
this.glutonFree = glutonFree;
}
public boolean getGlutonFree(){return glutonFree;}
public void setGlutonFree(boolean glutonFree){this.glutonFree = glutonFree;}
@Override
public String toString() {
return "Dessert{" + "Name=" + getName() + "Price=" + getPrice() +
" OderCode=" + getOrderCode() + "glutonFree=" + glutonFree + '}';
}
}
public class Entree extends MenuItems{
private boolean vegan;
public void setVegan(boolean vegan){this.vegan = vegan;}
public boolean getVegan(){return vegan;}
//constructor that extends from MenuItems
public Entree(boolean vegan, String name, String orderCode, double price) {
super(name, orderCode, price);
this.vegan = vegan;
}
@Override
public String toString() {
return "Entree{" + "Name=" + getName() + "Price=" + getPrice() +
" OrderCode=" + getOrderCode() + "vegan=" + vegan + '}';
}
}
public class Menu{
private MenuItems[] menuItems;//array of classes MenuItems
int n; //counter number
//reserves 20 indexes for objects in array
public Menu(){
menuItems = new MenuItems[20];
n = 0;
}
//lists all items NAME, ORDERCODE, PRICE.
public void listAllItems(){
for(int i = 0; i < n; i++){
// System.out.println(menuItems[i].getName()+menuItems[i].getOrderCode()
// " $" + menuItems[i].getPrice());
System.out.print(menuItems[i].getName()+" "+menuItems[i].getOrderCode());
System.out.println(" $"+menuItems[i].getPrice());
}
}
public void addItems(){
}
//sets up for taking input of orderCode and compare input to orderCode field
public MenuItems getItem(String orderCode){
for(int i = 0; i < n; i++)
if(menuItems[i].getOrderCode().equals(orderCode))
return menuItems[i];
return null;
}
}
public class Ticket{
private MenuItems[] menuItems;
int n;
public Ticket(){
menuItems = new MenuItems[20];
n = 0;
}
public void addItem(Menu menu, String orderCode){
MenuItems menuItem = menu.getItem(orderCode);
if (menuItem != null)
menuItems[n++] = menuItem;
}
public void deleteTicket(){
n = 0;
}
public void viewTicket(){
for(int i = 0; i < n; i++){
System.out.print(menuItems[i].getName()+" "+menuItems[i].getOrderCode());
System.out.println("/t$" + menuItems[i].getPrice());
}
}
public void tabOut(){
int total = 0;
for (int i = 0; i < n ; i++)
total += menuItems[i].getPrice();
System.out.println("Total of all item is: " + total);
}
}
Right now, I need help with getting the arrays established for the drink items, desert items, and entree items. I dont know how to get the arrays established in this polymorphisim so I can hardcode like an entree what an entree's name, price, and if it's vegan or not.
Then I need to put all the MenuItems array into the Menu, and call the menu class so every object prints out.
Explanation / Answer
Basically for creating array with polymorphism property, we just need to create an array of parent class and add all child elements as below: -
Drink d1 = new Drink('L', true, "CocaCola", "A1", 40);
Drink d2 = new Drink('S', true, "CocaCola", "A2", 20);
Desert ds1 = new Desert(false, "Ice Cream", "I1", 100);
Desert ds2 = new Desert(true, "Shake", "I2", 70);
Entree e1 = new Entree(true, "RaffleSnack", "RS", 240.56);
MenuItems[] items = new MenuItems[20];
items[0] = d1;
items[1] = d2;
items[2] = ds1;
items[3] = ds2;
items[4] = e1;
But as your Menu class is checking the value of n and listing the array, so we need a method to add all elements in the array.. in you Menu class your addItems() method was empty so, I implemented it to get the values of menuitem array to the class.
Your class of Menu look likes follows: -
Menu.java
public class Menu {
private MenuItems[] menuItems;// array of classes MenuItems
int n; // counter number
// reserves 20 indexes for objects in array
public Menu() {
menuItems = new MenuItems[20];
n = 0;
}
// lists all items NAME, ORDERCODE, PRICE.
public void listAllItems() {
for (int i = 0; i < n; i++) {
// System.out.println(menuItems[i].getName()+menuItems[i].getOrderCode()
// " $" + menuItems[i].getPrice());
System.out.print(menuItems[i].getName() + " " + menuItems[i].getOrderCode());
System.out.println(" $" + menuItems[i].getPrice());
}
}
public void addItems(MenuItems[] items) {
this.menuItems = items;
}
// sets up for taking input of orderCode and compare input to orderCode
// field
public MenuItems getItem(String orderCode) {
for (int i = 0; i < n; i++)
if (menuItems[i].getOrderCode().equals(orderCode))
return menuItems[i];
return null;
}
}
See the bold part above for the changes.
Now the Driver class looks like below: -
Driver.java
public class Driver {
public static void main(String[] args) {
Drink d1 = new Drink('L', true, "CocaCola", "A1", 40);
Drink d2 = new Drink('S', true, "CocaCola", "A2", 20);
Desert ds1 = new Desert(false, "Ice Cream", "I1", 100);
Desert ds2 = new Desert(true, "Shake", "I2", 70);
Entree e1 = new Entree(true, "RaffleSnack", "RS", 240.56);
MenuItems[] items = new MenuItems[20];
items[0] = d1;
items[1] = d2;
items[2] = ds1;
items[3] = ds2;
items[4] = e1; // adding all elements in the array
Menu menu = new Menu();
menu.addItems(items);
menu.n = 5; // 5 elements are added
menu.listAllItems(); // printing the array
}
}
If we run this class output comes as follows: -
CocaCola A1 $40.0
CocaCola A2 $20.0
Ice Cream I1 $100.0
Shake I2 $70.0
RaffleSnack RS $240.56
Hope this is what you wanted. Let me know in comments if you needed something else in this
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.