// filename: VendingMachine.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class VendingMachine extends Applet implements ActionListener
{
public Button coffee, orange, water, milk; // For choosing drinks
public Button small, large; // For choosing size of the drink.
public Button deposit, take; // For depositing money and taking drink.
public TextArea display; // To display prompts to the user
public TextField depositField; // For User to deposit money
public String titleMsg, guideMsg; // To display various prompts
public Cup cup;
public String drink, drinkSize; // The drink and its size.
public double price; // Price of the drink
public void init()
{
Panel buttonPanel, rightPanel; // Panel on the left and right
Panel depositPanel; // Panel to hold the "deposit" and "take" buttons
Dash dash; // To display a line separating the
// drink buttons and size buttons
cup = new Cup();
// Create all the buttons
coffee = new Button(cup.COFFEE); orange = new Button(cup.JUICE);
water = new Button(cup.WATER); milk = new Button(cup.MILK);
small = new Button("Small"); large = new Button("Large");
deposit = new Button("Deposit"); take = new Button("Take the glass");
// Create the TextArea and TextField
display = new TextArea();
depositField = new TextField();
dash = new Dash();
// Create the Panels
buttonPanel = new Panel(new GridLayout(7, 1, 10, 10));
rightPanel = new Panel(new GridLayout(3, 1, 10, 10));
depositPanel = new Panel(new GridLayout(3, 1, 10, 10));
// Layout for the Applet.
setLayout(new GridLayout(1, 2, 10, 10));
// Add the buttons to the buttonPanel, and add buttonPanel to Applet.
buttonPanel.add(coffee); buttonPanel.add(orange);
buttonPanel.add(water); buttonPanel.add(milk);
buttonPanel.add(dash); buttonPanel.add(small);
buttonPanel.add(large);
add(buttonPanel);
// Add the appropriate components to the depositPanel
depositPanel.add(depositField); depositPanel.add(deposit);
depositPanel.add(take);
// Add the display, cup and depositPanel to the rightPanel
rightPanel.add(display); rightPanel.add(cup);
rightPanel.add(depositPanel);
add(rightPanel);
// User cannot edit the TextField.
// It is for prompts generated by the system.
display.setEditable(false);
// Add actionListeners for the buttons.
coffee.addActionListener(this); orange.addActionListener(this);
water.addActionListener(this); milk.addActionListener(this);
small.addActionListener(this); large.addActionListener(this);
deposit.addActionListener(this); take.addActionListener(this);
// Display the title.
titleMsg = new String("CTE SSD1 - SUPER SOFT DRINK ONE");
// Set the initial state: the user should be able to select the drink.
selectDrink();
}
// Ask the user to select the drink.
public void selectDrink()
{
// The drink buttons should be enabled, and others disabled.
coffee.setEnabled(true); orange.setEnabled(true);
water.setEnabled(true); milk.setEnabled(true);
small.setEnabled(false); large.setEnabled(false);
deposit.setEnabled(false); depositField.setEnabled(false);
take.setEnabled(false);
// The cup should be empty.
cup.setDrinkSize(cup.EMPTY); cup.fill();
// Prompt the user to selet the drink.
guideMsg = "Please choose your drink.";
display.setText(titleMsg + "\n" + guideMsg);
}
// Ask the user to select the drink size.
public void selectDrinkSize(String dr)
{
drink = dr;
// Get the price of the drink.
price = cup.setDrink(drink);
// Disable the drink buttons, and enable the size buttons.
coffee.setEnabled(false); orange.setEnabled(false);
water.setEnabled(false); milk.setEnabled(false);
small.setEnabled(true); large.setEnabled(true);
guideMsg = "Please choose the size of your " + drink + ".";
display.setText(titleMsg + "\n" + guideMsg);
}
// Ask the user to deposit money
public void deposit(String size)
{
drinkSize = size;
// If the size is Large, double the price.
if (drinkSize.equals (cup.LARGE)) {
price = 2 * price;
}
small.setEnabled(false); large.setEnabled(false);
deposit.setEnabled(true); depositField.setEnabled(true);
guideMsg = "Please deposit " + price + " dollars.";
display.setText(titleMsg + "\n" + size + " " + drink + "\n" + guideMsg);
}
// Ask the user to take the drink
public void takeDrink()
{
Double amount;
amount = new Double(depositField.getText());
// See if the deposited amount is at least equal to the cost of the drink.
// If so, fill the cup, and ask the user to take the drink.
// Or else, ask the user to deposit sufficient amount.
if (amount.doubleValue() >= price) {
cup.setDrinkSize(drinkSize);
cup.fill();
display.setText(titleMsg+"\n"+"Please take your drink.");
depositField.setEnabled(false); depositField.setText("");
deposit.setEnabled(false); take.setEnabled(true);
}
else display.setText(titleMsg+"\n"+"Insufficient amount.\n"+
"Please deposit "+price+" dollars.");
}
// Respond to button clicks.
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
// If the user selcts the drink, next ask to to select the size.
if (action.equals(coffee.getLabel())) selectDrinkSize(cup.COFFEE);
else if (action.equals(orange.getLabel())) selectDrinkSize(cup.JUICE);
else if (action.equals(water.getLabel())) selectDrinkSize(cup.WATER);
else if (action.equals(milk.getLabel())) selectDrinkSize(cup.MILK);
// If the user has chosen the size, ask them to deposit money
else if (action.equals(large.getLabel())) deposit(cup.LARGE);
else if (action.equals(small.getLabel())) deposit(cup.SMALL);
// If the user has deposited sufficient money, ask them to take the drink
else if (action.equals(deposit.getLabel())) takeDrink();
// If the user has taken the drink, go back to the initial state
// and ask the user to select the drink
else if (action.equals(take.getLabel())) selectDrink();
}
} // end of class definition for VendingMachine.
//filename: Dash.java
import java.awt.*;
public class Dash extends Component
{
public void paint(Graphics g)
{
int height, width;
height = this.getSize().height;
width = this.getSize().width;
g.fillRect(0, (height - 5)/2, width, 5);
}
}
//filename: Cup.java
import java.awt.*;
public class Cup extends Component
{
// The drinks
public String COFFEE = "Coffee", JUICE = "Orange Juice";
public String WATER = "Water", MILK = "Milk";
// The size
public String EMPTY = "Empty", SMALL = "Small", LARGE = "Large";
// The increment in which the cup should be filled to illustrate animation.
public int FILLSTEPS = 100;
// The drink chosen by the user.
public String drink = "None";
// The level to which the cup should be filled -- depends
// on the size chosen by the user.
public int level = 0;
public void paint(Graphics g)
{
int height, width, length, depth;
int x, y;
// Get the area available to display the cup.
height = this.getSize().height;
width = this.getSize().width;
// Use half the height available and
// one third of the width available to draw the cup.
depth = height/2; length = width/3;
x = (width - length)/2; y = (height - depth)/2;
setBackground(Color.lightGray);
// draw the empty glass with a bottom and two sides.
g.setColor(Color.black);
g.fillRect(x, y, 2, depth);
g.fillRect(x, y + depth - 4, length, 4);
g.fillRect(x + length - 2, y, 2, depth);
// pick the color of the drink
if (drink.equals ("Coffee")) g.setColor(Color.darkGray);
else if (drink.equals ("Orange Juice")) g.setColor(Color.orange);
else if (drink.equals ("Water")) g.setColor(Color.blue);
else if (drink.equals ("Milk")) g.setColor(Color.lightGray);
// fill the glass
if (level > 0)
fillCup(g, x + 2, y + depth - 4, depth - 4, length - 4);
}
private void fillCup(Graphics g, int x, int y, int depth, int length)
{
int i, j, fillLevel;
// fill the glass up to the appropriate level.
for (i = 1; i < level; i = i + 1) {
fillLevel = i * depth / FILLSTEPS;
// Fill the cup with 10 waves on top,
// and fill the bottom with a solid rectangle.
drawWaves(g, x, y - fillLevel, length, depth);
g.fillRect(x, y - fillLevel, length, fillLevel);
// Pause a while to make the animation visible to the human eye.
for (j = 0; j < 10000000 / FILLSTEPS; j = j + 1);
}
fillLevel = level * depth / FILLSTEPS;
g.fillRect(x, y - fillLevel, length, fillLevel);
drawWaves(g, x, y - fillLevel, length, depth);
}
// Draw the waves on top of the drink.
public void drawWaves(Graphics g, int x, int y, int length, int depth)
{
int j;
// Draw 10 arcs
for (j = 0; j < 10; j = j + 1)
g.fillArc(x + j * length / 10, y - depth / 20,
length / 10, depth / 10, 0, 180);
}
// Remember the drink chosen by the user. Return the price of the drink.
public double setDrink(String drink)
{
double price = 0;
this.drink = drink;
if (drink.equals (COFFEE)) { price = 1; }
else if (drink.equals (JUICE)) { price = 1.5; }
else if (drink.equals (WATER)) { price = 0.5; }
else if (drink.equals (MILK)) { price = 1; }
return price;
}
// Remember the size of the drink selected by the user.
public void setDrinkSize(String size)
{
// Based on the drink size, set the level to which the cup
// should be filled.
if (size.equals (SMALL)) level = FILLSTEPS/2;
else if (size.equals (LARGE)) level = 4 * FILLSTEPS/5;
else level = 0;
}
// To fill the cup, call repaint.
// The browser will then call our paint method.
public void fill()
{
repaint();
}
}