Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: Java  |  size: 1.67 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * המחלקה מגדירה דלי בעל קיבולת כלשהי
  3.  * @author MisterPick
  4.  * @version 1.0.0
  5.  * @since 27/04/12
  6.  */
  7. public class Bucket
  8. {
  9.         private int capacity;
  10.         private double currentAmount;
  11.        
  12.         /**
  13.          * הפעולה בונה דלי שקיבולתו היא הפרמטר capacity.
  14.          * הנחה: קיבולת הדלי היא מספר אי שלילי
  15.          * @param capacity קיבולת הדלי
  16.          */
  17.         public Bucket (int capacity)
  18.         {
  19.                 this.capacity = capacity;
  20.                 this.currentAmount = 0;
  21.         }
  22.  
  23.         public Bucket (int capacity, int currentAmount)
  24.         {
  25.                 this.capacity = capacity;
  26.                 this.currentAmount = currentAmount;
  27.         }
  28.        
  29.         public Bucket (Bucket b1)
  30.         {
  31.                 this.capacity = b1.capacity;
  32.                 this.currentAmount = b1.currentAmount;
  33.         }
  34.        
  35.         public void empty ()
  36.         {
  37.                 this.currentAmount = 0;
  38.         }
  39.        
  40.         public boolean isEmpty ()
  41.         {
  42.                 return (this.currentAmount == 0);
  43.         }
  44.        
  45.         public void fill (double amountToFill)
  46.         {
  47.                 this.currentAmount += amountToFill;
  48.                 if(this.currentAmount > this.capacity)
  49.                         this.currentAmount = this.capacity;
  50.         }
  51.        
  52.         public int getCapacity ()
  53.         {
  54.                 return this.capacity;
  55.         }
  56.        
  57.         public double getCurrentAmount ()
  58.         {
  59.                 return this.currentAmount;
  60.         }
  61.        
  62.         public void pourInto (Bucket bucketInto)
  63.         {
  64.                 double freeSpace = bucketInto.getCapacity() - bucketInto.getCurrentAmount();
  65.                 if(freeSpace <= this.currentAmount)
  66.                 {
  67.                         bucketInto.fill(this.currentAmount);
  68.                         this.empty();
  69.                 }
  70.                 else
  71.                 {
  72.                         bucketInto.fill(freeSpace);
  73.                         this.currentAmount -= freeSpace;
  74.                 }
  75.                
  76.         }
  77.        
  78.         public String toString ()
  79.         {              
  80.                 return ("A bucket: " + this.currentAmount + " liter full of " + (double)this.capacity + " liter");
  81.         }
  82.        
  83.        
  84.        
  85.        
  86. }