Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate the cost of shopping  RSS feed

 
Marcus lewis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys ;
As a new beginner in Java ... i want yr help :

the owner of shop is having trouble with his staff
giving the wrong change. he wants to designa program that allows the staff member to enter the total cost of the shopping
and the amount the customer has paid. The program should then calculate and
output how many euro50, euro20, euro10, euro5 notes, and how many e2, and e1 coins
to give the customer as change.
the owner refuses to deal with small coins so although the staff member
enters a decimal value for the cost of the shopping he rounds all of his prices
down to the nearest euro and doesn’t bother with 50c, 20c, 10c, 5c, 2c or 1c
coins.
Thanks
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus lewis,

Welcome to Code Ranch.

It seems to be a homework to me. If it is please DoYourOwnHomework

Please understand that code ranch is NotACodeMill. You will have to ShowSomeEffort first.

Tell us what you have tried and what is the error that occurs. We will help you to solve those errors. We can't give away codes as per code ranch's policy.
 
Marcus lewis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not a homework first.
second. i did the scanner class. let the code gets the totalCost and amountPaid.

but i dont know how to let the program differentiate between notes and coins and says your change for example 50 euro note and 2 euro coins ....
i could do it in the way just say yr change 52 euro ...
just know am learning java 5 days ago ... that is why i need forums ...
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Right: so you have got the amount paid and the amount of the bill. I presume you can work out the amount of change easily now? Why are your Eurozone traders so curmudgeonly as only to charge to the nearest whole number? Once you have the program it is easier to calculate change to the nearest cent than to try rounding.
I would suggest you learn about enumerated types which you can read about in the Java™ Tutorials. You can use an enum to create a object corresponding to each denomination of money and you can even add a value and N or C to that object.

By the way: please search this forum. Change is a common beginner's assignment and you will probably find other discussions which will be helpful.
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this is crossposted here.

While we don't mind if you cross post to another site, we do ask that you cite where you have done so. You can read up on why we prefer it this way here -> BeForthrightWhenCrossPostingToOtherSites
 
Marcus lewis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks cambell ... this is what i need ... enum ... thanksssss
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

By the way: you will find calculating change much easier if you denominate everything in whole numbers rather than floating‑point numbers. Use ints and caount in cents.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Marcus lewis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guyes kindly any one who amend this code to be the following

instead of USD let it be Euro :

in the receipt it has to say 50 or 20 or 10 or 5 euro bills and 2 or 1 coins ( no need for the cents in the change)

if there is cents in change the program has to near it down to the nearst value .. ex : if change 32.40 it has to take it just 32euro and says 30 euro bills and 2 coins.

no need for the rest like record.

please help me!!!
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus,

I've moved your post to this new topic.
Please show some effort and show us what you have come up with so far.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus lewis wrote:instead of USD let it be Euro :

Actually, that's kind of irrelevant.

Both currencies are divided into 100 cents, so whatever solution you come up with for Euros should work equally well for Dollars, Rubles, or Yuan..

But Karthik is right: TellTheDetails (←click) and show us what you've tried already.

Winston
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something familiar about this question. I am sure I have already given you advice. I have told you to stick to integer arithmetic and denominate everything in whole numbers either € or ¢ or $ or something. You will find it much easier not to have to do rounding. Beware of floating‑point arithmetic; things will go horribly wrong if you enter $12.34 and find it is interpreted as twelve dollars and 33.99999999999999999999 cents.

You should not ask the same question twice. Nil desperandum; I can join this question to your old thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!