Pampour

OilTank.java

Oct 25th, 2013
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class OilTank {
  2.     int capacity = 5000;
  3.     int contents = 0;
  4.    
  5.     public int getCapacity() {
  6.         return this.capacity;
  7.     }
  8.    
  9.     public int getContents() {
  10.         return this.contents;      
  11.     }
  12.    
  13.     private void setCapacity(int capacity) {
  14.         if (capacity > 0) {
  15.             this.capacity = capacity;
  16.         }
  17.     }
  18.    
  19.     private void setContents(int contents) {
  20.         if (contents >= 0 && contents <= this.capacity) {
  21.             this.contents = contents;
  22.         } else if (contents > this.capacity) {
  23.             this.contents = this.capacity;
  24.         }
  25.     }
  26.    
  27.     public OilTank() {
  28.         // Initialize using default values
  29.     }
  30.    
  31.     public OilTank(int capacity) {
  32.         this.setCapacity(capacity);
  33.     }
  34.    
  35.     public OilTank(int capacity, int contents) {
  36.         this.setCapacity(capacity);
  37.         this.setContents(contents);
  38.     }
  39.    
  40.     public boolean isFull() {
  41.         if (this.getCapacity() == this.getContents()) {
  42.             return true;
  43.         } else {
  44.             return false;
  45.         }
  46.     }
  47.    
  48.     public boolean isEmpty() {
  49.         if (this.getContents() == 0) {
  50.             return true;
  51.         } else {
  52.             return false;
  53.         }
  54.     }
  55.    
  56.     public boolean isZelfde(OilTank other) {
  57.         if (other != null && this.getContents() == other.getContents() && this.getCapacity() == other.getCapacity()) {
  58.             return true;
  59.         } else {
  60.             return false;
  61.         }
  62.     }
  63.    
  64.     public String toString() {
  65.         return "Capaciteit: " + this.getCapacity() + ", Contents: " + this.getContents();
  66.     }
  67.    
  68.     public boolean fill() {
  69.         this.setContents(this.capacity);
  70.         return true;
  71.     }
  72.    
  73.     public boolean fill(int amount){
  74.         boolean result = false;
  75.        
  76.         if (amount >= 0 && amount <= this.getCapacity() - this.getContents()) {
  77.             this.setContents(this.getContents()+amount);
  78.             result = true;
  79.         } else if (amount > 0 && amount >= this.getCapacity()-this.getContents()) {
  80.             this.setContents(this.getCapacity());
  81.         }
  82.        
  83.         return result;
  84.     }
  85.    
  86.     public int extract() {
  87.         int temp = this.getContents();
  88.         this.setContents(0);
  89.         return temp;
  90.     }
  91.    
  92.     public int extract(int amount) {
  93.         int temp = this.getContents();
  94.         if (amount > 0) {
  95.             if (temp - amount < 0) {
  96.                 this.setContents(0);
  97.                 return temp;
  98.             } else {
  99.                 this.setContents(this.getContents() - amount);
  100.                 return amount;
  101.             }
  102.         }
  103.         return -1;
  104.     }
  105.    
  106.     public void transferFrom(OilTank other) {
  107.         if (other != null) {
  108.             int transfer = other.getContents()-(this.getCapacity()-this.getContents());
  109.             this.setContents(this.getContents()+transfer);
  110.             other.setContents(other.getContents()-transfer);
  111.         }
  112.        
  113.     }
  114.    
  115.     public void isRelativelyFullerThan(OilTank other) {
  116.         if (other != null) {
  117.             float a = this.getContents() / this.getCapacity();
  118.             float b = other.getContents() / other.getCapacity();
  119.            
  120.             if (a < b) {
  121.                 System.out.println("De eerste olietank is voller dan de tweede.");
  122.             } else if (a > b) {
  123.                 System.out.println("De tweede olietank is voller dan de eerste.");
  124.             } else {
  125.                 System.out.println("De twee olietanken zijn relatief gelijk aan elkaar.");
  126.             }  
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment