0

I have a cashregister program that inputs purchases and payment and outputs the change due. i need it to not give just an amount but what particular coins/dollars user should get back. heres two methods i have

  public void recordPurchase()
  {
    System.out.print("Enter total purchase price or negative number to end: "); 
    double input = keyboard.nextDouble();
    while(input > 0)
    {
      purchase = purchase + input;
      System.out.print("Enter total purchase price or negative number to end: "); 
      input = keyboard.nextDouble();
    }
  }

  public double giveChange(Money moneyTypes)
  {
    double change = payment - purchase;
    purchase = 0;
    payment = 0;
    //computes change rounding to two decimal places
    change = (double)(Math.round(change*100))/100;
    return change;
  }

I need to output what coins/dollars person should get back. i have the money types saved in an array called moneyTypes. for example if the change due is $1.06 it would output you receive a dollar nickel and penny.

any advice would help. Thanks! if you need to see more of the code let me know

Jess Anastasio
  • 687
  • 5
  • 18
  • 26
  • 1
    This is a pretty popular homework assignment posted to StackOverflow. Is there something specific that wasn't explained on the several other questions regarding this exact sort of problem? – nhgrif Nov 11 '13 at 18:30

1 Answers1

0

I'll give you an advice how to do it, not a solution.

Make a list of possible coin/note values.

Then from the biggest to lowest, compute how many times it fits into the remainder, and subtract this amount of money from the value. Make a note of the number of coins/notes.

This way, you will get the numbers you need.

count = Math.floor(remainder/coinValue) might help you.

MightyPork
  • 18,270
  • 10
  • 79
  • 133