So I did a vending machine project for Java class but it isn't working correctly. It gives the total amount of change inserted into the machine, but the problem lies in that it doesn't do the math. The % isn't giving my change remaining. If someone could copy it and run it you would see my issue. Not sure if I need to send class data.
This is the code that isn't running correctly.
Code
centsRemaining = totalCents - priceOfCandy;
VendingMachineProject.JavaCode
/**
* @(#)VendingMachineProject.java
*
*
* @Michael Anderson
* @version 1.00 2013/2/13
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class VendingMachineProject extends JFrame
implements ActionListener {
private JButton button;
public static void main(String[] args) {
VendingMachineProject frame = new VendingMachineProject();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );
button = new JButton("Insert currency here.");
window.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
int totalCents, priceOfCandy;
//Total amount put in, price of candy overall.
int dollars, quarters, dimes, nickles, pennies;
//Amounts in dollars (100), quarters (25), dimes (10), nickles (5), pennies (1).
int centsRemaining;
String totalCentsString ,candyString;
totalCentsString = JOptionPane.showInputDialog(
"Enter your amount in change. I.E. $1.35 is 135.");
totalCents = Integer.parseInt(totalCentsString);
candyString = JOptionPane.showInputDialog(
"Enter the price of candy. I.E. Candy is = 0.50 cents or 50.");
priceOfCandy = Integer.parseInt(candyString);
centsRemaining = totalCents - priceOfCandy;
System.out.println( " priceOfCandy \n " + priceOfCandy);
dollars = centsRemaining / 100;
centsRemaining = totalCents % 100;
//Used to find amount in dollars.
quarters = centsRemaining / 25;
centsRemaining = totalCents % 25;
//Used to find amount in quarters.
dimes = centsRemaining / 10;
centsRemaining = totalCents % 10;
//Used to find amount in dimes.
nickles = centsRemaining / 5;
centsRemaining = totalCents % 5;
//Used to find amount in nickles.
pennies = centsRemaining / 1;
centsRemaining = totalCents % 1;
//Used to find amount in pennies.
JOptionPane.showMessageDialog(null,
totalCentsString + " cents breaks down into:\n" +
dollars + " dollars\n" +
quarters + " quarters\n" +
dimes + " dimes\n" +
nickles + " nickles\n" +
pennies + " pennies\n" +
centsRemaining + " cents.");
}
}
VendingMachineProject.ClassCode
Êþº¾
totalCents
SourceFile
quarters
setVisible
¶
¸
l6
p6 l6p6 l6p6 »
¶
Not sure if this helps. I'll input 50 cents and the program will read 50 cents. I'll put price of candy 25 cents and it'll read 25 cents. The problem is it doesn't subtract priceOfCandy from totalCents. So it'll read as 2 quarters and not 1 quarter. Anyone?
This post was edited by NinjaSushi2 on Feb 13 2013 12:47pm