#The runSnackBar funtion keeps buying a random packet of crisps (BlueJ)
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Please share your actual code here rather than a random zip file.
This is where a random student is selected
public void runSnackBar(int nSteps)
{
int rand1;
for (int i = 1; i <= nSteps; i++) {
rand1 = randomGenerator.nextInt(students.size());
System.out.println("Time Step " + i);
describe();
students.get(rand1).snackTime();
System.out.println(" ");
}
}
The student will buy their favourite flavour of crisp
public void buyCrisps()
{
if (snackMachine.countPacks(favourviteFlavour) != 0)
if (pocket.pennyCount() >= snackMachine.getCost()) {
for (int i = 0; i < snackMachine.getCost(); i++)
snackMachine.insertMoney(pocket.removePenny());
snackMachine.buyPack(favourviteFlavour);
packOfCrisps = new PackOfCrisps(favourviteFlavour);
}
else
System.out.println(studentID + " doesn't have enough money to buy a pack!");
else
System.out.println("The machine has run out of " + studentID + "'s favourite " + favourviteFlavour + " crisps!");
}
The funtion which purchases a crisp
public PackOfCrisps buyPack(String flavour)
{
int i = 0;
boolean found = false;
PackOfCrisps packet = null;
if (countPacks(flavour) > 0)
if (Pennies.size() >= cost) {
while (i < capacity && found == false) {
if (PacksOfCrisps.get(i).getFlavour().equals(flavour))
packet = PacksOfCrisps.get(i);
PacksOfCrisps.remove(i);
found = true;
}
while (payment > 0) {
payment -= 1;
Pennies.remove(payment);
}
return packet;
}
else
System.out.println("Insuffient funds!");
else
System.out.println("No packets of specified flavour availble!");
return packet;
}
Where is that snackTime method?..
Have you tried placing some breakpoints to troubleshoot it?