Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic