// File: Washer.java from the package edu.colorado.simulations
// Additional javadoc documentation is available from the Washer link at
// http://www.cs.colorado.edu/~main/docs/
package edu.colorado.simulations;
/******************************************************************************
* An Washer
simulates a simple washing machine.
*
* Java Source Code for this class:
*
* http://www.cs.colorado.edu/~main/edu/colorado/simulations/Washer.java
*
*
* @author Michael Main
* (main@colorado.edu)
*
* @version Feb 10, 2016
******************************************************************************/
public class Washer
{
private int secondsForWash; // Seconds for a single wash
private int washTimeLeft; // Seconds until this washer is no longer busy
/**
* Initialize a washer.
* @param s
* the number of seconds required for one wash cyle of this washer
* Postcondition:
* This washer has been initialized so that it takes s
* seconds to complete one wash cycle.
**/
public Washer(int s)
{
secondsForWash = s;
washTimeLeft =0;
}
/**
* Determine whether this washer is currently busy.
* @return
* true
if this washer is busy (in a wash cycle);
* otherwise false
**/
public boolean isBusy( )
{
return (washTimeLeft > 0);
}
/**
* Reduce the remaining time in the current wash cycle by one second.
* Postcondition:
* If a car is being washed, then the remaining time in the current
* wash cycle has been reduced by one second.
**/
public void reduceRemainingTime( )
{
if (washTimeLeft > 0)
washTimeLeft--;
}
/**
* Start a wash cycle going for this washer.
* Precondition:
* isBusy()
is false
.
* Postcondition:
* This washer has started simulating one wash cycle. Therefore,
* isBusy()
will return true
until the required
* number of simulated seonds have passed.
* @exception IllegalStateException
* Indicates that this washer is busy.
**/
public void startWashing( )
{
if (washTimeLeft > 0)
throw new IllegalStateException("Washer is already busy.");
washTimeLeft = secondsForWash;
}
}