This fun task involves Pizza decorating. Imagine that the basic ingredients are used, but you want to add something more.
Decorator
Pizza Decorator
Report a typo
Sample Input 1:
Sample Output 1:
Vegan $4.99
Vegan, Broccoli, Tomato, Spinach $5.27
MeatHeaven, Ham, Chicken, Cheese $6.70Write a program in Java 25
import java.util.Locale;
class TestDrive {
public static void main(String[] args) {
Pizza simpleVeganPizza = new Vegan();
System.out.println(simpleVeganPizza.getDescription() + " $" + formatSum(simpleVeganPizza.cost()));
Pizza veganPizzaDecor = new Vegan();
veganPizzaDecor = new Broccoli(/* write your code here */);
veganPizzaDecor = new Tomato(/* write your code here */);
veganPizzaDecor = new Spinach(/* write your code here */);
System.out.println(veganPizzaDecor.getDescription() + " $" + formatSum(veganPizzaDecor.cost()));
Pizza meatPizzaDecor = new MeatHeaven();
meatPizzaDecor = new Ham(/* write your code here */);
meatPizzaDecor = new Chicken(/* write your code here */);
meatPizzaDecor = new Cheese(/* write your code here */);
System.out.println(meatPizzaDecor.getDescription() + " $" + formatSum(meatPizzaDecor.cost()));
}
private static String formatSum(double sum) {
return String.format(Locale.ROOT, "%.2f", sum);
}
}
abstract class Pizza {
String description;
String getDescription() {
return description;
}
abstract double cost();
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.