i need to do the system about the item from the small shop.
how can i calculate the item with price. if user choose more quantity from it. 1. ikan masin (price = 1.50) 2. garam (price = 1.00) 3. buttercup (price = 1.00) 4. mentega (price = 2.00) 5. honey (price = 3.00)
if customer pay more from the buying value, it will give the change. example : 3.00+2.00 = 5.00 customer pay 10.00 - 5.00 = 5.00
how can i do this coding with swtich case from the java.
thanks for helps.
Hi Friend,
Try the following code:
import java.util.*; class Product{ public String item; public int quantity; public double price; public double total; public Product(){} public Product(String item,int quantity, double price,double total) { super(); this.item=item; this.quantity= quantity; this.price = price; this.total=total; } public String getItem() { return item; } public int getQuantity() { return quantity; } public double getPrice() { return price; } public double getTotal() { return total; } } public class BuyProducts { public static void main(String[] args) throws Exception { double sum=0.0; List<Product> list = new ArrayList<Product>(); Scanner scan = new Scanner(System.in); int menu = 0; System.out.println("School Registration System Main Menu"); System.out.println(); System.out.println("1. Ikan masin"); System.out.println("2. Garam Masala"); System.out.println("3. Buttercup"); System.out.println("4. Mentega "); System.out.println("5. Honey"); System.out.println("6. Customer Bill"); System.out.println("7. Exit"); boolean quit = false; do{ System.out.print("Please enter your choice: "); menu = scan.nextInt(); System.out.println(); switch(menu) { case 1: System.out.print("Quantity: "); int q1 = scan.nextInt(); System.out.print("Price: "); double p1 = scan.nextDouble(); double t1=p1*q1; list.add(new Product("Ikan masin",q1,p1,t1)); break; case 2: System.out.print("Quantity: "); int q2 = scan.nextInt(); System.out.print("Price: "); double p2 = scan.nextDouble(); double t2=p2*q2; list.add(new Product("Garam Masala",q2,p2,t2)); break; case 3: System.out.print("Quantity: "); int q3 = scan.nextInt(); System.out.print("Price: "); double p3 = scan.nextDouble(); double t3=p3*q3; list.add(new Product("Buttercup",q3,p3,t3)); break; case 4: System.out.print("Quantity: "); int q4 = scan.nextInt(); System.out.print("Price: "); double p4 = scan.nextDouble(); double t4=p4*q4; list.add(new Product("Mentega",q4,p4,t4));break; case 5: System.out.print("Quantity: "); int q5 = scan.nextInt(); System.out.print("Price: "); double p5 = scan.nextDouble(); double t5=p5*q5; list.add(new Product("Honey",q5,p5,t5));break; case 6: quit = true; System.out.println("Items Quantity Price Total"); for (Product s : list){ System.out.println(s.getItem()+" " +s.getQuantity()+" "+s.getPrice()+" " +s.getTotal()); sum+=s.getTotal(); } System.out.print("Customer pays: "); double pay=scan.nextDouble(); if(pay>sum){ double amt=pay-sum; System.out.println("Give Change to Customer: "+amt); } else{ System.out.println("You have to pay "+sum); System.out.print("Enter amount to pay:"); pay=scan.nextDouble(); } break; case 7: quit=true; break; default: System.out.println("Invalid Entry!"); } } while(!quit); } }
Thanks
thanks!!! you really helpful!!!
Ads