• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple change program  RSS feed

 
Dalin Mansour
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about this project :
You are to create a simple change program. The user enters the amount due and the amount tendered. You are to calculate how much change they should receive. You are then to break down the change in $1 bills, quarters, dimes, nickels, and pennies such that the user will receive the least amount of coins. Remember to make the program user-friendly. Check for non-numeric characters and insufficient amounts tendered. Have the program loop so that I can test different cases.
I Almost finish the program but I need help to know how check for non-numeric characters and insufficient amount tendered. Any suggestions please?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably you already have some code to take what the user types in and turn it into a number. What does that code look like?
What happens right now if you type "foo" in when you are asked for amount due?
An Exception?
Then you need to catch that exception and "handle" it in some way.
One way of handling it is to set up a loop until the user enters in a "valid" value.

A similar approach could be taken for insufficient amount tendered.
you should be able to calculate if they have given you enough money to cover the bill.
If they haven't then keep asking until they do.

 
Dalin Mansour
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, your answer was very helpful. I tried what you said for both the amount due and the amount tendered and it works.
if (isValidNumber(amountTendered)) {
BigDecimal tender = new BigDecimal(amountTendered);
tender = tender.multiply(new BigDecimal(100));
amTender = tender.intValue();
if(amTender >= amDue){
break;
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow your code is so much nicer than mine. I am trying to get my head around how you did that!
 
Dalin Mansour
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!