I'm attempting to create a function that returns coins after an item has been purchased. I'm not finished, but the code below is an attempt to find the number of quarters, dimes, nickels and pennies that should be returned:
public String getChange(VendingMachine vendingMachine, Change change) {
double dispensedQuarters = 0;
double dispensedDimes = 0;
double dispensedNickels = 0;
double dispensedPennies = 0;
double d = Double.parseDouble(vendingMachine.getFunds());
if (d % .25 == 0) {
dispensedQuarters = d / .25;
} else if (d % .25 != 0) {
double remainder = d % .25;
d = d - remainder;
dispensedQuarters = d / .25;
if (remainder % .10 == 0) {
dispensedDimes = remainder / .10;
} else if (remainder % .05 == 0) {
dispensedNickels = remainder / .05;
} else if (remainder % .01 == 0) {
dispensedPennies = remainder / .01;
} else {
dispensedDimes = dispensedNickels = dispensedPennies = 0;
}
} else if (d % .10 == 0) {
dispensedDimes = d / .10;
} else if (d % .05 == 0) {
dispensedNickels = d / .10;
}
Is there a more compact way of creating a function that will find the number of quarters, dimes, nickels, and pennies that should be returned?
Aucun commentaire:
Enregistrer un commentaire