-1

how can I exchange a given amount of money into notes and coins? lets say input is 1234,26 and we have notes for 1000, 500, 200, 100, 50, and coins for 20, 10, 1, and 0.5? so if input is greater than .25 and less than .75 it should be rounded to 1x 0.5 if its between .75 and 1.00 it should be rounded to 1x 1 and if its less than .25 it should be rounded to nothing?¨ for this exact program the desired output would look something like this:

1x: 1000
1x:  200
1x:   20
1x:   10
4x:    1
1x:    0.5

if it wasnt for the 0.5 coin, I think I would have been able to do it using int and %, but as of right now I am pretty much clueless(think I have to use array, but im not sure how) and have no idea how to start. also im beginnner, if you can keep that in mind as well when answering and explaining! any tips/solutions? thanks in advance!

like this?:

   System.out.println((input/1000) + " thousand " + ((input/500)%2) + " fivehundred " + (input/200%2.5) + " two hundred " + (input/100%2) + " hundred " + (input/50%2) + " fifty " + (input/20%2.5) + " twenty " + (input/10%2) + " ten " + input/1%10 + " one " );

still not sure how to deal with the 0.5 since I have to use int, input only cuz if I use double I get it completely wrong, I also have to use a if statement for the 0.5 coin..

gger234
  • 149
  • 7
  • 1
    Why don't you do exactly what you had planned on doing, with modulo? As for the .5- once you have 1 coins, you'll have exactly 1 .5 coin if the amount ends in .5, and 0 otherwise. – Sherz Aug 18 '14 at 21:35
  • check updated code for urself! and see if it works, this way, maybe you can figure it out, but I couldnt :/ – gger234 Aug 18 '14 at 21:37
  • For many currencies a "greedy" algorithm works. Start with the largest denomination and subtract it from the total as many times as you can, then go with the next larger denomination. Works for US currency so long as no one hands you a $3 bill. – Hot Licks Aug 18 '14 at 21:45

1 Answers1

1

I believe this is the standard approach to this kind of question.

double input = 1234.26;

int thousands = input/1000;
input = input - 1000*thousands;  //So now it would 234,26
int fivehundreds = input/500;
input = input - 500*fivehundreds;
etc...

Right, but you can't convert from double to int (i.e. thousands is an int, but input is a double, so input/1000 is a double). So you have a few options:

  1. Make thousands, fivehundreds, etc... be double. That is kinda ugly, though, there's no way they will have any decimal valu
  2. Casting mean anything to you? For example, (int)int thousands = input/1000; will work. You can read up on "casting", but basically I'm just telling Java to treat that number as an int, not a double
  3. Keep input as a int, and round it down. Then just check at the end if it has a decimal value (input % 1 > 0), and if it does, you need a half dollar.
Sherz
  • 568
  • 2
  • 14
  • ahh I see, so I basically use - instead of % so I can use double etc? so I assume I just do that all the way down and write a if statement for the 0.5 coin? – gger234 Aug 18 '14 at 21:50
  • Don't think you'll need a double (outside of the initial amount), because you'll have whole values of each bill. But you can do either way. Using `-1000*thousands` will be the equivalent of `%1000` – Sherz Aug 18 '14 at 21:52
  • wow this is harder than I tought, I cant use double, not even with the part you wrote down, if input number is double, it just mess up the whole thing, so what do you think? also for some weird reason whenever I type double, I get Inputmismatchexception, so my program doesnt run, ,but if I type in a int number with the exact same code, and use nextDouble(); etc the whole program just mess up – gger234 Aug 18 '14 at 22:13
  • how can I keep input as int if im gonna exchange 1234.26, thats what I dont understand, do I just int input=keyboard.nextInt(); and then on the next line write input %1>0? and also I have every single variable as double, except for input which has to be int. – gger234 Aug 18 '14 at 22:28
  • How is input an int? Isn't 1234,26 have a decimal value of "26"? – Sherz Aug 18 '14 at 22:33
  • exactly, but if I have double input=keyboard.nextDouble(); then the program wont run correctly, no matter if I actually input a integer or not. if I have int input=keyboard.nextInt(); then it runs correctly, the only progrem is that it doesnt work if I input 1234.26 cuz its double, but if I input 1234, its correct. so im pretty sure I have to have input as int, just as you said in 3) to get it right – gger234 Aug 18 '14 at 22:35
  • if you want I can show you my code so far, and you will see for yourself. I made a pretty pathetic attempt of an if sentence, not gonna lie, but thats not even the problem atm :) – gger234 Aug 18 '14 at 22:41
  • ok if I have int, and my input is: 1234 this is my output: 1.0 t 0.0 fh 1.0 th 0.0 h 0.0 f 1.0 tw 1.0 ten 4.0 one t=thousands, fh=fivehundred, I guess you can understand, and as you can see this is correct, 100%, if I change it to double, and have same input this is my output: 1.234 t 0.0 fh 0.0 th 0.0 h 0.0 f 0.0 tw 0.0 ten 0.0 one completely wrong, cuz all it did here was to divide input/1000 I guess. – gger234 Aug 18 '14 at 22:44
  • and the funny thing, even when I have double, and input a double, I get inputmismatchexception.. which is the same thing I get as if I have int – gger234 Aug 18 '14 at 22:45
  • Of course that's going to happen, because input is a double, so you can't do integer division. Try to do the second suggestion, with casting. – Sherz Aug 18 '14 at 22:50
  • yo, I edited the Original post, with my current code so you can see! :D if loop is probably wrong, but with the casting thing, where can I read more up on it, and do I just type (int)int thousands = input/1000; for every note, coin etc? – gger234 Aug 18 '14 at 22:51
  • ^well that didnt work, just getting a bunch of errors for every note/coin im initilizing – gger234 Aug 18 '14 at 22:55