Write an applet that simulates a soft drink vending machine. The simulated machi
ID: 3557697 • 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, grape soda, root beer, and bottle water. These drinks cost $0.75 each to purchase.
When the applet starts, the drink machine should have a supply of 20 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 whether 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.
To accomplish this, you need to create a class name Drink, use a constant for the price, and have properties to hold the inventory and the type of drink.
Make a button for each soda, each button should have a different color representing the soda, this color should also be a property of the class. When there is not enough money deposited or there are no more bottles of the selection, disable the button.
Explanation / Answer
package drinkmachine;
002
003
import java.awt.BorderLayout;
004
import java.awt.Graphics;
005
import java.awt.GridLayout;
006
import java.awt.event.ActionEvent;
007
import java.awt.event.ActionListener;
008
import javax.swing.JApplet;
009
import javax.swing.JButton;
010
import javax.swing.JFrame;
011
import javax.swing.JLabel;
012
import javax.swing.JOptionPane;
013
import javax.swing.JPanel;
014
import javax.swing.JTextField;
015
016
public class MachineWindow extends JApplet
017
{
018
019
private JButton[] drinkButton;
020
private String[] titles = {"Cola","Lemon-Lime Soda","Grape Soda",
021
"Root Beer","Bottled Water"};
022
private JPanel buttonPanel;
023
private Machine_Panel machinePanel;
024
private JTextField userInput;
025
private JLabel messageLabel;
026
private JPanel panel;
027
private JButton change;
028
private JButton[] buttonsArray;
029
JFrame frame = new JFrame("");
030
031
032
033
public MachineWindow()
034
{
035
036
buildButtonPanel();
037
setSize(300,150);
038
add(panel);
039
setVisible(true);
040
041
}
042
@Override
043
public void init()
044
{
045
046
047
machinePanel = new Machine_Panel(drinkButton);
048
add(buttonPanel,BorderLayout.EAST);
049
add(machinePanel,BorderLayout.CENTER);
050
051
buildButtonPanel();
052
053
}
054
public void buildButtonPanel()
055
{
056
buttonPanel = new JPanel();
057
buttonPanel.setLayout(new GridLayout(5,1));
058
drinkButton = new JButton[5];
059
change = new JButton("Change");
060
061
for(int i=0; i<drinkButton.length; i++)
062
{
063
drinkButton[i] = new JButton(titles[i]);
064
drinkButton[i].addActionListener(new ButtonListener());
065
buttonPanel.add(drinkButton[i]);
066
067
}
068
messageLabel = new JLabel("Enter The Amount You Have: ");
069
userInput = new JTextField(10);
070
panel = new JPanel();
071
panel.add(messageLabel);
072
panel.add(userInput);
073
panel.add(change);
074
change.addActionListener(new ButtonListener());
075
change.setEnabled(true);
076
}
077
078
private class ButtonListener implements ActionListener
079
{
080
081
public void actionPerformed(ActionEvent e)
082
{
083
084
String str;
085
double amount;
086
double num=20;
087
str = userInput.getText();
088
089
// System.out.print("String " + str);
090
//amount = Double.parseDouble(str) - (double)0.75;
091
amount = num - 0.75;
092
JOptionPane.showMessageDialog(null,"You have entered:R "+ num + " Your Change is: "+ amount );
093
machinePanel.repaint();
094
095
if(drinkButton[0].isSelected())
096
{
097
System.out.println("You selected: [COLA]");
098
}
099
if(drinkButton[1].isSelected())
100
{
101
System.out.println("You selected: [LEMON-LIME SODA]");
102
}
103
if(drinkButton[2].isSelected())
104
{
105
System.out.println("You selected: [GRAPE SODA]");
106
}
107
if(drinkButton[3].isSelected())
108
{
109
System.out.println("You selected: [ROOT BEER]");
110
}
111
if(drinkButton[4].isSelected())
112
{
113
System.out.println("You selected: [BOTTLED WATER]");
114
}
115
}
116
}
117
118
// @Override
119
// public void paintComponents(Graphics g)
120
// {
121
// super.paint(g);
122
//// if(buttonsArray[0].isSelected())
123
//// {
124
//// System.out.println("You selected: [COLA]");
125
//// }
126
//// if(buttonsArray[1].isSelected())
127
//// {
128
//// System.out.println("You selected: [LEMON-LIME SODA]");
129
//// }
130
//// if(buttonsArray[2].isSelected())
131
//// {
132
//// System.out.println("You selected: [GRAPE SODA]");
133
//// }
134
//// if(buttonsArray[3].isSelected())
135
//// {
136
//// System.out.println("You selected: [ROOT BEER]");
137
//// }
138
//// if(buttonsArray[4].isSelected())
139
//// {
140
//// System.out.println("You selected: [BOTTLED WATER]");
141
//// }
142
// }
143
144
145
146
}
/*
02
* To change this template, choose Tools | Templates
03
* and open the template in the editor.
04
*/
05
06
package drinkmachine;
07
08
import java.awt.Color;
09
import java.awt.Dimension;
10
import java.awt.Graphics;
11
import javax.swing.JButton;
12
import javax.swing.JPanel;
13
14
15
public class Machine_Panel extends JPanel
16
{
17
private JButton[] buttonsArray;
18
// private double money;
19
// private double change;
20
// private double amountEntered;
21
22
23
public Machine_Panel(JButton[] bArray)
24
{
25
buttonsArray = bArray;
26
27
setBackground(Color.WHITE);
28
setPreferredSize(new Dimension(300,200));
29
30
31
}
32
// @Override
33
// public void paintComponents(Graphics g)
34
// {
35
// super.paintComponent(g);
36
//
37
//
38
// if(buttonsArray[0].isSelected())
39
// {
40
// change = amountEntered-0.75;
41
// }
42
// if(buttonsArray[1].isSelected())
43
// {
44
//
45
// }
46
// if(buttonsArray[2].isSelected())
47
// {
48
//
49
// }
50
// if(buttonsArray[3].isSelected())
51
// {
52
//
53
// }
54
// if(buttonsArray[4].isSelected())
55
// {
56
//
57
// }
58
// }
59
60
61
//}
package drinkmachine;
002
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.