Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an applet in java that simulates a soft drink vending machine. The simulat

ID: 3860146 • Letter: W

Question

Write an applet in java that simulates a soft drink vending machine.  The simulated machine dispenses the following soft drinks:  cola, lemon-lime soda, and bottled water.  These drinks cost $0.75 each to purchase.

When the applet starts, the drink machine should have a supply of 10 of each of the drinks.  The applet should have a text field where the user can enter the amount of money he or she is giving the machine.  The user can then click on a button to select a drink to dispense.  The applet should also display the amount of change it is giving back to the user.  The applet should keep track of its inventory of drinks and inform the user if he or she has selected a drink that is out of stock.  Be sure to handle operator errors such as selecting a drink with no money entered and selecting a drink with an inadequate amount of money entered.

Your applet must be embedded in a webpage.  The webpage should have an appropriate title and description of the applet.

Explanation / Answer

import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;

public class Drink extends JApplet
{
private double balance;
private int numClicks = 0;
private JButton cola;
private JButton lemmon;
private JButton grape;
private JButton root;
private JButton water;

public void init()
{
setLayout(new FlowLayout());
String mouney = JOptionPane.showInputDialog("Enter amount: ");
double amount = Double.parseDouble(mouney);
balance = (amount - 0.75);
makeButtons();
}

private void makeButtons()
{
cola = new JButton("Cola Button");
lemmon = new JButton("Lemmon-Line Button");
grape = new JButton("Grape Soda Button");
root = new JButton("Root beer Button");
water = new JButton("bottle water Button");

cola.addActionListener(new ButtonHandler());
lemmon.addActionListener(new ButtonHandler());
grape.addActionListener(new ButtonHandler());
root.addActionListener(new ButtonHandler());
water.addActionListener(new ButtonHandler());

add(cola);
add(lemmon);
add(grape);
add(root);
add(water);

}

private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

if(e.getSource() == cola )
{
numClicks++;
if(numClicks <= 20)
JOptionPane.showMessageDialog(null, "The balance is : " + balance + " and You selected Dirnk is Cola");
else JOptionPane.showMessageDialog(null, "Out of range ");
}

if(e.getSource() == lemmon)
{
numClicks++;
if(numClicks <= 20)
JOptionPane.showMessageDialog(null, "The balance is : " + balance + " And You selected Dirnk is lemon-line soda" );
else JOptionPane.showMessageDialog(null, "Out of range ");
}

if(e.getSource() == grape )
{
numClicks++;
if(numClicks <= 20)
JOptionPane.showMessageDialog(null, "The balance is : " + balance + "And You selected Dirnk is Grape soda" );
else JOptionPane.showMessageDialog(null, "Out of range ");
}

if(e.getSource() ==root )
{
numClicks++;
if(numClicks <= 20)
JOptionPane.showMessageDialog(null, "The balance is : " + balance + " And You selected Dirnk is root");
else JOptionPane.showMessageDialog(null, "Out of range ");
}


if(e.getSource() == water )
{
numClicks++;
if(numClicks <= 20)
JOptionPane.showMessageDialog(null, "The balance is : " + balance + "And You selected bottle of water");
else JOptionPane.showMessageDialog(null, "Out of range ");
}

}
}
}


HTML Code:

<html>
<body>
<applet code = "Drink.class",width = "400" height = "215">
</applet>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote