You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
678 B
JavaScript
40 lines
678 B
JavaScript
5 years ago
|
class Bucket {
|
||
|
constructor({ capacity, volume = 0 }) {
|
||
|
this.capacity = capacity
|
||
|
this.volume = volume
|
||
|
}
|
||
|
|
||
|
fill() {
|
||
|
this.volume = this.capacity
|
||
|
}
|
||
|
|
||
|
empty() {
|
||
|
this.volume = 0
|
||
|
}
|
||
|
|
||
|
isEmpty() {
|
||
|
return this.volume === 0
|
||
|
}
|
||
|
|
||
|
isFull() {
|
||
|
return this.volume == this.capacity
|
||
|
}
|
||
|
|
||
|
pour(bucket) {
|
||
|
const availableVolume = bucket.capacity - bucket.volume
|
||
|
if (availableVolume > this.volume) {
|
||
|
bucket.volume += this.volume
|
||
|
this.volume = 0
|
||
|
} else {
|
||
|
this.volume -= availableVolume
|
||
|
bucket.volume += availableVolume
|
||
|
}
|
||
|
}
|
||
|
|
||
|
string() {
|
||
|
return `${this.volume}vol/${this.capacity}cap`
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Bucket;
|