473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with an array.

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
1 1563
horace1
1,510 Expert 1GB
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

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

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.