Need help with an array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benton2862
    New Member
    • Nov 2006
    • 2

    #1

    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.sho wInputDialog (null,"Enter the amount of the item for purchase");
    if (price.length() < 1) //Loop
    {
    JOptionPane.sho wMessageDialog (null, "You did not enter a purchase amount, try again please.");//Error message
    }
    payment = JOptionPane.sho wInputDialog (null,"Enter your payment amount.");
    if (payment.length () < 1) //Loop
    {
    JOptionPane.sho wMessageDialog (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.isUp perCase(price.c harAt(i)))
    {
    count++;
    }
    } //End loop
    // response = price - payment;
    JOptionPane.sho wMessageDialog (null, "You item costs "+price+" and you payed "+payment+" and your change will be "+change);





    System.exit(0);
    } //End Main
    } //End
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you can use method Float.parseFloa t() to convert the input strings into float and then calculate the change. e.g. the JOptionPane.sho wMessageDialog( ) could be
    Code:
    // calculate change
    // calculate change
    float fPrice = Float.parseFloat(price); 
    float fPayment = Float.parseFloat(payment);
    float fChange = fPayment-fPrice;
    JOptionPane.showMessageDialog (null, "You item costs "+price+" and you payed "+payment+" and your change will be "+ fChange);
    you can then work on rest of the output information

    Comment

    Working...