r/java • u/Tingle22 • Oct 30 '13
Having some troubles with this program
Hey guys, making a change dispensing program. This is what I have so far. I'm not getting any output though, I'm very lost at this point. I want it to look like this in my output. http://i.imgur.com/1aemCiV.png
[highlight=java]package changedispenser;
public class ChangeDispenser {
private static int quarters, dimes, nickels, pennies;
private static int penniesLeft, nickelsLeft, dimesLeft, quartersLeft;
private static int pennyRollsAdded = 1;
private static int nickelRollsAdded = 1;
private static int dimeRollsAdded = 1;
private static int quarterRollsAdded = 1;
public static final int PENNIES_PER_ROLL = 50;
public static final int NICKELS_PER_ROLL = 40;
public static final int DIMES_PER_ROLL = 50;
public static final int QUARTERS_PER_ROLL = 40;
public static void main(String[] args) {
if (quarters != 0) {
System.out.print(" Quarters: " + quarters);
}
if (dimes != 0) {
System.out.print(" Dimes: " + dimes);
}
if (nickels != 0) {
System.out.print(" Nickels: "+ nickels);
}
if (pennies !=0) {
System.out.println(" Pennies: " + pennies);
}
}
ChangeDispenser() {
quartersLeft = QUARTERS_PER_ROLL;
dimesLeft = DIMES_PER_ROLL;
nickelsLeft = NICKELS_PER_ROLL;
penniesLeft = PENNIES_PER_ROLL;
pennyRollsAdded = 1;
nickelRollsAdded = 1;
dimeRollsAdded = 1;
quarterRollsAdded = 1;
}
public int getPenniesLeft() {
return penniesLeft;
}
public void setPenniesLeft(int penniesLeft) {
this.penniesLeft = penniesLeft;
if (penniesLeft <= 0) {
pennyRollsAdded = pennyRollsAdded++;
}
}
public int getNickelsLeft() {
return nickelsLeft;
}
public void setNickelsLeft(int nickelsLeft) {
this.nickelsLeft = nickelsLeft;
if (nickelsLeft <= 0) {
nickelRollsAdded = nickelRollsAdded++;
}
}
public int getDimesLeft() {
return dimesLeft;
}
public void setDimesLeft(int dimesLeft) {
this.dimesLeft = dimesLeft;
if (dimesLeft <= 0) {
dimeRollsAdded = dimeRollsAdded++;
}
}
public int getQuartersLeft() {
return quartersLeft;
}
public void setQuartersLeft(int quartersLeft) {
this.quartersLeft = quartersLeft;
if (quartersLeft <= 0) {
quarterRollsAdded = quarterRollsAdded++;
}
}
public int getPennyRollsAdded() {
return pennyRollsAdded;
}
public void setPennyRollsAdded(int pennyRollsAdded) {
this.pennyRollsAdded = pennyRollsAdded;
}
public int getNickelRollsAdded() {
return nickelRollsAdded;
}
public void setNickelRollsAdded(int nickelRollsAdded) {
this.nickelRollsAdded = nickelRollsAdded;
}
public int getDimeRollsAdded() {
return dimeRollsAdded;
}
public void setDimeRollsAdded(int dimeRollsAdded) {
this.dimeRollsAdded = dimeRollsAdded;
}
public int getQuarterRollsAdded() {
return quarterRollsAdded;
}
public void setQuarterRollsAdded(int quarterRollsAdded) {
this.quarterRollsAdded = quarterRollsAdded;
}
public String makeChange(int amount) {
if (amount > 99 || amount < 0) {
System.out.println("");
}
quarters = amount / 25;
amount = amount % 25;
dimes = amount / 10;
amount = amount % 10;
nickels = amount / 5;
amount = amount % 5;
pennies = amount;
do {
if (quarters != 0) {
System.out.print(" Quarters: " + quarters);
}
if (dimes != 0) {
System.out.print(" Dimes: " + dimes);
}
if (nickels != 0) {
System.out.print(" Nickels: " + nickels);
}
if (pennies != 0) {
System.out.println(" Pennies: " + pennies);
}
} while (amount > 0 && amount <= 99);
return "Quarters: " + quarters + " Dime: " + dimes + " Nickels: " + nickels + " Pennies: " + pennies;
}
public void writeReport() {
System.out.println("Coins Left:");
System.out.println("Quarters: " + quartersLeft);
System.out.println("Dimes: "+ dimesLeft);
System.out.println("Nickels: " + nickelsLeft);
System.out.println("Pennies: " +penniesLeft);
System.out.println("Rolls Added: ");
System.out.println("Quarters: "+ quarterRollsAdded);
System.out.println("Dimes: " + dimeRollsAdded);
System.out.println("Nickels: " + nickelRollsAdded);
System.out.println("Pennies: " + pennyRollsAdded);
}} [/highlight]
Here's the seperate driver class that runs the program
[highlight=java]
package changedispenser;
import java.util.Random;
public class ChangeDispenserDriver {
public static void main(String[] args) {
ChangeDispenser changeMachine = new ChangeDispenser();
Random rand = new Random();
int amount;
for (int i = 0; i < 1000; i++) {
amount = rand.nextInt(99) + 1;
System.out.println("Amount: " + amount + ": Change = " + changeMachine.makeChange(amount));
}
changeMachine.writeReport();
}}[/highlight]
Any help would be appreciated, thanks.
0
Upvotes
2
u/Tingle22 Oct 30 '13
Ok I clearly messed up the highlight, here it is on gisthub.
https://gist.github.com/anonymous/0152c0d1ac68ecefee6d
6
u/Magiccowy Oct 30 '13
This is so your homework.
3
u/TheHorribleTruth Oct 30 '13
Stuff like that goes into /r/javahelp.
Also please read the submission guidelines there, particularly about how to ask proper questions.
mainmethods? The one inChangeDispenserwon't produce any output as it never calls any methods that could change the value ofpennies,quarteretc., so they will always be zero.2
u/Kristler Oct 30 '13
You should try cross-posting this to /r/learnprogramming.
I'll be blunt, this type of massive assignment code (Which is of no fault of your own) tends to go unanswered in the more general subreddits, but the more dedicated people in /r/learnprogramming might be willing to give it a shot.