Site hosted by Angelfire.com: Build your free website today!

//RODA ATHEA E. BARAQUIO //CMSC22 ST-4L /*This is a program which function as a vending machine. The user should choose the product from the list of the names of the product Messages/directions which will appear will guide the user on how to use this program properly. The user should buy atmost one only per transaction. In case, he/she plans to buy a quantity . more than 1,he/she must choose that product again and just repeat the process. After the user had chosen his/her product, he/she must choose which coin should be dropped. It's either a 5-peso coin or a 10-peso coin. The user should continue dropping coins until he total price is reached. If the total amount of dropped coins is greater than or equal to total price, a message will appear that the item is already released by the machine. In order to get the item, the user should click the button get product.*/

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

public class VMachine extends JFrame implements ActionListener{

JLabel machineLabel,product,coins;

JPanel pan1, pan2;

JRadioButton coin5;

JRadioButton coin10;

JComboBox products;

JButton dropCoin;

JButton getProduct;

JButton exit;

public JList productList;

Container vmachine;

double pay;

String buy;

int bought=0;

double tprice=0.0;

public String productNames[] = {"Oreo", "Chips Ahoy", "Snickers", "Hershey's Chocolate", "Nestle Crunch", "Twix", "Butterfinger", "Pringles (Original)", "Pringles (Sour Cream 'n Onion)", "Pepsi", "7 UP","A&W Root Beer","Sprite","Coke"};

double price[] = { 10.00, 15.00, 25.00, 20.00, 20.00, 20.00, 20.00, 30.00, 30.00, 15.00, 15.00, 15.00, 15.00, 15.00};

int available[] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};

double state5=0.0;

double state10=0.0;

double total=0.0;

public VMachine(String title){

super(title);

vmachine = getContentPane();

vmachine.setBackground(Color.white);

vmachine.setLayout(new FlowLayout());

Icon machine=new ImageIcon("p1.gif");

coins =new JLabel("Coins: ");

coin5 = new JRadioButton("P5.00");

coin5.setBackground(Color.white);

coin10 = new JRadioButton("P10.00");

coin10.setBackground(Color.white);

machineLabel = new JLabel();

machineLabel.setToolTipText("Vending Machine");

machineLabel.setIcon(machine);

productList = new JList( productNames );

productList.setVisibleRowCount( 4 );

productList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );

product = new JLabel("PRODUCTS: ");

dropCoin = new JButton("DROP COIN");

getProduct = new JButton("GET PRODUCT");

exit = new JButton("EXIT");

ButtonGroup choose = new ButtonGroup();

choose.add(coin5);

choose.add(coin10);

pan1 = new JPanel();

pan1.add(machineLabel);

pan1.add(product);

pan1.add(new JScrollPane(productList));

vmachine.add(pan1);

pan2 = new JPanel(new FlowLayout());

pan2.add(coins);

pan2.add(coin5);

pan2.add(coin10);

pan2.add(dropCoin);

pan2.add(getProduct);

pan2.add(exit);

vmachine.add(pan2);

dropCoin.addActionListener(this);

getProduct.addActionListener(this);

exit.addActionListener(this);

addWindowListener(new WindowHandler());

setSize(600,600);

setVisible(true);

setResizable(false);

productList.addListSelectionListener(

new ListSelectionListener() {

public void valueChanged( ListSelectionEvent event )

{

JOptionPane.showMessageDialog(null, "Do you really want to buy "+productNames[productList.getSelectedIndex()]+"?");

buy = JOptionPane.showInputDialog(null, "Available quantity: " + available[productList.getSelectedIndex() ] + " Price: P" + price[productList.getSelectedIndex()] + " each");

int toBuy=Integer.parseInt(buy);

pay = toBuy*price[productList.getSelectedIndex()];

if (toBuy==1) {

JOptionPane.showMessageDialog(null, "You will buy " + toBuy +" " +productNames[productList.getSelectedIndex()] + " so you will pay the amount of P" + pay );

JOptionPane.showMessageDialog(null,"Which of the 2 coins will you drop? Choose from the the two coins then press DROP COIN!");

JOptionPane.showMessageDialog(null,"Drop coins until you reach the total price!");

}

else if (toBuy>1) {

JOptionPane.showMessageDialog(null,"You are only allowed to buy one item. In case, you want to buy N items, repeat the process N times.");

}

available[productList.getSelectedIndex ()] = available[productList.getSelectedIndex() ] - toBuy;

//formula for the total price the user will pay

tprice=tprice + pay;

}

}

); // end call to addListSelectionListener

}

public void actionPerformed(ActionEvent dc_gp){

Icon drop5= new ImageIcon("y2.gif");

Icon drop10= new ImageIcon("ten.gif");

Icon machine=new ImageIcon("p1.gif");

Icon rel_pepsi= new ImageIcon("p2.gif");

Icon rel_coke= new ImageIcon("c2.gif");

Icon rel_sprite= new ImageIcon("sp2.gif");

Icon rel_7up= new ImageIcon("sv2.gif");

Icon rel_aNw= new ImageIcon("a2.gif");

Icon rel_butterfinger= new ImageIcon("bf2.gif");

Icon rel_chipsAhoy= new ImageIcon("ch2.gif");

Icon rel_nestleCrunch= new ImageIcon("cr2.gif");

Icon rel_pringlesGreen= new ImageIcon("g2.gif");

Icon rel_hershey= new ImageIcon("h2.gif");

Icon rel_oreo= new ImageIcon("or2.gif");

Icon rel_pringlesRed= new ImageIcon("r2.gif");

Icon rel_snickers= new ImageIcon("sn2.gif");

Icon rel_twix= new ImageIcon("t2.gif");

if (dc_gp.getSource().equals(dropCoin)){

int ctr=0;

System.out.println("P5 (state): "+state5);

System.out.println("P10 (state): "+state10);

System.out.println("TOTAL AMOUNT: "+tprice);

System.out.println("Total amount dropped: "+total);

if (coin5.isSelected()){

machineLabel.setIcon(drop5);

JOptionPane.showMessageDialog(null," Drop 5-peso coin?");

machineLabel.setIcon(machine);

ctr++;

}

if (ctr!=0) {state5=state5+5.0;

System.out.println("state5: "+state5);

}

int cnt=0;

if (coin10.isSelected()){

machineLabel.setIcon(drop10);

JOptionPane.showMessageDialog(null," Drop 10-peso coin?");

machineLabel.setIcon(machine);

cnt++;

}

if (cnt!=0) {state10=state10+10.0;

System.out.println("state10: "+state10);

}

total=state10+state5;

System.out.println("Total amount dropped: "+total+ " Total Price: "+tprice);

int index;

index=productList.getSelectedIndex();

int quantity=Integer.parseInt(buy);

if ((total==tprice) || (total>tprice)){

for (int i=0;i

switch(index){ //chages the images, displays the released item

case 0:{machineLabel.setIcon(rel_oreo);};break;

case 1:{machineLabel.setIcon(rel_chipsAhoy);};break;

case 2:{machineLabel.setIcon(rel_snickers);};break;

case 3:{machineLabel.setIcon(rel_hershey);};break;

case 4:{machineLabel.setIcon(rel_nestleCrunch);};break;

case 5:{machineLabel.setIcon(rel_twix);};break;

case 6:{machineLabel.setIcon(rel_butterfinger);};break;

case 7:{machineLabel.setIcon(rel_pringlesRed);};break;

case 8:{machineLabel.setIcon(rel_pringlesGreen);};break;

case 9:{machineLabel.setIcon(rel_pepsi);};break;

case 10:{machineLabel.setIcon(rel_7up);};break;

case 11:{machineLabel.setIcon(rel_aNw);};break;

case 12:{machineLabel.setIcon(rel_sprite);};break;

case 13:{machineLabel.setIcon(rel_coke);};break;

default:{machineLabel.setIcon(machine);};break;

}//switch index

}//for

JOptionPane.showMessageDialog(null, " You may now get your "+productNames[productList.getSelectedIndex()]+". Thank you and enjoy!");

}//if paid

}//if drop coin

if (dc_gp.getSource().equals(getProduct)){

machineLabel.setIcon(machine);

}

if (dc_gp.getSource().equals(exit)){

System.exit(0);

}

}//end of actionPerformed

class WindowHandler extends WindowAdapter{

public void windowClosing(WindowEvent we){

System.exit(0);

}

}

public static void main(String args[]){

new VMachine("Vending Mchine");

}

}