Reputation: 69
I am making a piggybank style program in java for a class project that can add, subtract, compare, and increment/decrement coins. I am trying to add the following toString method into the code so that it displays the correct words for the corresponding coin values.
If any of the quarters, dimes, nickels, or pennies are 0, I want them to be completely left off the string. If there is just 1 of the quarters, dimes, nickels, or pennies, I want the output to say quarter, dime, nickel, or penny.
So basically, if there was $1.01 in the bank in 4 quarters, 0 dimes, 0 nickels, and 1 penny it would output: $1.01: 4 quarters, 1 penny
I'm not sure how to do this. I was trying to exhaustively do this with if statements, but I'm sure there is a better way. Here is the code I have currently, which obviously doesn't work when there are more than 1 value set to 0 or 1, and it looks ridiculous.
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String str = fmt.format(amountInJar()); //amountInJar is a double value of the current currency
if (this.quarters > 1 && this.dimes > 1 && this.nickels > 1
& this.pennies > 1) {
str = str + ": " + quarters + " quarters, " + dimes + " dimes, "
+ nickels + " nickels, " + pennies + " pennies ";
} else if (quarters == 1) {
str = str + ": " + quarters + " quarter, " + dimes + " dimes, "
+ nickels + " nickels, " + pennies + " pennies ";
} else if (quarters == 0) {
str = str + ": " + dimes + " dimes, " + nickels + " nickels, "
+ pennies + " pennies ";
} else if (dimes == 1) {
str = str + ": " + quarters + " quarters, " + dimes + " dime, "
+ nickels + " nickels, " + pennies + " pennies ";
} else if (dimes == 0) {
str = str + ": " + quarters + " quarters, " + nickels
+ " nickels, " + pennies + " pennies ";
} else if (nickels == 1) {
str = str + ": " + quarters + " quarters, " + dimes + " dimes, "
+ nickels + " nickel, " + pennies + " pennies ";
} else if (nickels == 0) {
str = str + ": " + quarters + " quarters, " + dimes + " dimes, "
+ pennies + " pennies ";
} else if (pennies == 1) {
str = str + ": " + quarters + " quarters, " + dimes + " dimes, "
+ nickels + " nickels, " + pennies + " penny";
} else if (pennies == 0) {
str = str + ": " + quarters + " quarters, " + dimes + " dimes, "
+ nickels + " nickels ";
}
return str;
}
I know it would simply be easier to format with quarter(s), dime(s), nickel(s), penny(ies), but in order to pass the instructors Junit tests, it must be formatted this way.
Thank you for any help!
Upvotes: 0
Views: 128
Reputation: 58888
Just do it separately for each type of coin. To handle the separators (":" and ","), a convenient trick is to add a separator after each item, then remove the extra separator from the end. Something like this:
str = str + ": ";
if(this.quarters > 1)
str = str + this.quarters + " quarters, ";
else if(this.quarters == 1)
str = str + "1 quarter, ";
if(this.dimes> 1)
str = str + this.dimes+ " dimes, ";
else if(this.dimes== 1)
str = str + "1 dime, ";
// same thing for nickels and pennies
str = str.substring(0, str.length() - 2); // cut off the last two characters, i.e. the extra comma and space
Upvotes: 0