Hi,

I am very new to Java, and am having trouble getting to grips with
objects......

We were given the Personi code (below) as an assignment to add certain parts
to the code to give it particular functionality.

I have almost finished the coding for this (as you can see), and, at the
moment there are no problems with the compiling of the code. Everything
seems to work fine so far.

There are two more parts of the assignment I need assistance with:

1. Add the attribute numberOfSandwichesEaten to the Personi class to
describe how many sandwiches have been eaten - I have done this and am
trying to display the number of sandwiches eaten. However, it keeps
displaying '0' as opposed to moving up in number.

2. Add the attribute wallet to the Personi class to hold the persons money,
if they do not have enough money in the wallet, they cannot buy the
sandwich. - I need to work out a way that either the Person or
SanwichMachine class checks on this - I would appreciate any suggestions on
how to do this?

Also:

1. I wanted to put in a new method into the Person Class to show the
contents of the Sandwich Machine (so I don't have to keep displaying the
contents of the machine over and over in the main method). However, this
always seems to cause me errors at compile time. How do I do this?

2. Although my code works fine so far, have I used the correct conventions?
(particularly creating a few different objects of Personi in Sandwichmachine
to print out the 'MACHINE OUTPUT')

3. In the first part of the code I have deleted out 'private SandwichMachine
theMachine' and the program seems to compile fine without it (where the
asterisks are). My teacher put this in - is it necessary, or can I leave it
out?

THANKS IN ADVANCE,

Tom.:



