0

ive doing a homework which we need to provide buttons (GUI) for a vending machine.
the vending machine takes dollars, quaters, dimes, and nickels, and the vending machine has drinks (60 cents) and snacks (45 cents).

we done a code were it does work through "SCANNER", but now we have to create a GUI for it.

/**
* VendingMachineCUI: A program that emulates a vending machine. It uses a 
* VendingMachine object to represent a vending machine, and interacts with 
* the user through the console.
*/ 

import java.util.*;        // for Scanner class



public class VendingMachineCUI
{
    public static void main (String[] args)
    {
        char    action;                // Store user's input characters
        String    strDrinkAvailable,     // whether drink is sold out
                strSnackAvailable;    // whether snack is sold out
        Change    change;                // Used to keep a change
        
        // Create a Scanner object for input
        Scanner keyboard = new Scanner (System.in);
        
        // Create a vending machine object
        VendingMachine vm = new VendingMachine ();
        
        while (true)
        {
            if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
                strDrinkAvailable = "";
            else
                strDrinkAvailable = " (sold out)";
            
            if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
                strSnackAvailable = "";
            else
                strSnackAvailable = " (sold out)";
            
            // Display the menu
            System.out.println (
                "A - Drink" + strDrinkAvailable + "\n" + 
                "B - Snack" + strSnackAvailable + "\n\n" + 
                
                "D - Enter a dollar\n" + 
                "Q - Enter a quarter\n" + 
                "M - Enter a dime\n" + 
                "N - Enter a nickel\n\n" + 
                
                "R - Return change\n\n");
            
            // Display the amount entered in the machine
            System.out.println ("Amount available: " + vm.getAmountAvailable () + "\n\n");
            
            // Get user's action
            System.out.print ("Your action: ");
            action = keyboard.next ().charAt (0);
            action = Character.toUpperCase (action);
            
            // Process
            switch (action)
            {
            case 'A':
                if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
                    System.out.println ("Sorry, it's sold out");
                else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_DRINK))
                    System.out.println ("Sorry, not enough money for the purchase");
                else if (vm.buyItem (VendingMachine.ITEM_TYPE_DRINK))
                    System.out.println ("Here is your drink");
                break;
            case 'B':
                if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
                    System.out.println ("Sorry, it's sold out");
                else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_SNACK))
                    System.out.println ("Sorry, not enough money for the purchase");
                else if (vm.buyItem (VendingMachine.ITEM_TYPE_SNACK))
                    System.out.println ("Here is your snack");
                break;
            case 'D':
                vm.inputDollar ();
                break;
            case 'Q':
                vm.inputQuarter ();
                break;
            case 'M':
                vm.inputDime ();
                break;
            case 'N':
                vm.inputNickel ();
                break;
            case 'R':
                change = vm.makeChange ();
                
                // Display the coins to the user 
                System.out.println (
                    "# of quarters: " + change.getQuarters () + "\n" + 
                    "# of dimes: " + change.getDimes () + "\n" + 
                    "# of nickles: " + change.getNickels () + "\n");
                
                break;
            
            default:
                System.out.println ("Sorry, " + action + " is an invalid option");
                break;        // simply do nothing for a invalid input    
            
            }    // end of switch
            
            System.out.println ("\n");        // get a vertical space
            
                    
        } // end of while
    }
}

thats the scanner one, it does work (since i have other programs which displays the change, and everyything else).

but i dont know where to start...

ive done one program with GUI, and was still strugglin about it.

i know the basics, such as...

inputing

import java.awt.*;         
import java.awt.event.*;

to activate the whole "event" thing through the eAction..

any help will be appreciateed...

thanks.

2
Contributors
1
Reply
5
Views
8 Years
Discussion Span
Last Post by peter_budo
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.