java - Cash Register Change Issue -


the below code not calculating best way distribute change in java 'cash register'. how fix it?

public void generateusdchange(){     change = payment-purchase;     quarters = (int) (change/.25);     change -= (quarters * .25);     dimes = (int) (change/.1);     change -= (dimes * .1);     nickels = (int) (change/.05);     change -= (nickels * .05);     pennies = (int) (change/.01);     changeamount = quarters*.25 + dimes*.1 + nickels*.05 + pennies*.01;     if(changeamount != (payment-purchase)){         pennies++;         if(pennies>=5){             nickels++;             pennies-=5;         }         if(nickels>=2){             dimes++;             nickels-=2;         }         if(((dimes*.1) + (nickels*.05)) >= .25){             quarters++;             dimes-=2;             nickels--;         }     } } 

as others suggested double , floats not currency. can use bigdecimal. here's working code:

import java.math.bigdecimal; import java.util.linkedhashmap; import java.util.map;  public enum currencydenomination {  hundred(bigdecimal.valueof(100)), fifty(bigdecimal.valueof(50)), twenty(bigdecimal.valueof(20)),          ten(bigdecimal.valueof(10)), five(bigdecimal.valueof(5)), one(bigdecimal.valueof(1)),                  quarter(bigdecimal.valueof(.25)), dime(bigdecimal.valueof(.10)),                          nicle(bigdecimal.valueof(.05)), pennies(bigdecimal.valueof(.01));  private bigdecimal value;  currencydenomination(bigdecimal value) {     this.value = value; }  public static map<currencydenomination, integer> calculate(bigdecimal balance) {     map<currencydenomination, integer> balancecurrency = new linkedhashmap<currencydenomination, integer>();     bigdecimal leftover = balance;     system.out.println("given amount : "+balance);     (currencydenomination currencydenomination : currencydenomination             .values()) {          int count = leftover.divide(currencydenomination.value).intvalue();          if (leftover != bigdecimal.zero) {             if (balancecurrency.containskey(currencydenomination)) {                 int existingcount = balancecurrency                         .get(currencydenomination);                 existingcount = existingcount + count;                 balancecurrency.put(currencydenomination, existingcount);             } else {                 balancecurrency.put(currencydenomination, count);             }         }          leftover = leftover.remainder(currencydenomination.value);          if (leftover.equals(bigdecimal.zero)) {             break;         }     }     return balancecurrency; }  public static void main(string[] args) {     system.out.println("balancecurrency : "+calculate(bigdecimal.valueof(49.52))); } } 

output:

given amount : 49.52 balancecurrency : {hundred=0, fifty=0, twenty=2, ten=0, five=1, one=4, quarter=2, dime=0, nicle=0, pennies=2} 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -