Loading...
Loading...
A canteen sells snacks from a fixed menu and cakes made to order. Write interface Priced { int price(); String label(); }. enum Snack implements Priced has SAMOSA(15), VADA_PAV(20), CHAI(10) and POHA(25); its label is its name. class Cake implements Priced has a flavour and a weight in kg; it costs Rs. 600 per kg and its label is <flavour> cake <kg> kg.
Add every bill line through one method that takes a Priced and a quantity.
SNACK_NAME qty (any case) or CAKE flavour kg.label x qty = Rs. amount (a cake always has qty 1), or Unknown item WORD for a word that is neither a snack nor CAKE. Last line: Total: Rs. total.Example
Input
4 samosa 3 CAKE Chocolate 2 chai 2 pizza 1
Output
SAMOSA x 3 = Rs. 45 Chocolate cake 2 kg x 1 = Rs. 1200 CHAI x 2 = Rs. 20 Unknown item pizza Total: Rs. 1265
Pizza is not on the menu; its quantity is still read and ignored.
Try the problem first. Open one hint at a time.
Read the first word; if it equals CAKE (ignoring case), read a flavour and a weight, otherwise try Snack.valueOf(word.toUpperCase()) and read the quantity.
A method static int addLine(Priced item, int qty) can print the line and return the amount for both kinds.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor