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

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

ID: 3860288 • Letter: W

Question

Write an java 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.

Explanation / Answer

Hi ,

Here by i have shared my code to you pleas compile and execute it and share your feed back.

Code compiled sucessfully as i have not have its required tool am unable to view it.

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 BhavasDrinkMachine extends JApplet

{

int numOfCola, numOfLemon, numOfBottledWater;

private final double drinkCost = 0.75;

double tot;

String amnt;

private JButton colaButton, lemonButton, waterBottleButton;

private JLabel moneyLabel, stockLabel;

private JTextField moneyTF;

private JPanel buttonsPanel;

// init method

@Override

public void init()

{

numOfCola = 10;

numOfLemon = 10;

numOfBottledWater = 10;

setSize(455,200);

buildPanel ();

}

private void buildPanel ()

{

colaButton = new JButton ("Cola");

lemonButton = new JButton ("Lemon Lime Soda");

waterBottleButton = new JButton("Water Bottle");

moneyLabel = new JLabel("Please enter amount to purchase your desired product: ");

stockLabel = new JLabel();

moneyTF = new JTextField (8);

buttonsPanel = new JPanel ();

// set the layout

buttonsPanel.setLayout(new GridLayout (9, 117));

buttonsPanel.add(moneyLabel);

buttonsPanel.add(moneyTF);

buttonsPanel.add(stockLabel);

buttonsPanel.add(colaButton);

buttonsPanel.add(lemonButton);

buttonsPanel.add(waterBottleButton);

colaButton.addActionListener(new ButtonActionListner());

lemonButton.addActionListener(new ButtonActionListner());

waterBottleButton.addActionListener(new ButtonActionListner());

add(buttonsPanel);

}

  

private class ButtonActionListner implements ActionListener

{

public void actionPerformed (ActionEvent ae)

{

String str = ae.getActionCommand();

try

{

tot = Double.parseDouble

(moneyTF.getText());

}

catch (Exception e)

{

if(moneyTF.getText().trim().equalsIgnoreCase(""))

{

moneyTF.setText("Enter The Amount");

} else {

moneyTF.setText("Insufficient money in your account");

return;

}

}

if (tot >= drinkCost)

{

if (str.equals("Cola"))

{

if(numOfCola > 0)

{

tot = Double.parseDouble

(moneyTF.getText());

tot = tot - drinkCost;

amnt = Double.toString(tot);

numOfCola--;

} else

stockLabel.setText("The product(cola) you have selected is out of stock.");

} else if(str.equals("Lemon Lime Soda")) {

if(numOfLemon > 0)

{

tot = Double.parseDouble

(moneyTF.getText());

tot = tot - drinkCost;

amnt = Double.toString(tot);

numOfLemon--;

} else {

stockLabel.setText("The product(Lemon Lime Soda) you have selected is out of stock.");

}

} else if(str.equals("Water Bottle")) {

if(numOfBottledWater > 0)

{

tot = Double.parseDouble(moneyTF.getText());

tot = tot - drinkCost;

amnt = Double.toString(tot);

numOfBottledWater--;

} else {

stockLabel.setText("The Product(Water Bottle) you have selected is out of stock.");

}

}

moneyTF.setText(amnt);

} else {

moneyTF.setText("Insufficient money in your account.Kindly Recheck it");

return;

}

}

}

}

O/P

Successfully compiled

Output is rendered in the Applet window. Press F4 to view.

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