I have program that the user picks their products in the product menu in which after they inputted the item ID and the quantity, the program will ask if they want to buy another, if YES then the transaction repeats again and asking the same question. My central question is can I replace the value in the single variable?
Scanner id = new Scanner (System.in); String products [] = {"Donut", "Ice Cream", "Kitkat", "Marshmallow", "Nutella"}; int prices [] = {25, 15, 40, 60, 150}; int number [] = {0, 1, 2 ,3 , 4}; char result [] = {'a', 'b', 'c','d'}; System.out.println(" Menu "); System.out.println("Item IDtItem NametPrice (Php)"); System.out.println(number [0]+"t"+products [0]+"tt"+prices[0]); System.out.println(number [1]+"t"+products [1]+"t"+prices[1]); System.out.println(number [2]+"t"+products [2]+"tt"+prices[2]); System.out.println(number [3]+"t"+products [3]+"t"+prices[3]); System.out.println(number [4]+"t"+products [4]+"tt"+prices[4]); System.out.println(); System.out.print("Select ID Number: "); int ID = id.nextInt(); System.out.println("Input quantity: "); int QUANTITY = id.nextInt(); System.out.println("Product ID: "+ID); int a = ID; System.out.println("Product Name: "+products[ID]); String b = products[ID]; System.out.println("Quantity: "+QUANTITY); int c = QUANTITY; System.out.println("Product Price: "+prices[ID]); int d = prices[ID]; System.out.println("Amount to be Paid: "+(QUANTITY * prices[ID])); int e = QUANTITY * prices[ID]; System.out.println("Added to cart, want to buy another? [Y] [N]"); char keys = id.next().charAt(0); switch (keys) { case 'Y': method6(); break; case 'N': System.out.println("*******************************************************************"); System.out.println("Item IDtItem NametQuantitytAmount"); System.out.println(a+"t"+b+"tt"+c+"t"+e+"t"); System.out.println("*******************************************************************"); int x = d; System.out.println("Total: "+x); System.out.print("Cash: "); int y = id.nextInt(); int z = y-x; System.out.println("Exchange: "+z); System.out.println("Do you want to create another transaction? [Y] [N]"); char condition = id.next().charAt(0); switch (condition) { case 'Y': method6(); break; case 'N': method4(); break; } break; default: System.out.println("Invalid function!"); break; }
The output must be the product id, name, quantity, and amount must be displayed in different value which shared the single variable.
Advertisement
Answer
You can use List<String>
You can use String s =id+" | " + name + " | " + quantity + " | " + amount
It is not clear what you can’t do.
But if you need you can create the object, and name its… Menu.
public class Menu { private int productId; private int quantity; private int price; private int totalPrice; private String productName; public Menu(){} public Menu(int productId,int quantity,int price,int totalPrice,String productName){ this.productId=productId; this.quantity=quantity; this.price=price; this.totalPrice=totalPrice; this.productName=productName; } //setters and getters public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProductName(int productName) { this.productName= productName; } public int getProductName() { return productName; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public int getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getTotalPrice() { return totalPrice; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; } }
and in your current class, you can make something like
Menu menu=new Menu(); System.out.print("Select ID Number: "); menu.setProductId(id.nextInt()); System.out.println("Input quantity: "); menu.setQuantity(id.nextInt()); System.out.println("Quantity: "+menu.getQuantity()); menu.setProductName(products[menu.getProductId()]) System.out.println("Product Name: "+menu.getProductName()); menu.setPrice(prices[menu.getProduct(id)]); menu.setTotalPrice(menu.getPrice()*menu.getQuantity()); System.out.println("Amount to be Paid: "+menu.getTotatPrice());