Coffee Decorator

Report a typo

Now your task is to make some Coffee. Your Decorators are different fillers; as a pro barista, you are provided with Sugar, Milk, and Whipped Cream.

Sample Input 1:

Sample Output 1:

Espresso $1.99
Espresso, Milk, Sugar $2.14
Instant Coffee, Whipped Cream, Sugar, Sugar, Sugar $1.16
I'm drinking my Instant Coffee, Whipped Cream, Sugar, Sugar, Sugar
-I want to add some Whipped Cream to my coffee. And don't make a new one! Just add Whipped Cream
-Okay! But the final price will change
-I understand
Instant Coffee, Whipped Cream, Sugar, Sugar, Sugar, Whipped Cream $1.26
Write a program in Java 25
import java.util.Locale;

class TestDrive {
public static void main(String[] args) throws InterruptedException {
Coffee simpleEspresso = new Espresso();
System.out.println(/* write your code here */ + " $" + formatSum(/* write your code here */));

Coffee espressoWithDecor = /* write your code here */;
espressoWithDecor = new Milk(/* write your code here */);
espressoWithDecor = new Sugar(/* write your code here */);
System.out.println(/* write your code here */ + " $" + formatSum(/* write your code here */));

Coffee instantWithDecor = /* write your code here */;
instantWithDecor = new WhippedCream(/* write your code here */);
instantWithDecor = new Sugar(/* write your code here */);
instantWithDecor = new Sugar(/* write your code here */);
instantWithDecor = new Sugar(/* write your code here */);
System.out.println(/* write your code here */ + " $" + formatSum(/* write your code here */));

System.out.println("I'm drinking my " + instantWithDecor.getDescription());
Thread.sleep(1500);
System.out.println("-I want to add some Whipped Cream to my coffee. And don't make a new one! Just add Whipped Cream");
Thread.sleep(1500);
System.out.println("-Okay! But the final price will change");
Thread.sleep(1500);
System.out.println("-I understand");

instantWithDecor = new WhippedCream(/* write your code here */);
System.out.println(/* write your code here */ + " $" + formatSum(/* write your code here */));
}

private static String formatSum(double sum) {
return String.format(Locale.ROOT, "%.2f", sum);
}
}

Create a free account to access the full topic