Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OilTank {
- int capacity = 5000;
- int contents = 0;
- public int getCapacity() {
- return this.capacity;
- }
- public int getContents() {
- return this.contents;
- }
- private void setCapacity(int capacity) {
- if (capacity > 0) {
- this.capacity = capacity;
- }
- }
- private void setContents(int contents) {
- if (contents >= 0 && contents <= this.capacity) {
- this.contents = contents;
- } else if (contents > this.capacity) {
- this.contents = this.capacity;
- }
- }
- public OilTank() {
- // Initialize using default values
- }
- public OilTank(int capacity) {
- this.setCapacity(capacity);
- }
- public OilTank(int capacity, int contents) {
- this.setCapacity(capacity);
- this.setContents(contents);
- }
- public boolean isFull() {
- if (this.getCapacity() == this.getContents()) {
- return true;
- } else {
- return false;
- }
- }
- public boolean isEmpty() {
- if (this.getContents() == 0) {
- return true;
- } else {
- return false;
- }
- }
- public boolean isZelfde(OilTank other) {
- if (other != null && this.getContents() == other.getContents() && this.getCapacity() == other.getCapacity()) {
- return true;
- } else {
- return false;
- }
- }
- public String toString() {
- return "Capaciteit: " + this.getCapacity() + ", Contents: " + this.getContents();
- }
- public boolean fill() {
- this.setContents(this.capacity);
- return true;
- }
- public boolean fill(int amount){
- boolean result = false;
- if (amount >= 0 && amount <= this.getCapacity() - this.getContents()) {
- this.setContents(this.getContents()+amount);
- result = true;
- } else if (amount > 0 && amount >= this.getCapacity()-this.getContents()) {
- this.setContents(this.getCapacity());
- }
- return result;
- }
- public int extract() {
- int temp = this.getContents();
- this.setContents(0);
- return temp;
- }
- public int extract(int amount) {
- int temp = this.getContents();
- if (amount > 0) {
- if (temp - amount < 0) {
- this.setContents(0);
- return temp;
- } else {
- this.setContents(this.getContents() - amount);
- return amount;
- }
- }
- return -1;
- }
- public void transferFrom(OilTank other) {
- if (other != null) {
- int transfer = other.getContents()-(this.getCapacity()-this.getContents());
- this.setContents(this.getContents()+transfer);
- other.setContents(other.getContents()-transfer);
- }
- }
- public void isRelativelyFullerThan(OilTank other) {
- if (other != null) {
- float a = this.getContents() / this.getCapacity();
- float b = other.getContents() / other.getCapacity();
- if (a < b) {
- System.out.println("De eerste olietank is voller dan de tweede.");
- } else if (a > b) {
- System.out.println("De tweede olietank is voller dan de eerste.");
- } else {
- System.out.println("De twee olietanken zijn relatief gelijk aan elkaar.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment