Compare commits

...

2 Commits

Author SHA1 Message Date
a021a64624 Merge branch 'main' of https://git.sfs.ddnss.org/peet/oop 2024-05-16 18:07:21 +02:00
65da2599bd meh 2024-05-16 18:07:20 +02:00
36 changed files with 559 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,9 @@
package uebung05.logistics;
public class Demo {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,5 @@
package uebung05.logistics;
public class Helicopter extends Vehicle{
}

View File

@ -0,0 +1,60 @@
package uebung05.logistics;
import java.util.Map;
import uebung05.logistics.quantities.FloatUnit;
import uebung05.logistics.quantities.IntUnit;
public class NeedCollector {
private Map<IntUnit, Integer> intNeeded;
private Map<FloatUnit, Float> floatNeeded;
/**
* add specific amount of operating material
*
* @param amount amount to be added
* @param unit type of operating material to be added
*/
public void add(int amount, IntUnit unit) {
}
/**
* add specific amount of operating material
*
* @param amount amount to be added
* @param unit type of operating material to be added
*/
public void add(float amount, FloatUnit unit) {
}
/**
* get the need of a specific operating material
*
* @param unit the type of operating material
* @return all the needs of the specific material
*/
public int getNeeded(IntUnit unit) {
return 0;
}
/**
* get the need of a specific operating material
*
* @param unit the type of operating material
* @return all the needs of the specific material
*/
public float getNeeded(FloatUnit unit) {
return 0;
}
/**
* Prints the total requirements in the therminal
*
*/
public void show(){
}
}

View File

@ -0,0 +1,5 @@
package uebung05.logistics;
public class Tank extends Vehicle{
}

View File

@ -0,0 +1,17 @@
package uebung05.logistics;
import java.util.HashMap;
import java.util.Map;
import uebung05.logistics.material.Oil;
import uebung05.logistics.quantities.IntUnit;
public class Truck extends Vehicle {
public Truck(String name){
this.name = name;
materials = new HashMap<>();
materials.put(Oil.getInstance(0), 5);
}
}

View File

@ -0,0 +1,16 @@
package uebung05.logistics;
import java.util.Map;
import uebung05.logistics.quantities.IntUnit;
public abstract class Vehicle{
protected String name;
protected Map<IntUnit, Integer> materials;
protected abstract void reportNeeds(NeedCollector collector);
protected abstract void fillUpAll();
protected abstract void consumeAll(int intensityRate);
}

View File

@ -0,0 +1,24 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.IntUnit;
public class BulletBelts implements IntUnit{
private static BulletBelts instance;
private int amount;
private BulletBelts(int amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " belts of 7.62 bullets";
}
public static BulletBelts getInstance(int amount){
if (instance == null){
instance = new BulletBelts(amount);
}
return instance;
}
}

View File

@ -0,0 +1,25 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.IntUnit;
public class Grease implements IntUnit{
private static Grease instance;
private int amount;
private Grease(int amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " units of grease";
}
public static Grease getInstance(int amount){
if (instance == null){
instance = new Grease(amount);
}
return instance;
}
}

View File

@ -0,0 +1,25 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.FloatUnit;
public class LiterDiesel implements FloatUnit{
private static LiterDiesel instance;
private float amount;
private LiterDiesel(float amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " liters of diesel";
}
public static LiterDiesel getInstance(float amount){
if (instance == null){
instance = new LiterDiesel(amount);
}
return instance;
}
}

View File

@ -0,0 +1,25 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.FloatUnit;
public class MetGallonsKerosene implements FloatUnit{
private static MetGallonsKerosene instance;
private float amount;
private MetGallonsKerosene(float amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " met gallons of kerosene";
}
public static MetGallonsKerosene getInstance(float amount){
if (instance == null){
instance = new MetGallonsKerosene(amount);
}
return instance;
}
}

View File

@ -0,0 +1,24 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.IntUnit;
public class Oil implements IntUnit{
private static Oil instance;
private int amount;
private Oil(int amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " units of oil";
}
public static Oil getInstance(int amount){
if (instance == null){
instance = new Oil(amount);
}
return instance;
}
}

View File

@ -0,0 +1,24 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.IntUnit;
public class RocketPods implements IntUnit{
private static RocketPods instance;
private int amount;
private RocketPods(int amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " pods of 70mm rockets";
}
public static RocketPods getInstance(int amount){
if (instance == null){
instance = new RocketPods(amount);
}
return instance;
}
}

View File

@ -0,0 +1,25 @@
package uebung05.logistics.material;
import uebung05.logistics.quantities.IntUnit;
public class ShellBatches implements IntUnit{
private static ShellBatches instance;
private int amount;
private ShellBatches(int amount){
this.amount = amount;
}
@Override
public String toString() {
return amount + " batches of 120mm shells";
}
public static ShellBatches getInstance(int amount){
if (instance == null){
instance = new ShellBatches(amount);
}
return instance;
}
}

View File

@ -0,0 +1,5 @@
package uebung05.logistics.quantities;
public interface FloatUnit {
}

View File

@ -0,0 +1,5 @@
package uebung05.logistics.quantities;
public interface IntUnit {
}

View File

@ -0,0 +1,60 @@
package uebung05.logistics.storage;
import uebung05.logistics.NeedCollector;
import uebung05.logistics.quantities.FloatUnit;
public class FloatStorage implements FloatUnit{
private float stored;
private final FloatUnit unit;
private final float max;
public FloatStorage(float stored, FloatUnit unit, float max){
this.stored = stored;
this.unit = unit;
this.max = max;
}
public float getStored(){
return stored;
}
public FloatUnit getUnit(){
return unit;
}
public float getMax(){
return max;
}
@Override
public String toString() {
return "storage with "+ stored + " of " + max + " units of " + unit;
}
public float consume(float amount){
float actualConsumed = Math.min(stored, amount);
stored = stored - actualConsumed;
return actualConsumed;
}
public void fill(float amount){
if (stored+amount > max) {
stored = max;
} else {
stored = stored + amount;
}
}
public void fillUp(){
stored = max;
}
public void reportNeed(NeedCollector collector){
collector.add(max-stored, unit);
}
}

View File

@ -0,0 +1,58 @@
package uebung05.logistics.storage;
import uebung05.logistics.NeedCollector;
import uebung05.logistics.quantities.IntUnit;
public class IntStorage implements IntUnit {
private int stored;
private final IntUnit unit;
private final int max;
public IntStorage(int stored, IntUnit unit, int max){
this.stored = stored;
this.unit = unit;
this.max = max;
}
public int getStored(){
return stored;
}
public IntUnit getUnit(){
return unit;
}
public int getMax(){
return max;
}
@Override
public String toString() {
return "storage with "+ stored + " of " + max + " units of " + unit;
}
public int consume(int amount){
int actualConsumed = Math.min(stored, amount);
stored = stored - actualConsumed;
return actualConsumed;
}
public void fill(int amount){
if (stored+amount > max) {
stored = max;
} else {
stored = stored + amount;
}
}
public void fillUp(){
stored = max;
}
public void reportNeed(NeedCollector collector){
collector.add(max-stored, unit);
}
}

View File

@ -0,0 +1,67 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.Grease;
import logistics.material.LiterDiesel;
import logistics.material.MetGallonsKerosene;
import logistics.material.Oil;
import logistics.material.RocketPods;
import logistics.material.ShellBatches;
import logistics.quantities.NeedCollector;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ManagerTest {
private static final float EPS = 1e-5f;
private Manager manager;
@Before
public void setup() {
manager = new Manager();
manager.addVehicle(new Tank("Leo1"));
manager.addVehicle(new Tank("Leo2"));
manager.addVehicle(new Helicopter("Tiger1"));
manager.addVehicle(new Helicopter("Tiger2"));
}
@Test
public void testInitialNeed() {
final NeedCollector collector = manager.collectNeeds();
assertEquals(4, collector.getNeed(RocketPods.INSTANCE));
assertEquals(12, collector.getNeed(Oil.INSTANCE));
assertEquals(24, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(20, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(12, collector.getNeed(Grease.INSTANCE));
assertEquals(1000f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(2400f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
@Test
public void testNeedAfterFillUp() {
manager.fillUpVehicles();
final NeedCollector collector = manager.collectNeeds();
assertEquals(0, collector.getNeed(RocketPods.INSTANCE));
assertEquals(0, collector.getNeed(Oil.INSTANCE));
assertEquals(0, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(0, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(0, collector.getNeed(Grease.INSTANCE));
assertEquals(0f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(0f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
@Test
public void testNeedAfterLogTick() {
manager.fillUpVehicles();
manager.logTick(1);
final NeedCollector collector = manager.collectNeeds();
assertEquals(2, collector.getNeed(RocketPods.INSTANCE));
assertEquals(4, collector.getNeed(Oil.INSTANCE));
assertEquals(6, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(4, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(4, collector.getNeed(Grease.INSTANCE));
assertEquals(100f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(360f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
}

View File

@ -0,0 +1,80 @@
package logistics.storage;
import logistics.material.Oil;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntStorageTest {
private static final int MAX = 3;
private IntStorage intStorage;
@Before
public void setup() {
intStorage = new IntStorage(0, Oil.INSTANCE, MAX);
}
@Test
public void testEmptyStorage() {
assertEquals(0, intStorage.getStored());
}
@Test
public void testGetStorage() {
intStorage.fill(1);
assertEquals(1, intStorage.getStored());
}
@Test
public void testGetMax() {
assertEquals(MAX, intStorage.getMax());
}
@Test
public void testFillUp() {
intStorage.fillUp();
assertEquals(MAX, intStorage.getStored());
}
@Test
public void testUpperBound() {
intStorage.fill(MAX + 1);
assertEquals(MAX, intStorage.getStored());
}
@Test
public void testConsume() {
intStorage.fillUp();
assertEquals(MAX, intStorage.getStored());
assertEquals(MAX, intStorage.consume(MAX));
}
@Test
public void testLowerBound() {
intStorage.consume(1);
assertEquals(0, intStorage.getStored());
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalFill() {
intStorage.fill(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalConsume() {
intStorage.consume(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalLowerBoundConstructor() {
new IntStorage(-1, Oil.INSTANCE, MAX);
}
@Test
public void testUpperBoundConstructor() {
final IntStorage storage = new IntStorage(MAX + 1, Oil.INSTANCE, MAX);
assertEquals(MAX, storage.getStored());
}
}