my problem is that in my code, the if statement I have is supposed to take from a certain pot and then return the value but instead it takes values from both pots instead of one, can't figure out how to fix it. Also my other issue is that every time it takes candy from one pot its supposed to switch to that other pot, how can i do that? The if statement I'm talking about is in the class PassOutCandy.
import java.util.Random;
public class TreatHouse {
int candyPot1; // amount of candy in pot 1
int candyPot2; // amount of candy in pot 2
int currentPot; // 1 or 2
int totalCandy;
int currentTreaters; // variables
int treatsPerTreater;
public TreatHouse(int candyPot, int totalCandy) {
// Add code here, be sure to split the candy between the pots.
currentPot = candyPot;
this.totalCandy = totalCandy;
if (totalCandy <=0) {
System.out.println("We can't give out candy if we don't have amy. I think we have some from last year. " +
"Yep, we have 100 pieces of candy to give out.");
totalCandy = 100;
candyPot1 = totalCandy/2; // splits candy between both pots and sets totalCandy to 100 if user puts in false input
candyPot2 = totalCandy/2;
}
if (totalCandy > 0) {
this.totalCandy = totalCandy;
candyPot1 = totalCandy/2; // with correct user input splits the candy properly between both pots
candyPot2 = totalCandy - candyPot1;
}
}
public int getCandyCount() {
return candyPot1 + candyPot2; // total candy left
}
public void passOutCandy() {
//If there are enough treats per treater for the given amount per treater, pass out candy
//from the current pot and switch to the other one.
//Else display a message that the treaters have been tricked... (no candy for them.)
// but do not change the current pot
if ((totalCandy > 0) && (treatsPerTreater < totalCandy)) {
if (currentPot == 1 || currentPot == 2) {
if (currentPot == 1) {
candyPot1 = candyPot1 - (this.currentTreaters*this.treatsPerTreater);
}
if (currentPot == 2) {
candyPot2 = candyPot2 - (this.currentTreaters*this.treatsPerTreater);
}
}
else
System.out.println("Pot " + currentPot + " doesn't exist. Try again next time");
}
else
System.out.println("You have been tricked! No candy for you >:D!!!!");
}
//Sets the number of trick or treaters.
public void knockKnock() {
Random gen = new Random(System.currentTimeMillis());
this.currentTreaters = gen.nextInt(13) + 1; //1 to 13 treaters.
}
//Displays how much candy in each pot, total candy left
public void getCandyStatus() {
//add in code here
System.out.println("Candy in Pot1: " + candyPot1 + " Candy in Pot2: " + candyPot2); // will display candy in pot 1 / pot 2
System.out.println("Total candy left: " + (candyPot1 + candyPot2)); // total candy left
}
//returns the pot number for which candy was last given.
public int getLastPot() {
if (currentPot == 1 || currentPot == 2) {
if (currentPot == 2) { // returns the last pot that candy was taken from
currentPot = 1;
return currentPot;
}
if (currentPot == 1) {
currentPot = 2;
return currentPot;
}
}
else
return currentPot;
}
public void setTreatsPerTreater(int treatsPerTreater) {
//add code here
this.treatsPerTreater = treatsPerTreater; // sets the proper amount of treats per treater from user input
}
}
Aucun commentaire:
Enregistrer un commentaire