Loading...
Loading...
Java · References, Casting & Immutability
A pizza shop makes immutable Pizza objects only through Pizza.Builder, a static nested class; Pizza's constructor is private. A builder starts at size M with no cheese and no toppings. SIZE c sets the size (the last one wins), CHEESE adds extra cheese (more than once counts once) and TOPPING adds one topping. Price: S Rs. 150, M Rs. 250, L Rs. 400, extra cheese Rs. 60, each topping Rs. 40.
Pizza <size> cheese=<yes|no> toppings=<t> Rs. <price>. Then Total Rs. <sum>.Example
Input
2 3 SIZE L CHEESE TOPPING 0
Output
Pizza L cheese=yes toppings=1 Rs. 500 Pizza M cheese=no toppings=0 Rs. 250 Total Rs. 750
The second order has no commands, so it is the default medium pizza.
Try the problem first. Open one hint at a time.
The builder holds the changing choices; each builder method returns this.
build() calls the private Pizza(Builder b) constructor, which copies the choices into final fields.
Create a new Pizza.Builder() for every order, so no choice leaks into the next pizza.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor