//CokeMachine.java //David Root //Programming II //Project 2 //1 DEC 99 import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; import java.text.*; public class CokeMachine extends Applet implements ActionListener { /*Coke machine will simulate a soda machine, with buttons for Coke, Rootbeer and gingerale. Also, there will be a coin return button, and textfields for total coins deposited and what soda is distributed. */ private TextField txtPrice, txtCoin, txtReturn, txtCanDrop, txtTotalDeposit; private Button[] sodaButtons; private Button btnCoinReturn; private String[] soda = {"COKE","GINGER-Ale","ROOT BEER","empty","empty"}; private int select=1; private boolean valid=false; static int totalCoin=0; static int coin=0; static int price=60; static String coinString; //Set up the layout for the panels/buttons public void init() { setLayout(new BorderLayout()); // Put coin slot, total deposited windows in Panel pn = new Panel(); pn.setLayout(new FlowLayout()); txtTotalDeposit = new TextField(15); txtTotalDeposit.setEditable(false); txtTotalDeposit.setText("Price ."+price); pn.add(new Label("TOTAL:")); pn.add(txtTotalDeposit); txtCoin = new TextField(5); txtCoin.setEditable(true); txtCoin.addActionListener(this); pn.add(new Label("COIN SLOT:")); pn.add(txtCoin); add("North", pn); //Add the drink selection buttons Panel pc = new Panel(); pc.setLayout(new GridLayout(3,2)); sodaButtons = new Button[soda.length]; for (int i=0; i < soda.length; i++) { sodaButtons[i] = new Button(soda[i]); sodaButtons[i].addActionListener(this); pc.add(sodaButtons[i]); } //Add the Coin Return Button btnCoinReturn = new Button("COIN RETURN"); btnCoinReturn.addActionListener(this); pc.add(btnCoinReturn); add ("Center", pc); //Add the can distribution field Panel ps = new Panel(); ps.setLayout(new FlowLayout()); txtCanDrop = new TextField(15); txtCanDrop.setEditable(false); ps.add(new Label("CAN SLOT:")); ps.add(txtCanDrop); //Add the Change return txtReturn = new TextField(5); txtReturn.setEditable(false); ps.add(new Label("Coin Return:")); ps.add(txtReturn); add ("South",ps); } //Look for apecific action, coin drop, coin return, etc. public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == txtCoin) { System.out.println("at txtCoin"); //debug comment saleReset(); validate(); if (valid) calcCoin(); } else if (source == btnCoinReturn) returnMoney(); else selectSoda(e.getActionCommand()); } //Reset method prior to sale public void saleReset() { System.out.println("RESET"); //debug comment txtCanDrop.setText(""); txtReturn.setText(""); } //Reset method after sale or coin return public void postSale() { System.out.println("postSale"); //debug comment txtCoin.setText(""); txtTotalDeposit.setText("Price: ."+price); totalCoin = 0; coin = 0; valid=false; } //Validate Method to check coins deposited public void validate() { coinString = txtCoin.getText(); System.out.println("Validate"); //debug comment if(! coinString.equals("")) { coin = new Integer(coinString.trim()).intValue(); if(coin==5 | coin==10 | coin==25 | coin==100) valid=true; else { valid=false; txtCanDrop.setText("Invalid Coin"); txtReturn.setText(""+coin); txtCoin.setText(""); } } } //Method to calculate coins deposited public void calcCoin() { totalCoin = totalCoin + coin; if (totalCoin==100) txtTotalDeposit.setText("1.00"); else if (totalCoin==5) txtTotalDeposit.setText(".05"); else txtTotalDeposit.setText("."+(totalCoin)); txtCoin.setText(""); System.out.println("Post Calc"); //debug comment if(totalCoin >= price) { valid = true; txtTotalDeposit.setText("MAKE SELECTION"); } } //Coin Return method public void returnMoney() { System.out.println("Coin return1"); //debug comment if (totalCoin==100) txtReturn.setText("1.00"); else if (totalCoin==5) txtReturn.setText(".05"); else txtReturn.setText("."+(totalCoin)); System.out.println("Coin return2"); //debug comment postSale(); } //Soda selection method public void selectSoda(String pop) { select = 0; while(!pop.equals(soda[select])) select++; if (! soda[select].equals("empty")) { if (totalCoin>=price) { txtCanDrop.setText(soda[select]); if (totalCoin >price) { if ((totalCoin-price)==5) txtReturn.setText(".05"); else txtReturn.setText("."+(totalCoin-price)); } postSale(); } } } }