oopuebung/uebung05/src/logistics/storage/IntStorage.java
2025-05-13 18:13:15 +02:00

61 lines
1.3 KiB
Java

package logistics.storage;
import logistics.quantities.IntUnit;
import logistics.quantities.NeedCollector;
public class IntStorage {
private int stored;
private final IntUnit unit;
private final int max;
public IntStorage(int stored, IntUnit unit, int max) {
if (stored < 0 || max < 0)
throw new IllegalArgumentException("negative");
this.stored = Math.min(stored, max);
this.unit = unit;
this.max = max;
}
public int consume(int amount) {
if (amount < 0)
throw new IllegalArgumentException("negative");
int result = Math.min(amount, stored);
stored -= result;
return result;
}
public void fill(int amount) {
if (amount < 0) throw new IllegalArgumentException("negative");
stored = Math.min(stored + amount, max);
}
public void fillUp() {
stored = max;
}
public void reportNeed(NeedCollector collector) {
collector.add(max - stored, unit);
}
public int getStored() {
return stored;
}
public int getMax() {
return max;
}
public IntUnit getUnit() {
return unit;
}
@Override
public String toString() {
return "storage with " + stored + " of " + max + " " + unit;
}
}