d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Someone To Check My Coding. > Java Vending Machine Project.
12Next
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 13 2013 12:47pm
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.Java

Code
/**
* @(#)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.Class

Code
Êþº¾




















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
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 13 2013 01:14pm
NM guys I found the problem. I was trying to mod totalCents rather than centsRemaing = centsRemaining % x or % y % z, etc. It should look like this lol.

Code

/**
* @(#)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.");
       //Currency button.
       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.");
           //Price in whole numbers. Example = 1 dollar = 100 pennies.
       totalCents = Integer.parseInt(totalCentsString);
       
       candyString = JOptionPane.showInputDialog(
           "Enter the price of candy. I.E. Candy is = 0.50 cents or 50.");
           //Price of candy in whole numbers. 50 cents = 50 pennies.
        priceOfCandy = Integer.parseInt(candyString);
       
       centsRemaining = totalCents - priceOfCandy;
       System.out.println( " priceOfCandy \n " + priceOfCandy);
             
       dollars = centsRemaining / 100;
        centsRemaining = centsRemaining % 100;
       //Used to find amount in dollars.
   
       quarters = centsRemaining / 25;
        centsRemaining = centsRemaining % 25;
       //Used to find amount in quarters.
       
       dimes = centsRemaining / 10;
        centsRemaining = centsRemaining % 10;
        //Used to find amount in dimes.
       
        nickles = centsRemaining / 5;
        centsRemaining = centsRemaining % 5;
        //Used to find amount in nickles.

        pennies = centsRemaining / 1;
        centsRemaining = centsRemaining % 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");
          //Total remaining change in appropriate amounts.      
   }
}
Member
Posts: 4,980
Joined: Jan 16 2010
Gold: 0.00
Warn: 20%
Feb 13 2013 01:20pm
I'm not completely sure what it is you're trying to achieve here, but a few things nonetheless:

- You don't need the .class file. It's the binary version of your .java source, and as you can see, it's gibberish. ;)
- The error lies in your conversion (which, by the way, is clever. I like it):
Code
centsRemaining = totalCents % 100;

should be
Code
centsRemaining = centsRemaining % 100;


e: Oh well. :p

This post was edited by Grandebedte on Feb 13 2013 01:20pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 13 2013 03:49pm
Quote (Grandebedte @ 13 Feb 2013 14:20)
I'm not completely sure what it is you're trying to achieve here, but a few things nonetheless:

- You don't need the .class file. It's the binary version of your .java source, and as you can see, it's gibberish. ;)
- The error lies in your conversion (which, by the way, is clever. I like it):
Code
centsRemaining = totalCents % 100;

should be
Code
centsRemaining = centsRemaining % 100;


e: Oh well. :p


Yep. Thanks for the thought though. I'll probably be back tonight because my code is off on my swimming pool java.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 13 2013 06:33pm
Learn to use logging.

log4j is what i use.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 13 2013 07:27pm
Quote (carteblanche @ 13 Feb 2013 19:33)
Learn to use logging.

log4j is what i use.


What is logging? I've only been studying java for a few weeks at most.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 14 2013 01:37pm
Too early to worry about logging. Just debug your shit using System.out.println or System.err.println.
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Feb 14 2013 02:23pm
Quote (irimi @ Feb 14 2013 01:37pm)
Too early to worry about logging.  Just debug your shit using System.out.println or System.err.println.


This. No need to over complicate the problem.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 14 2013 02:28pm
Quote (irimi @ 14 Feb 2013 14:37)
Too early to worry about logging.  Just debug your shit using System.out.println or System.err.println.


I already fixed the error and turned it in.

Quote (DirtyRasa @ 14 Feb 2013 15:23)
This.  No need to over complicate the problem.


Over complicate what? That script is rather easy to read imo.
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Feb 14 2013 02:44pm
By adding logging functionality.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll