Write an applet that simulates a soft drink vending machine. The simulated machi
ID: 3860846 • Letter: W
Question
Write an applet 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.
NOTE: Your applet must be embedded in a webpage. The webpage should have an appropriate title and description of the applet. Feel free to add color and excitement to your webpage.
Explanation / Answer
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DrinkMachhine extends JApplet
{
int no.Cola, no.Lemon, no.BottledWater;
private final double drinkCost = 0.75;
double total;
String amount;
private JButton colaButton, lemonLimeSodaButton, waterBottleButton;
private JLabel moneyLabel, stockLabel;
private JTextField moneyTF;
private JPanel buttonsPanel;
@Override
public void init()
{
no.Cola = 10;
no.Lemon = 10;
no.BottledWater = 10;
setSize(455,200);
buildPanel ();
}
private void buildPanel ()
{
colaButton = new JButton ("Cola");
lemonLimeSodaButton = new JButton ("Lemon Lime Soda");
waterBottleButton = new JButton("Water Bottle");
moneyLabel = new JLabel("Please enter $0.75 to purchase the desired product: ");
stockLabel = new JLabel();
moneyTF = new JTextField (8);
buttonsPanel = new JPanel ();
buttonsPanel.setLayout(new GridLayout (9, 117));
buttonsPanel.add(moneyLabel);
buttonsPanel.add(moneyTF);
buttonsPanel.add(stockLabel);
buttonsPanel.add(colaButton);
buttonsPanel.add(lemonLimeSodaButton);
buttonsPanel.add(waterBottleButton);
colaButton.addActionListener(new ButtonActionListner());
lemonLimeSodaButton.addActionListener(new ButtonActionListner());
waterBottleButton.addActionListener(new ButtonActionListner());
add(buttonsPanel);
}
private class ButtonActionListner implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
String str = ae.getActionCommand();
try {
total = Double.parseDouble
(moneyTF.getText());
} catch (Exception e)
{
if(moneyTF.getText().trim().equalsIgnoreCase(""))
{
moneyTF.setText("Enter The Money");
}
else
{
moneyTF.setText("Sorry, insufficient money");
return;
}
}
if (total >= drinkCost)
{
if (str.equals("Cola"))
{
if(no.Cola > 0)
{
total = Double.parseDouble
(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
no.Cola--;
}
else
stockLabel.setText("Sorry, Cola is out of stock.");
}
else if(str.equals("Lemon Lime Soda"))
{
if(no.Lemon > 0)
{
total = Double.parseDouble
(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
no.Lemon--;
}
else
{
stockLabel.setText("Sorry, Lemon Lime Soda is out of stock.");
}
}
else if(str.equals("Water Bottle"))
{
if(no.BottledWater > 0)
{
total = Double.parseDouble(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
no.BottledWater--;
}
else
{
stockLabel.setText("Sorry, Water Bottle is out of stock.");
}
}
moneyTF.setText(amount);
}
else
{
moneyTF.setText("Sorry, insufficient money");
return;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.