/**
* This class approximates the standard interface to a physical
* toilet. The current implementation is a stub that mostly
* just prints stuff. We could replace it later with a more detailed
* implementation that manipulates the chain, the siphon jet, the ball
* float, the filler valve, etc. All of these would be handled with
* private methods and variables, because the user shouldn't
* have to know how the toilet works in order to use it.
*
* @author Jason Eisner
* @version 1.0, 2003-01-26
*/
public class Toilet {
/** Whatever's in the toilet. */
protected Waste contents; // starts out as null, i.e., empty
/** True iff the seat is up. Can be changed but isn't really used yet. */
protected boolean seatup; // starts out as false
/** Probability that the toilet will clog and overflow if you try to
* flush TrashWaste. This is a static field, so it is shared by
* all toilets - it cannot vary from toilet to toilet. It is also
* a final field, i.e., a constant that can't be changed or overridden.
*/
private static final float CLOG_PROBABILITY = 0.5F; // static, so shared by all Toilets
private static java.util.Random random = new java.util.Random();
// We have no special constructor - so we'll get a 0-argument
// constructor Toilet() by default.
/** Place something in the toilet.
* @param w The waste to deposit.
*/
public void deposit(Waste w) {
if (isYucky())
System.out.print("Yuck ... should have flushed first ... oh well here goes ... ");
contents = w; // simply replace old contents - not realistic
System.out.println("Splash! " + w);
// Java treats the argument as "Splash! " + w.toString()
}
/** Does the toilet need flushing?
* @return true
if it does, false
otherwise.
*/
public boolean isYucky() {
return (contents instanceof SolidWaste);
}
/** Make the contents of the toilet (if any) go away.
* @throws FloatingOverflowException If the toilet happened to clog.
* In this case it needs to be reflushed (after you clean up the
* bathroom), although it might clog again.
*/
public void flush() throws FloatingOverflowException {
System.out.print("Pretending to execute complicated flush procedure ... ");
if (contents instanceof TrashWaste
&& random.nextFloat() < CLOG_PROBABILITY) {
System.out.print("Overflowed " + contents + " all over the place ... ");
throw new FloatingOverflowException();
}
contents = null;
System.out.println("all gone");
}
/** Change the seat position.
* @param b true to raise it, false to lower it.
*/
public void raiseSeat(boolean b) {
seatup = b;
System.out.println( b ? "Seat up" : "Seat down" );
}
/** This isn't necessarily the real main program, but it
* will be run if we type "java Toilet", so it is a good
* way to test the Toilet class. If the toilet overflows,
* we will crash with a FloatingOverflowException; the method's
* signature warns about this, as required. */
public static void main(String args[]) throws FloatingOverflowException {
Toilet throne = new Toilet();
throne.raiseSeat(true); // considerate male user
throne.deposit(new LiquidWaste("pee")); // call Toilet.deposit
(new LiquidWaste("more pee")).deposit(throne); // same thing via Waste.deposit
throne.raiseSeat(false);
throne.deposit(new SolidWaste("poo")); // make toilet yucky
throne.deposit(new SolidWaste("more poo")); // so wish we'd flushed first
throne.flush();
// assert !throne.isYucky(); // works in java 1.4
throne.deposit(new TrashWaste("cigarettes"));
throne.flush(); // might overflow
System.out.println("All done. No need to destroy toilet; it will be garbage-collected.");
}
}