• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

simple change program

 
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?
 
Bartender
Posts: 1845
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;
 
Ranch Hand
Posts: 133
  • 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.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!