I'm making a vending machine .. and everything's pretty much together, I just have a few problems that I don't have the faintest clue how to fix
Firstly, when I choose a snack, the price of the thing comes up fine, no bother. But if I start to pay, then it doesn't count down how much left I have to pay, although the program itself still operates (quite badly, it doesn't calculate how much has been put into the machine and how much change to give etc.. )
Also when I get the box telling me how much change I have .. the price comes in -£0.00 instead of £0.00 .. I have no idea why this is either.
and finally .. I want the money buttons disabled when I first run the program, then when I make a selection I want them enabled, and the selection buttons disabled.. as far as I can see, the code should do that for me, but it just won't compile.
Any help would be much appreciated, thanks ... here is my code:
import static javax.swing.JOptionPane.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import java.text.DecimalFormat;
public class VendingMachine extends JFrame {
JButton oneP, twoP, fiveP, tenP, twentyP, fiftyP, hundredP, twohundredP;
JButton gingbisc, toffdel, jumcrisp, cashnut, omgchoc;
JButton resetButton;
JLabel messLabel = new JLabel("Amount to pay: ");
JTextField message = new JTextField(4);
double amount = 0;
DecimalFormat pounds = new DecimalFormat("£0.00");
public static void main(String[] args) {
VendingMachine c = new VendingMachine();
c.setTitle("Vending Machine");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(800, 600);
c.setVisible(true);
}
public VendingMachine() {
message.setText(pounds.format(amount));
//Configuring the left side button icons
setLayout(new BorderLayout());
oneP = new JButton("1p");
oneP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.01);
}});
twoP = new JButton("2p");
twoP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.02);
}});
fiveP = new JButton("5p");
fiveP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.05);
}});
tenP = new JButton("10p");
tenP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.1);
}});
twentyP = new JButton("20p");
twentyP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.2);
}});
fiftyP = new JButton("50p");
fiftyP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(0.5);
}});
hundredP = new JButton("£1");
hundredP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(1);
}});
twohundredP = new JButton("£2");
twohundredP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertMoney(2);
}});
// Making the icons
ClassLoader cldr = this.getClass().getClassLoader();
gingbisc = new JButton("Ginger Biscuits", new ImageIcon(cldr.getResource("ginger.jpg")));
toffdel = new JButton("Toffee Delight", new ImageIcon(cldr.getResource("toffee.jpg")));
jumcrisp = new JButton("Jumbo Crisps", new ImageIcon(cldr.getResource("crisps.jpg")));
cashnut = new JButton("Cashew Nuts", new ImageIcon(cldr.getResource("cashews.jpg")));
omgchoc = new JButton("Omega Chocolate", new ImageIcon(cldr.getResource("chocolate.jpg")));
// Configuring the right side button icons
gingbisc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
debitAmount(0.5);
}});
toffdel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg1) {
debitAmount(0.7);
}});
jumcrisp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg2) {
debitAmount(1.0);
}});
cashnut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg3) {
debitAmount(1.2);
}});
omgchoc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg4) {
debitAmount(1.5);
}});
// Left side panel components
JPanel leftSide = new JPanel();
leftSide.setLayout(new GridLayout(8, 1));
leftSide.add(oneP);
leftSide.add(twoP);
leftSide.add(fiveP);
leftSide.add(tenP);
leftSide.add(twentyP);
leftSide.add(fiftyP);
leftSide.add(hundredP);
leftSide.add(twohundredP);
add("West", leftSide);
// Right side panel components
JPanel rightSide = new JPanel();
rightSide.setLayout(new GridLayout(5, 1));
rightSide.add(gingbisc);
rightSide.add(toffdel);
rightSide.add(jumcrisp);
rightSide.add(cashnut);
rightSide.add(omgchoc);
add("East", rightSide);
JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(messLabel);
message.setEditable(false);
middle.add(message);
add("Center", middle);
}
public void debitAmount(double pence) {
amount = amount + pence;
message.setText(pounds.format(amount));
}
public void insertMoney(double pence) {
amount = amount - pence;
message.setText(pounds.format(amount));
if (amount > 0) message.setText(pounds.format(amount / 100.0));
else {
message.setText("");
if (amount < 0) {
showMessageDialog(this, "Your change is "
+ pounds.format(amount),
"Change", JOptionPane.INFORMATION_MESSAGE);
} else {
showMessageDialog(this, "Thank you",
"Exact amount", JOptionPane.INFORMATION_MESSAGE);
}
// Enabling thhe left buttons, disabling the right buttons
void leftSide() {
oneP.setEnabled(true);
twoP.setEnabled(true);
fiveP.setEnabled(true);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
hundredP.setEnabled(true);
twohundredP.setEnabled(true);
gingbisc.setEnabled(false);
toffdel.setEnabled(false);
jumcrisp.setEnabled(false);
cashnut.setEnabled(false);
omgchoc.setEnabled(false);
}
// Enabling the right buttons, disabling the left buttons
void rightSide() {
oneP.setEnabled(false);
twoP.setEnabled(false);
fiveP.setEnabled(false);
tenP.setEnabled(false);
twentyP.setEnabled(false);
fiftyP.setEnabled(false);
hundredP.setEnabled(false);
twohundredP.setEnabled(false);
gingbisc.setEnabled(true);
toffdel.setEnabled(true);
jumcrisp.setEnabled(true);
cashnut.setEnabled(true);
omgchoc.setEnabled(true);
}
}
}
}