In: Computer Science
First level CS, should not be too complex. Need in a week.
You must turn in three java files - FoodItem.java, Order.java and MyOrder.java. In addition to these, you must include a screenshot of the output by executing the file MyOrder.java. Submissions without all these files will not be graded.
Task 1:
Design a class named FoodItem which contains the following fields:
Type of food item: Like entree, bread, soup, salad, drink or dessert to name a few
Item number: could be an alphanumeric code or just a number
Unit price: $xx.xx
You must include appropriate instance methods like constructors, getters , setters and toString for all the fields.
Task 2:
Create another class named Order which has the following fields:
Order number: It could be an alphanumeric id or numeric.
Customer Name: First and last name of the customer
Combo meal option: yes or no
DiscountAmount: If combo meal is chosen, offer a 5% discount
OrderTotal: Total bill amount
You must create the following methods for the Order class:
Constructor (s) - You may include an extra constructor that takes in the Customer name as a parameter.
Setters, getters and toString methods for the appropriate fields.
add(item,quantity) - this method adds the price of purchasing an item to the Order total.
getOrderTotal() - this method returns the total bill amount to be paid by the customer after including any discounts applicable and also a 10% sales tax on the total amount.
Task 3:
Create a class named MyOrder that displays a Menu of items available to choose from, creates an order for two different customers and reports their total bill amount. Use appropriate print statements to guide the user through the ordering process. Remember, this is the class where you are actually placing an order (action required). So, this class must contain the main method.
/**
*
* FoodItem class
*/
public class FoodItem {
//Type : entree, bread, soup, salad, drink or dessert to name a few
String itemType;
// could be an alphanumeric code or just a number
String itemNumber;
// Unit price: $xx.xx
double unitPrice;
// constructors
public FoodItem(String itemType, String itemNumber, double unitPrice) {
this.itemType = itemType;
this.itemNumber = itemNumber;
this.unitPrice = unitPrice;
}
// getters and setters
public String getItemType() {
return itemType;
}
public void setItemType(String itemType) {
this.itemType = itemType;
}
public String getItemNumber() {
return itemNumber;
}
public void setItemNumber(String itemNumber) {
this.itemNumber = itemNumber;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
// toString for all the fields.
@Override
public String toString() {
return "itemNumber = " + itemNumber + ", itemType = " + itemType + ", unitPrice = " + unitPrice;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package foodordering;
/**
*
* Order class
*/
public class Order {
// Order number: It could be an alphanumeric id or numeric.
String orderNumber;
// Customer Name: First and last name of the customer
String customerName;
// Combo meal option: yes or no
boolean comboMeal;
// DiscountAmount: If combo meal is chosen, offer a 5% discount
double discountAmount;
// OrderTotal: Total bill amount
double totalBill;
//Constructor -include an extra constructor that takes in the Customer name as a parameter.
public Order(String customerName) {
this.customerName = customerName;
}
public Order(String orderNumber, String customerName, boolean comboMeal) {
this.orderNumber = orderNumber;
this.customerName = customerName;
setComboMeal(comboMeal);
}
//Setters, getters
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public boolean isComboMeal() {
return comboMeal;
}
public void setComboMeal(boolean comboMeal) {
this.comboMeal = comboMeal;
if(this.comboMeal){
discountAmount = 5;
}else{
discountAmount = 0;
}
}
public double getTotalBill() {
return totalBill;
}
public void setTotalBill(double totalBill) {
this.totalBill = totalBill;
}
//adds the price of purchasing an item to the Order total.
void add(FoodItem item,int quantity){
totalBill += (item.unitPrice * quantity);
}
/*getOrderTotal() - this method returns the total bill amount to be paid
by the customer after including any discounts applicable
and also a 10% sales tax on the total amount.*/
double getOrderTotal(){
double bill = totalBill;
bill -= bill * (discountAmount /100);
bill += bill * (10/100);
return bill;
}
@Override
public String toString() {
return "orderNumber=" + orderNumber + ", "
+ "customerName=" + customerName + ", "
+ "comboMeal=" + comboMeal + ", totalBill=" + totalBill;
}
}
package foodordering;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* MyOrder class
*/
public class MyOrder {
public static void main(String[] args) {
//Create a list of foodItem
ArrayList<FoodItem> foodItem = new ArrayList<>();
// Adding new elements to the FoodItem
foodItem.add(new FoodItem("entree", "P123", 250.20));
foodItem.add(new FoodItem("salad", "SA123", 100.10));
foodItem.add(new FoodItem("bread", "B123", 150.23));
foodItem.add(new FoodItem("soup", "SO123", 200));
foodItem.add(new FoodItem("salad", "SA123", 350));
Scanner keyboard = new Scanner(System.in);
ArrayList<Order> orders = new ArrayList<>();
orders.add(new Order("12424", "Sara", false));
orders.add(new Order("1234", "Prof", true));
String choice;
for(Order order : orders){
System.out.println("Order for " + order.getCustomerName());
do{
for(FoodItem item : foodItem){
// getQualityPoint converts grade letter into grade points
System.out.println(item.toString());
}
System.out.print("Enter your item number (-1 to exit): " );
choice = keyboard.nextLine();
for(FoodItem item: foodItem){
if(item.getItemNumber().equalsIgnoreCase(choice)){
System.out.print("Enter Quantity: ");
int qty = keyboard.nextInt();
keyboard.nextLine();
order.add(item, qty);
}
}
}while(!choice.equalsIgnoreCase("-1"));
System.out.println(order.toString());
}
}
}