By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Need help with an array.

P: 2
I need help with and array for a program that acts like a cash register. The user will input an items price and then there payment. It will tell you the change and then tell you what kinda of change to use, see example:

Use JOptionPane to take inputs from the user and display the results.
Example:
--------
If the user enters
Amount Due = 5.52 // First Input from the User
Amount Given = 10.00 // Second Input from the User
Calculate the Balance Due
Balance Amount = $ 4.48
and display the balance due in denomination as (The following four lines will be ONE Output to the user)
$1 bill = 4 (4.00)
Quarters = 1 (0.25)
Dimes = 2 (0.20)
Pennies = 3 (0.03)

This last part, where you display the change that will be given back, ie: how many quarters, dimes, nickels, pennies is the part I am having problems with.
Can some one help? Please pardon my coding abilities, I am still learning how to do this and would really appreciate any help you can with my program.

Thanks in advance.


import javax.swing.*;
import java.text.*;

public class CashReg
{ //Start
public static void main (String [] args)
{ //Start Main
int count = 0;
char c1 = 0;
String price = null;
String payment = null;
String change = null;
boolean correct = false;
while(!correct)
{ //Start Loop
price = JOptionPane.showInputDialog (null,"Enter the amount of the item for purchase");
if (price.length() < 1) //Loop
{
JOptionPane.showMessageDialog (null, "You did not enter a purchase amount, try again please.");//Error message
}
payment = JOptionPane.showInputDialog (null,"Enter your payment amount.");
if (payment.length() < 1) //Loop
{
JOptionPane.showMessageDialog (null, "You did not enter a payment amount, try again please.");//Error message
}

else
{
correct = true; //Boolean to confirm that there are enough characters
}
}
for (int i=0; i<price.length(); i++)
{
if (Character.isUpperCase(price.charAt(i)))
{
count++;
}
} //End loop
// response = price - payment;
JOptionPane.showMessageDialog (null, "You item costs "+price+" and you payed "+payment+" and your change will be "+change);





System.exit(0);
} //End Main
} //End
Nov 24 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you can use method Float.parseFloat() to convert the input strings into float and then calculate the change. e.g. the JOptionPane.showMessageDialog() could be
Expand|Select|Wrap|Line Numbers
  1. // calculate change
  2. // calculate change
  3. float fPrice = Float.parseFloat(price); 
  4. float fPayment = Float.parseFloat(payment);
  5. float fChange = fPayment-fPrice;
  6. JOptionPane.showMessageDialog (null, "You item costs "+price+" and you payed "+payment+" and your change will be "+ fChange);
  7.  
you can then work on rest of the output information
Nov 25 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.