/**
* המחלקה מגדירה דלי בעל קיבולת כלשהי
* @author MisterPick
* @version 1.0.0
* @since 27/04/12
*/
public class Bucket
{
private int capacity;
private double currentAmount;
/**
* הפעולה בונה דלי שקיבולתו היא הפרמטר capacity.
* הנחה: קיבולת הדלי היא מספר אי שלילי
* @param capacity קיבולת הדלי
*/
public Bucket (int capacity)
{
this.capacity = capacity;
this.currentAmount = 0;
}
public Bucket (int capacity, int currentAmount)
{
this.capacity = capacity;
this.currentAmount = currentAmount;
}
public Bucket (Bucket b1)
{
this.capacity = b1.capacity;
this.currentAmount = b1.currentAmount;
}
public void empty ()
{
this.currentAmount = 0;
}
public boolean isEmpty ()
{
return (this.currentAmount == 0);
}
public void fill (double amountToFill)
{
this.currentAmount += amountToFill;
if(this.currentAmount > this.capacity)
this.currentAmount = this.capacity;
}
public int getCapacity ()
{
return this.capacity;
}
public double getCurrentAmount ()
{
return this.currentAmount;
}
public void pourInto (Bucket bucketInto)
{
double freeSpace = bucketInto.getCapacity() - bucketInto.getCurrentAmount();
if(freeSpace <= this.currentAmount)
{
bucketInto.fill(this.currentAmount);
this.empty();
}
else
{
bucketInto.fill(freeSpace);
this.currentAmount -= freeSpace;
}
}
public String toString ()
{
return ("A bucket: " + this.currentAmount + " liter full of " + (double)this.capacity + " liter");
}
}