public class Personi
{
//private SandwichMachine theMachine; - **************

static int numberOfSandwichesEaten;
static int wallet;

public static void main(String[] args)
{

Personi sandwichMan = new Personi();
SandwichMachine theMachine = new SandwichMachine();
SandwichMachine.setPrices(120,140);
sandwichMan.makeInitialSandwiches(theMachine);
numberOfSandwichesEaten = sandwichMan.checkForSale(theMachine);
// The prices have been set and the machine has been filled


// showContents(); - I couldn't get this to work

printThis ("\nLets check the machine, what is in it at the
moment?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has shown the contents of the machine

pauseForAMoment();

Personi hungryStudent = new Personi();
printThis ("\nA student has just walked up to the machine......");
printThis ("He wants a cheese sandwich and has the correct change (140
pence)");
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 140, theMachine);

// A new person, hungryStudent has been created and he has purchased a
cheese sandwich (he had the right money)

pauseForAMoment();

printThis ("\nLets check the machine again, what is in it now?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has re-shown the contents of the machine

pauseForAMoment();

printThis ("\nThe sandwich was nice, but it didn't satisfy the hungry
student......");
printThis ("He wants another cheese sandwich, but, this time only puts
in 135 pence");
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 135, theMachine);
// The student has just put in too little money for the sandwich, the
error is displayed, and the money returned.

pauseForAMoment();

printThis ("\nLets check the machine again, what is in it now?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has re-shown the contents of the machine again

pauseForAMoment();

printThis ("\nThe student then remembers he needs to buy 3 egg
sandwiches for his friends,");
printThis ("as well as the one cheese sandwich that he wanted");
printThis ("He puts in 3 lots of 120 pence and selects Egg for his
friends' sandwiches");
printThis ("and 140 pence for his cheese sandwich.....");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);
pauseForAShortMoment();
hungryStudent.buySandwich("cheese", 140, theMachine);
// 3 egg sandwiches are bought, and one cheese exact money given, no
change needed

pauseForAMoment();

printThis ("\nLets check the machine another time, what is in it
now?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has re-shown the contents of the machine again

pauseForAMoment();

printThis ("\nThat Student must have a serious eating disorder......");
printThis ("He wants another egg sandwich for himself, and puts in a two
pound coin");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 200, theMachine);
// The student has just put in too much money for the egg sandwich, so
his change is returned.

pauseForAMoment();

printThis ("\nOk, lets check the machine another time, what is in it
now?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has re-shown the contents of the machine again

printThis ("\nWho'd believe it, the greedy student is back for another
egg......");
printThis ("He puts in the correct change, but .... there's no egg left
in the machine ....");
pauseForAShortMoment();
hungryStudent.buySandwich("egg", 120, theMachine);


printThis ("\nOk, lets check the machine one last time, what is in it
now?....");
printThis (sandwichMan.showNumberOfCheese(theMachine)+ " Cheese
Sandwiches left in the Machine");
printThis (sandwichMan.showNumberOfEgg(theMachine)+ " Egg Sandwiches
left in the Machine");
printThis ("Therefore, the total number of sandwiches in the machine is
" + (sandwichMan.showNumberOfCheese(theMachine) +
sandwichMan.showNumberOfEgg(theMachine) + " sandwiches"));
printThis ("and .....");
printThis ("There is "+ sandwichMan.showMoneyLeft(theMachine) + " Pence
left in the Machine");
printThis (numberOfSandwichesEaten + " Sandwiches have been eaten since
it was last filled");
// This has re-shown the contents of the machine again

}

public void makeInitialSandwiches(SandwichMachine machine)
{
machine.fillMachine();
}

public static void printThis(String output)
{
System.out.println(output);
}
// this method produces all output for the program (to cut down on code)


public int showNumberOfCheese(SandwichMachine machine)
{
return machine.checkCheeseLeft();
}

public int showNumberOfEgg(SandwichMachine machine)
{
return machine.checkEggLeft();
}

public int showMoneyLeft(SandwichMachine machine)
{
return machine.checkMoneyLeft();
}


public int checkForSale(SandwichMachine machine)
{
return machine.howManyBought();
}


public void buySandwich (String flavour, int money, SandwichMachine
machine)
{
machine.dispenseSandwich (flavour, money);
}

public static void pauseForAMoment()
{
int index = 1;
while (index <= 900000000)
{
index++;
}}

public static void pauseForAShortMoment()
{
int index = 1;
while (index <= 450000000)
{
index++;
}
}

}// end of class

//-----------------------------SandwichMachine
class----------------------------
class SandwichMachine
{
private int numberOfCheeseLeft;
private int numberOfEggLeft;
private int amountBought;

private static int cheesePrice;
private static int eggPrice;

private String flavour;

private int moneyBox;
private int money;


public void fillMachine()
{
Sandwich eggSandOne = new Sandwich("egg",eggPrice);
Sandwich eggSandTwo = new Sandwich("egg",eggPrice);
Sandwich eggSandThree = new Sandwich("egg",eggPrice);
Sandwich eggSandFour = new Sandwich("egg",eggPrice);
Sandwich cheeseSandOne = new Sandwich("cheese",cheesePrice);
Sandwich cheeseSandTwo = new Sandwich("cheese",cheesePrice);
Sandwich cheeseSandThree = new Sandwich("cheese",cheesePrice);
Sandwich cheeseSandFour = new Sandwich("cheese",cheesePrice);
upDateStatistics();
}

public static void setPrices( int egg,int cheese)
{
cheesePrice=cheese;
eggPrice=egg;
}

public void upDateStatistics()
{
numberOfCheeseLeft= numberOfCheeseLeft+4;
numberOfEggLeft=numberOfEggLeft+4;
}

public int checkCheeseLeft()
{
return numberOfCheeseLeft;
}

public int checkEggLeft()
{
return numberOfEggLeft;
}

public int checkMoneyLeft()
{
return moneyBox;
}

public int howManyBought() // ****** this is the problem
{
return amountBought;
}

public void dispenseSandwich(String flavour, int money)
{
this.flavour = flavour;
this.money = money;

if
(flavour.equals("cheese")&&(SandwichMachine.getCheesePrice())<=money&&(numbe
rOfCheeseLeft>=1))
{
numberOfCheeseLeft=numberOfCheeseLeft-1;
amountBought++;
Personi tempPersoni = new Personi();
tempPersoni.printThis("MACHINE OUTPUT - Thankyou, here is your Cheese
Sandwich .....");
moneyBox=moneyBox+getCheesePrice(); // made this flexible so that, if the
price of the sandwiches changes, this will compensate and the correct change
will be given
tempPersoni.printThis("MACHINE OUTPUT - "+(money-getCheesePrice())+" pence
change");


}
else if (flavour.equals("egg")
&&(SandwichMachine.getEggPrice())<=money&&(numberOfEggLeft>=1))
{
numberOfEggLeft=numberOfEggLeft-1;
amountBought++;
Personi tempPersonii = new Personi();
tempPersonii.printThis("MACHINE OUTPUT - Thankyou, here is your Egg
Sandwich .....");
moneyBox=moneyBox+getEggPrice(); // made this flexible so that, if the
price of the sandwiches changes, this will compensate and the correct change
will be given
tempPersonii.printThis("MACHINE OUTPUT - "+(money-getEggPrice())+" pence
change");


}

else if
(flavour.equals("cheese")&&(numberOfCheeseLeft==0)&&(numberOfEggLeft>=1))
{
Personi tempPersoniii = new Personi();
tempPersoniii.printThis("MACHINE OUTPUT - Sorry, we do not have enough
Cheese Sandwiches left.");
tempPersoniii.printThis("MACHINE OUTPUT - However, we do have " +
numberOfEggLeft + " Egg Sandwiches left");
tempPersoniii.printThis("MACHINE OUTPUT - They are priced at " +
getEggPrice() + " pence each");
tempPersoniii.printThis("MACHINE OUTPUT - money refunded");
}


else if
(flavour.equals("egg")&&(numberOfEggLeft==0)&&(numberOfCheeseLeft>=1))
{
Personi tempPersoniv = new Personi();
tempPersoniv.printThis("MACHINE OUTPUT - Sorry, we do not have enough Egg
Sandwiches left.");
tempPersoniv.printThis("MACHINE OUTPUT - However, we do have " +
numberOfCheeseLeft + " Cheese Sandwiches left");
tempPersoniv.printThis("MACHINE OUTPUT - They are priced at " +
getCheesePrice() + " pence each");
tempPersoniv.printThis("MACHINE OUTPUT - money refunded");
}

else if ((numberOfEggLeft==0)&&(numberOfCheeseLeft==0))
{
Personi tempPersonv = new Personi();
tempPersonv.printThis("MACHINE OUTPUT - Sorry, this machine is empty, come
back soon, money refunded");
}

else
{
Personi tempPersonvi = new Personi();
tempPersonvi.printThis("MACHINE OUTPUT - There is an error, not enough
money entered or incorrect selection made, money refunded");
}

}


public static int getCheesePrice()
{
return cheesePrice;
}

public static int getEggPrice()
{
return eggPrice;
}


}//end of class

//---------------------------------Sandwich
class-------------------------------
class Sandwich
{
private String filling;
private int price;

public Sandwich(String filling,int price)
{
this.filling = filling;
this.price = price;
if(filling.equalsIgnoreCase("egg"))
{
filling="egg";
}
if(filling.equalsIgnoreCase("cheese"))
{
filling="cheese";
}
}


}//end of class