Write an applet that simulates a soft drink vending machine. The simulated machi
ID: 3817981 • 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. Be sure to follow the instructions on “How to Create and Deploy an Applet” to add your webpage to your project. Export your project as usual for submission.
//Java program
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 LeavinesDrinkMachine extends JApplet
{
// components and variables needed
int numberOfCola, numberOfLemon, numberOfBottledWater;
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;
// init method
@Override
public void init()
{
// number of drinks set to 10
numberOfCola = 10;
numberOfLemon = 10;
numberOfBottledWater = 10;
setSize(455,200);
// call to buildPanel method
buildPanel ();
}
// buildPanel method
private void buildPanel ()
{
// the button components
colaButton = new JButton ("Cola");
lemonLimeSodaButton = new JButton ("Lemon Lime Soda");
waterBottleButton = new JButton("Water Bottle");
// the label components
moneyLabel = new JLabel("Please enter $0.75 to purchase the desired product: ");
stockLabel = new JLabel();
// the text field component
moneyTF = new JTextField (8);
// the panel
buttonsPanel = new JPanel ();
// set the layout
buttonsPanel.setLayout(new GridLayout (9, 117));
// components to the panel added
buttonsPanel.add(moneyLabel);
buttonsPanel.add(moneyTF);
buttonsPanel.add(stockLabel);
buttonsPanel.add(colaButton);
buttonsPanel.add(lemonLimeSodaButton);
buttonsPanel.add(waterBottleButton);
// the action listener
colaButton.addActionListener(new ButtonActionListner());
lemonLimeSodaButton.addActionListener(new ButtonActionListner());
waterBottleButton.addActionListener(new ButtonActionListner());
// add the panel
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)
{
// TODO: handle exception
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(numberOfCola > 0)
{
total = Double.parseDouble
(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
numberOfCola--;
}
else
stockLabel.setText("Sorry, Cola is out of stock.");
}
else if(str.equals("Lemon Lime Soda"))
{
if(numberOfLemon > 0)
{
total = Double.parseDouble
(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
numberOfLemon--;
}
else
{
stockLabel.setText("Sorry, Lemon Lime Soda is out of stock.");
}
}
else if(str.equals("Water Bottle"))
{
if(numberOfBottledWater > 0)
{
total = Double.parseDouble(moneyTF.getText());
total = total - drinkCost;
amount = Double.toString(total);
numberOfBottledWater--;
}
else
{
stockLabel.setText("Sorry, Water Bottle is out of stock.");
}
}
moneyTF.setText(amount);
}
else
{
moneyTF.setText("Sorry, insufficient money");
return;
}
}
}
}
<!--HTML-->
<html>
<head>
<title> Leavines Drink Machine </title>
</head>
<body>
<center>
<h1>Ward's Drinking Machine</h1>
</center>
<center>
<applet code="LeavinesDrinkMachine.class" height="200" width="500">Applet</applet>
</center>
</body>
</html>
Explanation / Answer
import.java.util.scanner;
open class VendingMachine
{
open static void primary (String args[])
{
int[4] coin = {5,5,5,5};//make a cluster to hold coin values
int[4] drink = {5,5,5,5};/make a cluster to hold drink values
int decision;
scorch cont;
/String input1, input2, input3;
twofold cost = 1.2, adjust = 0, coinInsert = 0, sumCoininsert = 0;
showmenu(coin, drink);
sumCoininsert = inputcoin(coinInsert);
showdrinks(choice);
showbalace(sumCoininsert, cost);
contmethod(cont);
}
/*****************showmenu method*****************/
open static int showmenu (int coin, int drink)
{
int coin;
int drink;
System.out.println("[Coins accessible : RM 1 [" + coin[0] + "] RM0.50 [ " + coin[1] + "] RM 0.20 [ " + coin[2] + "] RM0.10 [" +coin[3] "]");
System.out.println("[Drinks accessible : Pepsi [" + drink[0] + "] Coke [" + drink[1] + "] Mirinda Orange [" + drink[2] + "] 7up [" + drink[3] + "]");
}
/********************showmenu method*****************/
/**************inputcoin strategy *******************/
open static twofold inputcoin (twofold coinInsert)
{
twofold coinInsert = 0, sumCoininsert = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Insert Coin==>");
twofold coinInsert = sc.nextDouble();
while (coinInsert == "1" || coinInsert == "0.5" || coinInsert == "0.2" || coinInsert == '0.1')
{
coinInc(coinInsert);
sumCoininsert = sumCoininsert + coinInsert;
while (sumCoinInsert < 1.20)
{
System.out.println("Insert Coin==>");
twofold coinInsert = sc.nextDouble();
}
}
System.out.println (" Total :" +sumCoinInsert);
}
/********************inputcoin method*******************/
/***************coinInc method************************/
open static twofold coinInc(int coinInsert)
{
twofold coinInsert;
Scanner sc = new Scanner(System.in);
switch (coinInsert) {
case coinInsert == '1':
coin[0]++;
break;
case coinInsert == '0.5':
coin[1]++;
break;
case coinInsert == '0.2':
coin[2]++;
break;
case coinInsert == '0.1':
coin[3]++;
break;
default:
System.out.println("Invalid coin");
sumCoininsert = 0;
System.out.println("Insert Coin==>");
twofold coinInsert = sc.nextDouble();
break;
}
}
/******************coinInc method*********************/
/*****************showdrinks technique **************/
open static int showdrinks (int decision)
{
int decision;
System.out.println("Drinks [RM1.20]");
System.out.println("1.Pepsi" " 2.Coke" " 3.Mirinda Orange" " 4.7up");
Scanner sc = new Scanner(System.in);
System.out.println("Choice==>");
int decision = sc.nextInt();
while (decision == "1" || decision == "2" || decision == "3" || decision == '4')
{
on the off chance that (decision =='1')
{
drink[0]- - ;
}else if (choice=='2')
{
drink[1]- - ;
}else if (decision =='3')
{
drink[2]- - ;
}else (decision == '4')
{
drink[3]- - ;
}
}
System.out.println("Invalid decision");
System.out.println("Choice==>");
int decision = sc.nextInt();
}
/*****************showdrinks technique **************/
/****************showbalance method*******************/
open static twofold showbalance(double sumCoininsert, twofold cost)
{
twofold cost = 1.2, adjust = 0, sumCoininsert;
adjust = sumCoininsert - cost;
on the off chance that (adjust != 0.00)
{
countbalance (adjust);
System.out.println(" Your adjust is :" +balance)
System.out.println(" :" +fifty+ "x50" " +twenty+ "x20" " +tenth+ "x10" );
}else/if (adjust == 0.00)
{
System.out.println("Your adjust is :" +balance);
}
}
/****************showbalance method*******************/
/****************countbalance method******************/
open static twofold countbalance(double adjust)
{
twofold adjust;
int ibalance = (int)(balance);
int fifty = ibalance/50;
int cfifty = ibalance%50;
int twenty = cfifty/20;
int ctwenty = cfifty%20;
int tenth = ctwenty/10;
int ctenth = ctwenty%10;
on the off chance that ( fifty != 0)
{
coin[1]- - ;
}
on the off chance that (twenty != 0)
{
coin[2]- - ;
}
on the off chance that (tenth != 0)
{
coin[3]- - ;
}
}
/*************countbalance method*********************/
/************************contmethod*****************************/
open static String contmethod (singe cont)
{
singe cont;
Scanner sc = new Scanner(System.in);
System.out.println ("Do you need to proceed? y/n :");
/input3 = in.readline();
cont = sc.charAt(0);
while (cont == "y" || cont == 'Y')
{
proceed ;
}
System.out.println("Thank You, Please come again.");
System.exit(0);
}
}
/****************contmethod*******************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.