Question

Next, implement the Drink class, which represents the specification for one type of drink. The Drink class should have: Two iYou can test your class with TestPhase2.java. You should get the output shown below (plus or minus any different rounding errMars, Bruno, $4.5: Latte, Ingredients: Milk, 500 mls, $2.55/L Espresso, 100 mls, $4.05/L Profit: $2.8200000000000003 BelongsProfit: $3.0700000000000003 Belongs to Ada? true

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

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";
}
}

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

public class TestPhase2 {
  
  

public static void main(String[] args) {
Customer bruno = new Customer("Bruno", "Mars");
Customer taylor = new Customer("Taylor", "Swift");
Customer ada = new Customer("Lovelace", "Ada");
System.out.println("Customer1: " + bruno);
System.out.println("Customer2: " + taylor);
System.out.println("Customer3: " + ada);
  
System.out.println();
  
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);

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);
  
System.out.println("Drink1: " + latte);
System.out.println("Drink1 cost $" + latte.calculateCost());
System.out.println();
  
System.out.println("Drink2: " + soyLatte);
System.out.println("Drink2 cost $" + soyLatte.calculateCost());
System.out.println();
  
System.out.println("Drink3: " + hazeLatte);
System.out.println("Drink3 $" + hazeLatte.calculateCost());
System.out.println();

DrinkOrder o1 = new DrinkOrder(bruno, latte, 4.50);
DrinkOrder o2 = new DrinkOrder(bruno, hazeLatte, 5.00);
DrinkOrder o3 = new DrinkOrder(taylor, soyLatte, 6.00);
DrinkOrder o4 = new DrinkOrder(ada, latte, 4.75);
  
System.out.println(o1);
System.out.println("Profit: $" + o1.getProfit());
System.out.println("Belongs to Taylor? " + o1.belongsTo(taylor));
System.out.println();
  
System.out.println(o2);
System.out.println("Profit: $" + o2.getProfit());
System.out.println("Belongs to Bruno? " + o2.belongsTo(bruno));
System.out.println();
  
System.out.println(o3);
System.out.println("Profit: $" + o3.getProfit());
System.out.println("Belongs to Taylor? " + o3.belongsTo(taylor));
System.out.println();
  
System.out.println(o4);
System.out.println("Profit: $" + o4.getProfit());
System.out.println("Belongs to Ada? " + o4.belongsTo(ada));
  
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

public class Drink {
  
   //Variable declaratio
   String drinkName;
   List<Ingredient> 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:\n");
       for(Ingredient i : ingredients) {
           output.append("\t" + i.ingredientName + ", " + i.amount +" mls, " + "$"+i.price +"/L\n");
       }
      
       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();
   }

}

Output

Customer1: Mars, Bruno
Customer2: Swift, Taylor
Customer3: Ada, Lovelace

Drink1: Latte, Ingredients:
   Milk, 500 mls, $2.55/L
   Espresso, 100 mls, $4.05/L

Drink1 cost $1.68

Drink2: Soy Latte, Ingredients:
   Soy Milk, 500 mls, $4.75/L
   Espresso, 100 mls, $4.05/L

Drink2 cost $2.7800000000000002

Drink3: Hazelnut Latte, Ingredients:
   Milk, 500 mls, $2.55/L
   Espresso, 100 mls, $4.05/L
   Hazelnut Syrup, 30 mls, $13.5/L

Drink3 $2.085

Mars, Bruno, 4.5: Latte, Ingredients:
   Milk, 500 mls, $2.55/L
   Espresso, 100 mls, $4.05/L

Profit: $2.8200000000000003
Belongs to Taylor? false

Mars, Bruno, 5.0: Hazelnut Latte, Ingredients:
   Milk, 500 mls, $2.55/L
   Espresso, 100 mls, $4.05/L
   Hazelnut Syrup, 30 mls, $13.5/L

Profit: $2.915
Belongs to Bruno? true

Swift, Taylor, 6.0: Soy Latte, Ingredients:
   Soy Milk, 500 mls, $4.75/L
   Espresso, 100 mls, $4.05/L

Profit: $3.2199999999999998
Belongs to Taylor? true

Ada, Lovelace, 4.75: Latte, Ingredients:
   Milk, 500 mls, $2.55/L
   Espresso, 100 mls, $4.05/L

Profit: $3.0700000000000003
Belongs to Ada? true

Add a comment
Know the answer?
Add Answer to:
----------------------------------- public class Customer { String firstName,lastName; //constructor public Customer(String firstName,String lastName) { this.firstName=firstName; this.lastName=lastName; }...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Correct the mistakes public class customer { public static void main(String[] args) { D customer=new d();...

    Correct the mistakes public class customer { public static void main(String[] args) { D customer=new d(); d.employee(56,”ali”); d.department(7,8.6,9); } public void employee(String name, int age) { System.out.println("Your name is"+name); System.out.println(“Age is”+age); } public int department(double d, double t, int a) { System.out.println("I have java exam at 3:00"); Return(1.1); } } V. PROGRAMMING. Write a complete JAVA program to implement the following. 1. Create a class named Exponent. Its main() method accepts an integer value from a user at the keyboard,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT