guys, I'm working on an assignment for class that involves a vending machine application..... console (no gui) here's the link to project, there are a few things, that I'm having problem implementing like for example I use a "switch case" but I can't get input from the menu in letters, b/c "switch case" only works with int, and also I'm having problem with the "add coins", "buy", "remove coins" methods, I've decided to put all the data into an ArrayList in the for of String, and that's a problem when working with numbers like the price...... could someone take a look at my code and give me some pointers..... here's what I've got so far
public class VendingMachine { String product = ""; String price = ""; String qty = ""; //constructing vending machine that takes product, price and qty public VendingMachine(String product, String price, String qty) { this.product = product; this.price = price; this.qty = qty; } //getters public String getProduct() { return product; } public String getPrice() { return price; } public String getQty() { return qty; } @Override public String toString() { return product+" @"+price; } }
import java.util.*; public class VendingMachineSimulation { public static void main(String [] args) { ArrayList<VendingMachine> vMachine = new ArrayList<VendingMachine>(); Scanner myscan = new Scanner(System.in); vMachine.add(new VendingMachine("Pepsi Cola", "1.00", "10")); System.out.println("SELECT FROM MENU!"); System.out.println("1)show product 2)insert coin 3)buy 4)Add product 5)remove coins 6)quit"); int key = myscan.nextInt(); switch (key) { case 1: for (int i = 0; i < vMachine.size(); i++) { System.out.println(vMachine.get(i)); }break; case 2: case 3: case 4: { System.out.println("Description:"); String description = myscan.next(); System.out.println("Price:"); String price = myscan.next(); System.out.println("Quantity:"); String qty = myscan.next(); vMachine.add(new VendingMachine(description, price, qty)); }break; case 5: case 6: } } }
Thanks in advance