Wednesday, 15 July 2015

java - How to add numbers that i get from switch case output? -



java - How to add numbers that i get from switch case output? -

public class menucard { public static void main (string [] args)throws ioexception { scanner input= new scanner(system.in); int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item; system.out.print(" menu \n1.tea :5\n2.coffee :7\n3.samosa :8\n4.idly :15\n5.biryani :50\n6.talaawa :35\n \thow many items want order:"); int size=input.nextint(); for(int i=1;i<=size;i++) { system.out.print("order item no"+i+":"); int choice=input.nextint(); switch(choice) { case 1: system.out.println("tea : inr "+tea); break; case 2: system.out.println("coffee : inr "+coffee); break; case 3: system.out.println("samosa : inr "+samosa); break; case 4: system.out.println("idly : inr "+idly); break; case 5: system.out.println("biryani : inr "+biryani); break; case 6: system.out.println("talaawa : inr "+talawa); break; default: system.out.println("invalid entry"); } } } }

output is:

menu 1.tea :5 2.coffee :7 3.samosa :8 4.idly :15 5.biryani :50 6.talaawa :35 how many items want order:5 order item no1:3 samosa : inr 8 order item no2:6 talaawa : inr 35 order item no3:1 tea : inr 5 order item no4:3 samosa : inr 8 order item no5:4 idly : inr 15

i need generate bill adding cost of item.

how should add together items..?

please don't utilize switch statements! create life hard.

i suppose doing following:

public interface menucard { void addtomenuitems(menuitem item); list<menuitem> getmenuitems(); void renderto(printstream printstream); } public interface menuitem { bigdecimal getprice(); string getdescription(); } public interface order { void add(menuitem item); //total cost of ordered items bigdecimal getprice(); } public static void main(string[] args) { scanner input= new scanner(system.in); menucard menucard = getmenucard(); //from database or wherever stored menucard.renderto(system.out); order order = createneworder(); int itemindex = input.nextint(); while(itemindex > 0) { order.add(menucard.getmenuitems().get(itemindex-1)); itemindex = input.nextint(); } //now generate bill order }

java switch-statement add

No comments:

Post a Comment