Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: need help with Vending Machine code...

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default need help with Vending Machine code...

    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
    Last edited by mia_tech; March 6th, 2009 at 07:17 PM.


  2. #2
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Switch case implementation problem in Vendor machine code

    well, I've made a few modification, but still, missing somethings

    public class VendingMachine {
        String product = ""; 
        double price = 0.0;
        int qty = 0;
        //constructing vending machine that takes product, price and qty
        public VendingMachine(String product, double price, int qty)
        {
            this.product = product;
            this.price = price;
            this.qty = qty;
        }
     
        //getters
        public String getProduct()
        {
            return product;
        }
        public double getPrice()
        {
            return price;
        }
        public int 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));
            String key = "";
            do
            {
                System.out.println("SELECT FROM MENU!");
            System.out.println("S)how product I)nsert coin B)uy A)dd product R)emove coins Q)uit");
            key = myscan.next().toUpperCase();
     
     
                if (key.equals("S"))
                {
                    for (int i = 0; i < vMachine.size(); i++)
                            {
                                System.out.println(vMachine.get(i));
                            }break;
                }
                else if(key.equals("I"))
                {
                    //implement Insert coins
                }
                else if(key.equals("B"))
                {
                    //implement add product
                }
                else if(key.equals("A"))
                {
                    //implement add product
                    System.out.println("Description:");
                    String description = myscan.next();
                    System.out.println("Price:");
                    double price = myscan.nextDouble();
                    System.out.println("Quantity:");
                    int qty = myscan.nextInt();
                    vMachine.add(new VendingMachine(description, price, qty));
                }
                else if(key.equals("R"))
                {
                    //implement remove product
                }
                else break;
           }while(!(key.equals("Q")));
        }
    }

  3. #3

    Default Re: need help with Vending Machine code...

    About the switch case, you can use chars, for example:

     key = myscan.next().toUpperCase();
    			char c = key.charAt(0);
    			switch (c) {
    			case 'S':
    				System.out.println("show");
    				break;
    			case 'I':
    				System.out.println("insert");
    				break;
    			case 'B':
    				System.out.println("buy");
    				break;
    			default:
    				break;
    			}

    Or you can use an enumeration (you need some "wrapper") like this:


    key = myscan.next().toUpperCase();
    			Wrapper w = Wrapper.valueOf(key);
    			switch (w.ordinal()) {
    			case 0:
    				System.out.println("show");
    				break;
    			case 1:
    				System.out.println("insert");
    				break;
    			case 2:
    				System.out.println("buy");
    				break;
    			default:
    				break;
    			}
                           .................
                           private enum Wrapper {
    		           S, I, B, A, R;
    	               }

    What else do you need? Not hesitate to ask

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: need help with Vending Machine code...

    Hello mia_tech,

    Did leandro help you to fix your problem or is this issue still open?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Java error "could not create java virtual machine"
    By aubrey4444 in forum Java Theory & Questions
    Replies: 17
    Last Post: October 3rd, 2010, 12:51 PM
  2. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM
  3. Replies: 8
    Last Post: February 24th, 2009, 04:04 PM
  4. Replies: 6
    Last Post: November 14th, 2008, 03:09 PM