I'm trying to compile my main class, Halloween
, but I'm getting an error that says that the class TreatHouse
is not found. I'm not sure why this is happening, but would anybody be able to help me out please? Also, please feel free to comment on any other errors you may see in my code; any help is greatly appreciated. Thanks!
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Halloween {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<TreatHouse> houses = new ArrayList<TreatHouse>();
Random gen = new Random(System.currentTimeMillis());
int numHouse = gen.nextInt(10) + 1;
// create houses
for (int i = 0; i < numHouse; i++) {
System.out.println("House " + i );
System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?");
int candyPot = scan.nextInt();
System.out.println("How much candy did we buy?");
int totalCandy = scan.nextInt();
houses.add( new TreatHouse(candyPot, totalCandy));
}
boolean allDone = false;
while(!allDone){
//Each house will pass out candy for a bit.
for (TreatHouse house : houses) {
house.passTime();
}
//Check to see if all the house are done. If so set sentinel flag to true and exit while loop
allDone = false;
for(TreatHouse house: houses){
//If a single house is not done then all are not done
if(!house.isDone()){
allDone = false;
break;
}
else
allDone = true;
}
//Call static method to display some statistics about the holiday so far
}
System.out.println("All houses are done");
scan.close();
}
}
import java.util.Random;
import java.util.Scanner;
public class TreatHouse {
private int candyPot1; // amount of candy in pot 1 for a single TreatHouse
private int candyPot2; // amount of candy in pot 2 for a single TreatHouse
private int currentPot; // (1 or 2) current Pot for a single TreatHouse
private int totalCandy; // total candy for a single TreatHouse
private int currentTreaters; // current Treaters for a single TreatHouse
private boolean isDone = false; // is this house done for the night?
private int treatsPerTreater; //How much candy to give out to each treater at a single TreatHouse
private int nextID;
//Static variables
public static int participatingHouses = 0; //This contains the total number of houses participating in this year's holiday
public static int totalCandyPassedOut = 0; //This contains the total number of candy passed out for all the houses.
public static int totalTricksGiven = 0; //This contains the total number of tricks given out. Remember that each kid gets tricked.
public static int houseID = 1; //This is the number for the house which is unique.
//Constructor
public TreatHouse()
{
participatingHouses++;
totalCandyPassedOut++;
totalTricksGiven++;
nextID=houseID++;
}
//Method to display the statistic variables
public static void getHalloweenStatus()
{
System.out.println("Total participating houses: " + participatingHouses); //Displays the total number of participating houses
System.out.println("Total candy passed out: " + totalCandyPassedOut); //Displays the total amount of candy passed out
System.out.println("Total tricks given: " + totalTricksGiven); //Displays the total amount of tricks given
System.out.println(houseID);
}
public TreatHouse(int candyPot, int totalCandy) {
// Error Check and split candy
if (totalCandy > 0) {
candyPot1 = totalCandy / 2;
candyPot2 = totalCandy / 2 + totalCandy % 2;
} else {
System.out.println("We can't give out candy if we don't have any."
+ " I think we have some from last year. Yep, we have 100 " + "" + "pieces of candy to give out.");
totalCandy = 100;
candyPot1 = totalCandy / 2;
candyPot2 = totalCandy / 2 + totalCandy % 2;
}
// currentPot Check
if (candyPot == 1) {
currentPot = 1;
} else if (candyPot == 2) {
currentPot = 2;
} else {
System.out.println("Invalid choice for pot. Only 1 or 2. Using pot 1");
currentPot = 1;
}
//Adjust Static variables as needed here:
}
public void passTime() {
Scanner scan = new Scanner(System.in);
//before
this.getCandyStatus();
System.out.println("How much candy per treater should we give out?");
this.setTreatsPerTreater(scan.nextInt());
System.out.println("Knock, knock...." + "Trick or treat!");
this.knockKnock();
this.passOutCandy();
//After
this.getCandyStatus();
//Determines if the house if done for the night.
if(totalCandy == 0){
System.out.println("Time to turn off the lights and go to bed!");
System.out.println("The last candy came from pot number" + this.getLastPot());
System.out.println("Happy Halloween!");
isDone = true;
}
}
private void printCurrentPot() {
System.out.println("The current Pot is: " + currentPot);
}
public int getCandyCount() {
return candyPot1 + candyPot2;
}
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 ((currentPot == 1 && candyPot1 == 0) || (currentPot == 2 && candyPot2 == 0)) {
// Switch pots and print message
System.out.println("There is no candy in the currentPot Switching pots.");
switchPots();
}
{// There is some candy to give out.
int currentPotAmmount = getCurrentPotAmount();
if (treatsPerTreater * currentTreaters <= currentPotAmmount) {
//Passing out Candy
if (currentPot == 1) {
candyPot1 -= treatsPerTreater * currentTreaters;
totalCandy -= treatsPerTreater * currentTreaters;
} else {
candyPot2 -= treatsPerTreater * currentTreaters;
totalCandy -= treatsPerTreater * currentTreaters;
}
switchPots();
} else {
// trick
System.out.println("You have been tricked!");
}
}
}
private int getCurrentPotAmount() {
if (currentPot == 1)
return candyPot1;
else
return candyPot2;
}
private void switchPots() {
if (currentPot == 1) {
currentPot = 2;
} else
currentPot = 1;
}
// 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.
System.out.println("There are " + currentTreaters + " treaters.");
}
// Displays how much candy in each pot, total candy left
public void getCandyStatus() {
this.printCurrentPot();
System.out.println("Candy in Pot 1: " + candyPot1);
System.out.println("Candy in Pot 2: " + candyPot2);
}
// returns the pot number for which candy was last given.
public int getLastPot() {
if (currentPot == 1)
return 2;
else {
return 1;
}
}
public void setTreatsPerTreater(int treatsPerTreater) {
this.treatsPerTreater = treatsPerTreater;
}
public boolean isDone() {
return isDone;
}
}