Ok, this is close, so I don't consider it plagiarism for you to get an alteration in the code structure.
Lets look at the pseudocode:
Code:
IF the student has a can THEN
take a sip
IF the can is now empty THEN
add it to the bin
OTHERWISE (i.e. the student does not have a can to start with)
IF the student has a Coin AND the machine is not empty THEN
insert a Coin into the machine
reduce Coins by one
get the can from the machine
open the can and take a sip
OTHERWISE
student starts sobbing
Without the endif statements, the above appears correct, but it will not match what you intend to do. The correct pseudo code appears to be:
Code:
IF the student has a can THEN
take a sip
IF the can is now empty THEN
add it to the bin
ENDIF
OTHERWISE IF the student has a Coin AND the machine is not empty THEN
insert a Coin into the machine
reduce Coins by one
get the can from the machine
open the can and take a sip
OTHERWISE
student starts sobbing
Mapping the otherwise to else:
Code:
IF (condition)
dostuff
IF (condition)
dostuff
ENDIF
ELSE IF (condition)
dostuff
ELSE
dostuff
ENDIF
Now, lets alter the structure of your current code.
Code:
public void doAction()
{
if (can != null) // First condition
{
can.giveSip();
if (can.isEmpty()) // Second condition
{
bin.addCan(bi);
can = null;
}
}
else if (purse != null && can != isEmpty())
{
machine.insertCoin();
machine.deliverCan();
can.open();
can.giveSip();
}
else
{
sobbing = true;
}
}
Now what we have is if the can is not null, take a sip. If its now empty, we dispose of the can and remove it from this class and the method is satisfied.
If we don't have a can, we check to see if purse is null (this is wrong, we'll get into that) and that the can is not empty (also wrong, we'll get into that too). If we have money and the can is not empty, we then purchase a new can, open it, and sip it. Otherwise, we start to sob.
The changes you will need to still do are here:
PHP Code:
else if (purse != null && can != isEmpty())
{
machine.insertCoin();
machine.deliverCan();
can.open();
can.giveSip();
}
purse will not be null, and you don't want to check if the can is empty since you don't care if it is by this point. As you've asked, the ArrayList is a typeof Collection<T>, so you retrieve the number of items with the size() method. If its > 0, you have items within your collection. Alternatively, isEmpty will return true if the ArrayList has no items. The in action checks are
if (list.size() > 0) or
if (!list.isEmpty()).
The check for can is also wrong, isEmpty is unlikely to return any results of type CanOfCoke (I don't see it defined in Student, so it actually won't compile), but its not the can you want to check - its the machine (defined here:
and the coke machine (if it isn't empty)). If the CokeMachine is also a collection, it will also have an isEmpty method. Using checks against size() or isEmpty() on your purse and machine variables need to result in
if (true && true) in order to process. That should give you enough information to get that one fixed up.
And finally, within that elseif block, the current Student needs to obtain the can itself. I assume that the machine and can take care of themselves since I don't know the methods on them. If the current Student does not obtain the can, they will repeatably purchase a can and take a sip, but never discard it.
Try those changes and post back if you run into any more trouble!