Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

------------------------ public class Customer { String firstName,lastName; //constructor public Customer(String firstName,String lastName) { this.firstName=firstName; this.lastName=lastName; } //override toString() method public String toString() { return this.lastName+,

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

------------------------

public class Customer { String firstName,lastName; //constructor public Customer(String firstName,String lastName) { this.firstName=firstName; this.lastName=lastName; } //override toString() method public String toString() { return this.lastName+", "+this.firstName; } boolean equals(Customer obj) { if(this.firstName.equals(obj.firstName) && this.lastName.equals(obj.lastName)) return true; else return false; } }

------------------------

public class Ingredient { String ingredientName; int amount; double price; public Ingredient(String ingredientName,int amount,double price) { this.ingredientName=ingredientName; this.amount=amount; this.price=price; } double getCost() { return amount/1000.0*price; } public String toString() { return this.ingredientName+", "+this.amount+" mls, $"+this.price+"/L"; } }

------------------------

import java.util.ArrayList; import java.util.List;

public class Drink { //Variable declaratio String drinkName; List ingredients; //Constructor to load values public Drink(String drink) { drinkName = drink; ingredients = new ArrayList(); }

//Add Ingredient to list public void addIngredient(Ingredient ingredient) { // TODO Auto-generated method stub ingredients.add(ingredient); }

//Calculate cost public double calculateCost() { // TODO Auto-generated method stub double cost = 0; //Loop thru all Ingredient and sum cost for(Ingredient i : ingredients) { cost += i.getCost(); } return cost; } public String toString() { StringBuffer output = new StringBuffer(); output.append(drinkName +", Ingredients: "); for(Ingredient i : ingredients) { output.append(" " + i.ingredientName + ", " + i.amount +" mls, " + "$"+i.price +"/L "); } return output.toString(); }

}

------------------------

public class DrinkOrder { //Variable declaration Customer custome; Drink drink; double amount; // Constructor to load values public DrinkOrder(Customer customer, Drink drink, double amount) { // TODO Auto-generated constructor stub this.custome =customer; this.drink = drink; this.amount = amount; } //Calculate profit public double getProfit() { // aount sold - cost return amount - drink.calculateCost(); } // Use equals method to compare the object public boolean belongsTo(Customer that) { // TODO Auto-generated method stub return this.custome.equals(that); } public String toString() { StringBuffer output = new StringBuffer(); output.append(custome.lastName +", " + custome.firstName +", " + "$"+amount +": " + drink.toString()); return output.toString(); }

}

------------------------

public class TestPhase3 { public static void main(String[] args) { //set up some customers Customer bruno = new Customer("Bruno", "Mars"); Customer taylor = new Customer("Taylor", "Swift"); Customer ada = new Customer("Lovelace", "Ada");

//set up some ingredients Ingredient milk = new Ingredient("Milk", 500, 2.55); Ingredient espresso = new Ingredient("Espresso", 100, 4.05); Ingredient syrup = new Ingredient("Hazelnut Syrup", 30, 13.5); Ingredient soyMilk = new Ingredient("Soy Milk", 500, 4.75);

//set up some drinks Drink latte = new Drink("Latte"); latte.addIngredient(milk); latte.addIngredient(espresso); Drink soyLatte = new Drink("Soy Latte"); soyLatte.addIngredient(soyMilk); soyLatte.addIngredient(espresso); Drink hazeLatte = new Drink("Hazelnut Latte"); hazeLatte.addIngredient(milk); hazeLatte.addIngredient(espresso); hazeLatte.addIngredient(syrup);

//Set up some stores CoffeeShop starbucks = new CoffeeShop("Starbucks", 3.5); CoffeeShop secondCup = new CoffeeShop("Second Cup", 2.1); CoffeeShop goodEarth = new CoffeeShop("Good Earth Coffee", 2.5); starbucks.newOrder(bruno, latte); starbucks.displayOrdersPending(); System.out.println(starbucks.getName() + " profit: " + starbucks.getProfit()); System.out.println();

starbucks.newOrder(taylor, soyLatte); starbucks.displayOrdersPending(); System.out.println(starbucks.getName() + " profit: " + starbucks.getProfit()); System.out.println(); System.out.println("Filled order at Starbucks: " + taylor); starbucks.orderFilled(taylor); starbucks.displayOrdersPending(); secondCup.newOrder(ada, latte); secondCup.newOrder(taylor, soyLatte); secondCup.displayOrdersPending(); System.out.println(secondCup.getName() + " profit: " + secondCup.getProfit()); System.out.println(); secondCup.newOrder(bruno, hazeLatte); secondCup.displayOrdersPending(); System.out.println(secondCup.getName() + " profit: " + secondCup.getProfit()); System.out.println(); System.out.println("Trying to fill an order not at Starbucks: " + ada); starbucks.orderFilled(ada); starbucks.displayOrdersPending(); System.out.println("Filled order at Starbucks: " + bruno); starbucks.orderFilled(bruno); starbucks.displayOrdersPending(); System.out.println(); System.out.println("Total coffee orders: " + CoffeeShop.getTotalOrders());

} }

Phase 3: CoffeeSho Next implement the CoffeeShop class, which contains the information and methods for a particular coffee shop The CoffeeShop class should have AString instance variable to hold the name of the shop An instance variable that contains a list of drink orders to be filled. Use a simple partially-filled array to do this You may assume that the list will never become full no error checking is needed A double instance variable that contains the shop's "mark-up" factor. The coffee shop will use this mark-up to calculate how much to charge for a drink given its actual cost (see newOrder below for details) An instance variable for the shop's total profit thus far (a double) A static class variable that stores the total number of drink orders filled across all CoffeeShops (an int) constructor with two parameters: the shop's name (a String) and the shop's mark-up factor (a double) A simple accessor method getName() that returns the shop's name (a String) A method void newOrder(Customer, Drink) that creates a DrinkOrder for that customer and adds the order to the partially filled array. When creating the DrinkOrder, use the mark-up factor instance variable and the drink's actual cost to determine how much to charge the customer. We are using a simple multiplicative formula for this assignment. For example, a mark-up factor of 1.1 means that the shop charges 1.1 times the actual cost of any drink. We won't worry about rounding the calculation to two decimal places. The method should also update the shop's profit instance variable and the class variable representing the total number of orders across all coffee shops A method void orderFilled(Customer) that removes the customer's drink order from the partially filled array You can assume that each Customer has only one order waiting to be filled at the shop. To keep this method short, you can create a private "helper" method that returns the position of the drink order in the array A method getNumOrdersPending() that returns the number of drink orders needing to be filled at the shop (an int) A method void displayOrdersPending() that displays the drink orders that still need to be completed at the shop. Note: this is one of only two instance methods in this assignment that outputs directly to the screen method getProfit that returns the shop's total profit (a double) A class method getTotalOrders that returns the number of drink orders across all coffee shops (an int) A . A You can test your class with TestPhase3.java. You should get the output shown below: Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 10 mls, $4.05/L Starbucks profit: 4.2 Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/1L 2: Swift, Taylor, $9.73: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/1L Espresso, 100 mls, $4.05/L Starbucks profit: 11.15 Filled order at Starbucks: Swift, Taylor Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Cup Orders pending at Second 1: Ada, Lovelace, $3.528: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L 2: Swift, Taylor, $5.838000000000001: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/L Espresso, 100 mls, $4.05/L Second Cup profit: 4.906000000000001 Cup Orders pending at Second 1: Ada, Lovelace, $3.528: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L 2: Swift, Taylor, $5.838000000000001: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/L Espresso, 100 mls, $4.05/L 3: Mars, Bruno, $4.3785: Hazelnut Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Hazelnut Syrup, 30 mls, $13.5/L Second Cup profit: 7.1995000000000005 Trying to fill an order not at Starbucks: Ada, Lovelace Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Filled order at Starbucks: Mars, Bruno Orders pending at Starbucks: None Total coffee orders: 5 Phase 3: CoffeeSho Next implement the CoffeeShop class, which contains the information and methods for a particular coffee shop The CoffeeShop class should have AString instance variable to hold the name of the shop An instance variable that contains a list of drink orders to be filled. Use a simple partially-filled array to do this You may assume that the list will never become full no error checking is needed A double instance variable that contains the shop's "mark-up" factor. The coffee shop will use this mark-up to calculate how much to charge for a drink given its actual cost (see newOrder below for details) An instance variable for the shop's total profit thus far (a double) A static class variable that stores the total number of drink orders filled across all CoffeeShops (an int) constructor with two parameters: the shop's name (a String) and the shop's mark-up factor (a double) A simple accessor method getName() that returns the shop's name (a String) A method void newOrder(Customer, Drink) that creates a DrinkOrder for that customer and adds the order to the partially filled array. When creating the DrinkOrder, use the mark-up factor instance variable and the drink's actual cost to determine how much to charge the customer. We are using a simple multiplicative formula for this assignment. For example, a mark-up factor of 1.1 means that the shop charges 1.1 times the actual cost of any drink. We won't worry about rounding the calculation to two decimal places. The method should also update the shop's profit instance variable and the class variable representing the total number of orders across all coffee shops A method void orderFilled(Customer) that removes the customer's drink order from the partially filled array You can assume that each Customer has only one order waiting to be filled at the shop. To keep this method short, you can create a private "helper" method that returns the position of the drink order in the array A method getNumOrdersPending() that returns the number of drink orders needing to be filled at the shop (an int) A method void displayOrdersPending() that displays the drink orders that still need to be completed at the shop. Note: this is one of only two instance methods in this assignment that outputs directly to the screen method getProfit that returns the shop's total profit (a double) A class method getTotalOrders that returns the number of drink orders across all coffee shops (an int) A . A You can test your class with TestPhase3.java. You should get the output shown below: Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 10 mls, $4.05/L Starbucks profit: 4.2 Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/1L 2: Swift, Taylor, $9.73: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/1L Espresso, 100 mls, $4.05/L Starbucks profit: 11.15 Filled order at Starbucks: Swift, Taylor Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Cup Orders pending at Second 1: Ada, Lovelace, $3.528: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L 2: Swift, Taylor, $5.838000000000001: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/L Espresso, 100 mls, $4.05/L Second Cup profit: 4.906000000000001 Cup Orders pending at Second 1: Ada, Lovelace, $3.528: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L 2: Swift, Taylor, $5.838000000000001: Soy Latte, Ingredients: Soy Milk, 500 mls, $4.75/L Espresso, 100 mls, $4.05/L 3: Mars, Bruno, $4.3785: Hazelnut Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Hazelnut Syrup, 30 mls, $13.5/L Second Cup profit: 7.1995000000000005 Trying to fill an order not at Starbucks: Ada, Lovelace Orders pending at Starbucks: 1: Mars, Bruno, $5.88: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Filled order at Starbucks: Mars, Bruno Orders pending at Starbucks: None Total coffee orders: 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions