updated uebung05 from lsg
This commit is contained in:
parent
a021a64624
commit
c60c85ade0
37
src/uebung05/logistics/Demo.java
Normal file → Executable file
37
src/uebung05/logistics/Demo.java
Normal file → Executable file
@ -1,9 +1,38 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
public class Demo {
|
||||
import logistics.material.BulletBelts;
|
||||
import logistics.material.LiterDiesel;
|
||||
import logistics.material.RocketPods;
|
||||
|
||||
class demo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
Manager ceo = new Manager();
|
||||
|
||||
Vehicle panther = new Tank("Panther");
|
||||
Vehicle leo = new Tank("Leopard 2A7");
|
||||
Vehicle tiger = new Helicopter("Tiger");
|
||||
Vehicle h125m = new Helicopter("H145M");
|
||||
Vehicle husky = new Truck("BV206-S Hägglunds");
|
||||
Vehicle zwoTonner = new Truck("Unimog2000");
|
||||
|
||||
ceo.addVehicle(panther);
|
||||
ceo.addVehicle(leo);
|
||||
ceo.addVehicle(tiger);
|
||||
ceo.addVehicle(h125m);
|
||||
ceo.addVehicle(husky);
|
||||
ceo.addVehicle(zwoTonner);
|
||||
|
||||
ceo.showOverallNeed();
|
||||
ceo.showNeed(LiterDiesel.getINSTANCE());
|
||||
ceo.fillUpVehicles();
|
||||
ceo.showOverallNeed();
|
||||
ceo.logTick(2);
|
||||
ceo.showOverallNeed();
|
||||
ceo.showNeed(RocketPods.getINSTANCE());
|
||||
ceo.fillUpVehicles();
|
||||
ceo.logTick(1);
|
||||
ceo.showOverallNeed();
|
||||
ceo.showNeed(BulletBelts.getINSTANCE());
|
||||
}
|
||||
}
|
||||
|
115
src/uebung05/logistics/Helicopter.java
Normal file → Executable file
115
src/uebung05/logistics/Helicopter.java
Normal file → Executable file
@ -1,5 +1,114 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
public class Helicopter extends Vehicle{
|
||||
import logistics.material.BulletBelts;
|
||||
import logistics.material.MetGallonsKerosene;
|
||||
import logistics.material.RocketPods;
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
import logistics.storage.FloatStorage;
|
||||
import logistics.storage.IntStorage;
|
||||
|
||||
}
|
||||
/**
|
||||
* The Helicopter class represents a helicopter in the logistics system.
|
||||
* It extends the Vehicle abstract class and includes a tank for kerosene,
|
||||
* a storage for bullet belts, and a storage for rocket pods, and the necessary methods
|
||||
* for reporting needs, filling up, and consuming supplies.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Helicopter extends Vehicle {
|
||||
|
||||
FloatStorage tank;
|
||||
IntStorage bullets;
|
||||
IntStorage rockets;
|
||||
|
||||
private final static int maxAmmunition = 2;
|
||||
private final static int maxFuel = 500;
|
||||
|
||||
/**
|
||||
* Construct a Helicopter object with a name, kerosene amount, bullet belts amount, and rocket pods amount
|
||||
*
|
||||
* @param name The name of the helicopter
|
||||
* @param kerosine The initial amount of kerosene for the helicopter
|
||||
* @param bulletsBelts The initial amount of bullet belts for the helicopter
|
||||
* @param rocketPods The initial amount of rocket pods for the helicopter
|
||||
*/
|
||||
public Helicopter(String name, float kerosine, int bulletsBelts, int rocketPods) {
|
||||
super(name, 3, 3);
|
||||
|
||||
this.bullets = new IntStorage(bulletsBelts, BulletBelts.getINSTANCE(), maxAmmunition);
|
||||
this.rockets = new IntStorage(rocketPods, RocketPods.getINSTANCE(), maxAmmunition);
|
||||
this.tank = new FloatStorage(kerosine, MetGallonsKerosene.getINSTANCE(), maxFuel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Helicopter object with a name, kerosene amount, bullet belts amount, and rocket pods amount
|
||||
*
|
||||
* @param name The name of the helicopter
|
||||
*/
|
||||
public Helicopter(String name) {
|
||||
this(name, 0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report the needs of the helicopter to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
@Override
|
||||
public void reportNeeds(NeedCollector collector) {
|
||||
tank.reportNeed(collector);
|
||||
bullets.reportNeed(collector);
|
||||
rockets.reportNeed(collector);
|
||||
super.reportBasicNeeds(collector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill up all the supplies for the helicopter
|
||||
*/
|
||||
@Override
|
||||
public void fillUpAll() {
|
||||
tank.fillUp();
|
||||
bullets.fillUp();
|
||||
rockets.fillUp();
|
||||
super.fillUpBasic();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the helicopter's supplies based on the intensity rate
|
||||
*
|
||||
* @param intensityRate The intensity rate for the consumption
|
||||
*/
|
||||
@Override
|
||||
public void consumeAll(int intensityRate) {
|
||||
tank.consume(intensityRate*200);
|
||||
bullets.consume(intensityRate);
|
||||
rockets.consume(intensityRate);
|
||||
super.consumeBasic();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the helicopter needs a specific IntUnit object
|
||||
*
|
||||
* @param unit The IntUnit object for which to check the need
|
||||
* @return True if the helicopter needs the IntUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(IntUnit unit) {
|
||||
return unit == bullets.getUnit() || unit == rockets.getUnit() || unit == oil.getUnit() || unit == grease.getUnit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the helicopter needs a specific FloatUnit object
|
||||
*
|
||||
* @param unit The FloatUnit object for which to check the need
|
||||
* @return True if the helicopter needs the FloatUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(FloatUnit unit) {
|
||||
return unit == tank.getUnit();
|
||||
}
|
||||
}
|
120
src/uebung05/logistics/Manager.java
Executable file
120
src/uebung05/logistics/Manager.java
Executable file
@ -0,0 +1,120 @@
|
||||
package logistics;
|
||||
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
import logistics.quantities.Unit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Manager class manages a list of Vehicle objects and provides methods for adding, filling up, and logging ticks for the vehicles.
|
||||
* It also includes methods for collecting and displaying the overall needs of the vehicles and the needs for specific IntUnit or FloatUnit objects.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Manager {
|
||||
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
public Manager() {
|
||||
this.vehicles = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Vehicle object to the list of vehicles
|
||||
*
|
||||
* @param vehicle The Vehicle object to be added
|
||||
*/
|
||||
public void addVehicle(Vehicle vehicle) {
|
||||
vehicles.add(vehicle);
|
||||
System.out.println("Boris bought a new " + vehicle.getClass().getSimpleName() + " " + vehicle + ".");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill up all the vehicles in the list
|
||||
*/
|
||||
public void fillUpVehicles() {
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
vehicle.fillUpAll();
|
||||
}
|
||||
System.out.println("All vehicles filled and ready for combat.");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a tick for all the vehicles in the list, consuming their supplies based on the intensity rate
|
||||
*
|
||||
* @param intensityRate The intensity rate for the tick
|
||||
*/
|
||||
public void logTick(int intensityRate) {
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
vehicle.consumeAll(intensityRate);
|
||||
}
|
||||
System.out.println("All vehicles consumed because of " + intensityRate + " battle(s).");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the needs of all the vehicles in the list and return a NeedCollector object
|
||||
*
|
||||
* @return The NeedCollector object containing the needs of all the vehicles
|
||||
*/
|
||||
NeedCollector collectNeeds() {
|
||||
NeedCollector collector = new NeedCollector();
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
vehicle.reportNeeds(collector);
|
||||
}
|
||||
return collector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the overall needs of all the vehicles in the list
|
||||
*/
|
||||
public void showOverallNeed() {
|
||||
NeedCollector collector = collectNeeds();
|
||||
collector.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the vehicles needing a specific IntUnit object
|
||||
*
|
||||
* @param unit The IntUnit object for which to display the vehicles
|
||||
*/
|
||||
public void showNeed(IntUnit unit) {
|
||||
/*if (unit instanceof IntUnit) {
|
||||
IntUnit intUnit = (IntUnit) unit;
|
||||
} else if (unit instanceof FloatUnit) {
|
||||
FloatUnit floatUnit = (FloatUnit) unit;
|
||||
}*/
|
||||
System.out.println("Vehicles needing " + unit.toString() + ":");
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
if (vehicle.need(unit)) {
|
||||
NeedCollector collector = new NeedCollector();
|
||||
vehicle.reportNeeds(collector);
|
||||
System.out.println("- " + vehicle + " needs " + collector.getNeed(unit) + " " + unit);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the vehicles needing a specific FloatUnit object
|
||||
*
|
||||
* @param unit The FloatUnit object for which to display the vehicles
|
||||
*/
|
||||
public void showNeed(FloatUnit unit) {
|
||||
System.out.println("Vehicles needing " + unit.toString() + ":");
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
if (vehicle.need(unit)) {
|
||||
NeedCollector collector = new NeedCollector();
|
||||
vehicle.reportNeeds(collector);
|
||||
System.out.println("- " + vehicle + " needs " + collector.getNeed(unit) + " " + unit);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
68
src/uebung05/logistics/NeedCollector.java
Normal file → Executable file
68
src/uebung05/logistics/NeedCollector.java
Normal file → Executable file
@ -1,60 +1,38 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
import java.util.HashMap;
|
||||
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) {
|
||||
|
||||
public NeedCollector() {
|
||||
intNeeded = new HashMap<>();
|
||||
floatNeeded = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public void add (int amount, IntUnit unit) {
|
||||
intNeeded.put(unit, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public void add (float amount, FloatUnit unit) {
|
||||
floatNeeded.put(unit, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the total requirements in the therminal
|
||||
*
|
||||
*/
|
||||
public void show(){
|
||||
public int getNeed (IntUnit unit) {
|
||||
return intNeeded.get(unit);
|
||||
}
|
||||
|
||||
public float getNeed (FloatUnit unit) {
|
||||
return floatNeeded.get(unit);
|
||||
}
|
||||
|
||||
public void show () {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
113
src/uebung05/logistics/Tank.java
Normal file → Executable file
113
src/uebung05/logistics/Tank.java
Normal file → Executable file
@ -1,5 +1,112 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
public class Tank extends Vehicle{
|
||||
import logistics.material.BulletBelts;
|
||||
import logistics.material.LiterDiesel;
|
||||
import logistics.material.ShellBatches;
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
import logistics.storage.FloatStorage;
|
||||
import logistics.storage.IntStorage;
|
||||
|
||||
}
|
||||
/**
|
||||
* The Tank class represents a tank in the logistics system.
|
||||
* It extends the Vehicle abstract class and includes a tank for diesel, a storage for bullet belts,
|
||||
* and a storage for shell batches, and the necessary methods for reporting needs, filling up, and consuming supplies.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Tank extends Vehicle {
|
||||
|
||||
FloatStorage tank;
|
||||
IntStorage shell;
|
||||
IntStorage bullets;
|
||||
|
||||
private final static int maxAmmunition = 10;
|
||||
private final static int maxFuel = 1200;
|
||||
|
||||
/**
|
||||
* Construct a Tank object with a name, diesel amount, bullet belts amount, and shell batches amount
|
||||
*
|
||||
* @param name The name of the tank
|
||||
* @param diesel The initial amount of diesel for the tank
|
||||
* @param bulletBelts The initial amount of bullet belts for the tank
|
||||
* @param shellBatches The initial amount of shell batches for the tank
|
||||
*/
|
||||
public Tank(String name, float diesel, int bulletBelts, int shellBatches) {
|
||||
super(name, 3, 3);
|
||||
|
||||
this.shell = new IntStorage(shellBatches, ShellBatches.getINSTANCE(), maxAmmunition);
|
||||
this.bullets = new IntStorage(bulletBelts, BulletBelts.getINSTANCE(), maxAmmunition);
|
||||
this.tank = new FloatStorage(diesel, LiterDiesel.getINSTANCE(), maxFuel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Tank object with a name
|
||||
*
|
||||
* @param name The name of the tank
|
||||
*/
|
||||
public Tank(String name) {
|
||||
this(name, 0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report the needs of the tank to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
@Override
|
||||
public void reportNeeds(NeedCollector collector) {
|
||||
tank.reportNeed(collector);
|
||||
shell.reportNeed(collector);
|
||||
bullets.reportNeed(collector);
|
||||
super.reportBasicNeeds(collector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill up all the supplies for the tank
|
||||
*/
|
||||
@Override
|
||||
public void fillUpAll() {
|
||||
tank.fillUp();
|
||||
shell.fillUp();
|
||||
bullets.fillUp();
|
||||
super.fillUpBasic();
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the tank's supplies based on the intensity rate
|
||||
*
|
||||
* @param intensityRate The intensity rate for the consumption
|
||||
*/
|
||||
@Override
|
||||
public void consumeAll(int intensityRate) {
|
||||
tank.consume(intensityRate*180);
|
||||
bullets.consume(intensityRate*2);
|
||||
shell.consume(intensityRate*2);
|
||||
super.consumeBasic();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the tank needs a specific IntUnit object
|
||||
*
|
||||
* @param unit The IntUnit object for which to check the need
|
||||
* @return True if the tank needs the IntUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(IntUnit unit) {
|
||||
return unit == shell.getUnit() || unit == bullets.getUnit() || unit == oil.getUnit() || unit == grease.getUnit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the tank needs a specific FloatUnit object
|
||||
*
|
||||
* @param unit The FloatUnit object for which to check the need
|
||||
* @return True if the tank needs the FloatUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(FloatUnit unit) {
|
||||
return unit == tank.getUnit();
|
||||
}
|
||||
}
|
109
src/uebung05/logistics/Truck.java
Normal file → Executable file
109
src/uebung05/logistics/Truck.java
Normal file → Executable file
@ -1,17 +1,104 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import uebung05.logistics.material.Oil;
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.material.BulletBelts;
|
||||
import logistics.material.LiterDiesel;
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
import logistics.storage.FloatStorage;
|
||||
import logistics.storage.IntStorage;
|
||||
|
||||
/**
|
||||
* The Truck class represents a truck in the logistics system.
|
||||
* It extends the Vehicle abstract class and includes a tank for diesel, a storage for bullet belts, and the necessary methods for reporting needs, filling up, and consuming supplies.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Truck extends Vehicle {
|
||||
|
||||
public Truck(String name){
|
||||
this.name = name;
|
||||
materials = new HashMap<>();
|
||||
materials.put(Oil.getInstance(0), 5);
|
||||
FloatStorage tank;
|
||||
IntStorage bullets;
|
||||
|
||||
private final static int maxAmmunition = 3;
|
||||
private final static int maxFuel = 180;
|
||||
|
||||
/**
|
||||
* Construct a Truck object with a name, diesel amount, and bullet belts amount
|
||||
*
|
||||
* @param name The name of the truck
|
||||
* @param diesel The initial amount of diesel for the truck
|
||||
* @param bulletBelts The initial amount of bullet belts for the truck
|
||||
*/
|
||||
public Truck(String name, float diesel, int bulletBelts) {
|
||||
super(name, 3, 3);
|
||||
|
||||
tank = new FloatStorage(diesel, LiterDiesel.getINSTANCE(), maxFuel);
|
||||
bullets = new IntStorage(bulletBelts, BulletBelts.getINSTANCE(), maxAmmunition);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Construct a Truck object with a name, diesel amount, and bullet belts amount
|
||||
*
|
||||
* @param name The name of the truck
|
||||
*/
|
||||
public Truck(String name) {
|
||||
this(name, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report the needs of the truck to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
@Override
|
||||
public void reportNeeds(NeedCollector collector) {
|
||||
tank.reportNeed(collector);
|
||||
bullets.reportNeed(collector);
|
||||
super.reportBasicNeeds(collector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill up all the supplies for the truck
|
||||
*/
|
||||
@Override
|
||||
public void fillUpAll() {
|
||||
tank.fillUp();
|
||||
bullets.fillUp();
|
||||
super.fillUpBasic();
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the truck's supplies based on the intensity rate
|
||||
*
|
||||
* @param intensityRate The intensity rate for the consumption
|
||||
*/
|
||||
@Override
|
||||
public void consumeAll(int intensityRate) {
|
||||
tank.consume(intensityRate*25);
|
||||
bullets.consume(intensityRate*2);
|
||||
super.consumeBasic();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the truck needs a specific IntUnit object
|
||||
*
|
||||
* @param unit The IntUnit object for which to check the need
|
||||
* @return True if the truck needs the IntUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(IntUnit unit) {
|
||||
return unit == bullets.getUnit() || unit == oil.getUnit() || unit == grease.getUnit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the truck needs a specific FloatUnit object
|
||||
*
|
||||
* @param unit The FloatUnit object for which to check the need
|
||||
* @return True if the truck needs the FloatUnit object, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public Boolean need(FloatUnit unit) {
|
||||
return unit == tank.getUnit();
|
||||
}
|
||||
}
|
109
src/uebung05/logistics/Vehicle.java
Normal file → Executable file
109
src/uebung05/logistics/Vehicle.java
Normal file → Executable file
@ -1,16 +1,105 @@
|
||||
package uebung05.logistics;
|
||||
package logistics;
|
||||
|
||||
import java.util.Map;
|
||||
import logistics.material.Grease;
|
||||
import logistics.material.Oil;
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
import logistics.storage.IntStorage;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
/**
|
||||
* The Vehicle abstract class represents a vehicle in the logistics system.
|
||||
* It includes an abstract reportNeeds method for reporting the needs of the vehicle,
|
||||
* an abstract fillUpAll method for filling up the vehicle, an abstract consumeAll method for
|
||||
* consuming the vehicle's supplies, and abstract need methods for checking if the vehicle needs
|
||||
* a specific IntUnit or FloatUnit object.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public abstract class Vehicle {
|
||||
|
||||
public abstract class Vehicle{
|
||||
protected String name;
|
||||
protected Map<IntUnit, Integer> materials;
|
||||
String name;
|
||||
IntStorage oil;
|
||||
IntStorage grease;
|
||||
|
||||
protected abstract void reportNeeds(NeedCollector collector);
|
||||
private final static int maxOil = 3;
|
||||
private final static int maxGrease = 3;
|
||||
|
||||
protected abstract void fillUpAll();
|
||||
/**
|
||||
* Construct a Vehicle object with a name, oil storage, and grease storage
|
||||
*
|
||||
* @param name The name of the vehicle
|
||||
* @param oil The initial amount of oil for the vehicle
|
||||
* @param grease The initial amount of grease for the vehicle
|
||||
*/
|
||||
protected Vehicle(String name, int oil, int grease) {
|
||||
this.name = name;
|
||||
this.oil = new IntStorage(oil, Oil.getINSTANCE(), maxOil);
|
||||
this.grease = new IntStorage(grease, Grease.getINSTANCE(), maxGrease);
|
||||
}
|
||||
|
||||
protected abstract void consumeAll(int intensityRate);
|
||||
}
|
||||
/**
|
||||
* Fill up oil and grease.
|
||||
*/
|
||||
public void fillUpBasic () {
|
||||
oil.fillUp();
|
||||
grease.fillUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume oil and grease.
|
||||
*/
|
||||
public void consumeBasic () {
|
||||
oil.consume(1);
|
||||
grease.consume(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report needs of oil and grease.
|
||||
*/
|
||||
public void reportBasicNeeds (NeedCollector collector) {
|
||||
oil.reportNeed(collector);
|
||||
grease.reportNeed(collector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report the needs of the vehicle to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
public abstract void reportNeeds(NeedCollector collector);
|
||||
|
||||
/**
|
||||
* Fill up all the supplies for the vehicle
|
||||
*/
|
||||
public abstract void fillUpAll();
|
||||
|
||||
/**
|
||||
* Consume the vehicle's supplies based on the intensity rate
|
||||
*
|
||||
* @param intensityRate The intensity rate for the consumption
|
||||
*/
|
||||
public abstract void consumeAll(int intensityRate);
|
||||
|
||||
/**
|
||||
* Check if the vehicle needs a specific IntUnit object
|
||||
*
|
||||
* @param unit The IntUnit object for which to check the need
|
||||
* @return True if the vehicle needs the IntUnit object, false otherwise
|
||||
*/
|
||||
public abstract Boolean need(IntUnit unit);
|
||||
|
||||
/**
|
||||
* Check if the vehicle needs a specific FloatUnit object
|
||||
*
|
||||
* @param unit The FloatUnit object for which to check the need
|
||||
* @return True if the vehicle needs the FloatUnit object, false otherwise
|
||||
*/
|
||||
public abstract Boolean need(FloatUnit unit);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
47
src/uebung05/logistics/material/BulletBelts.java
Normal file → Executable file
47
src/uebung05/logistics/material/BulletBelts.java
Normal file → Executable file
@ -1,24 +1,39 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
public class BulletBelts implements IntUnit{
|
||||
private static BulletBelts instance;
|
||||
private int amount;
|
||||
/**
|
||||
* The BulletBelts class represents a unit of measurement for 7.62 bullets.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BulletBelts implements IntUnit {
|
||||
|
||||
private BulletBelts(int amount){
|
||||
this.amount = amount;
|
||||
private static BulletBelts INSTANCE;
|
||||
|
||||
private BulletBelts() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the BulletBelts class
|
||||
*
|
||||
* @return The singleton instance of the BulletBelts class
|
||||
*/
|
||||
public static BulletBelts getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new BulletBelts();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the BulletBelts object
|
||||
*
|
||||
* @return A string representation of the BulletBelts object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " belts of 7.62 bullets";
|
||||
return "belts of 7.62 bullets";
|
||||
}
|
||||
|
||||
public static BulletBelts getInstance(int amount){
|
||||
if (instance == null){
|
||||
instance = new BulletBelts(amount);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
48
src/uebung05/logistics/material/Grease.java
Normal file → Executable file
48
src/uebung05/logistics/material/Grease.java
Normal file → Executable file
@ -1,25 +1,39 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
public class Grease implements IntUnit{
|
||||
private static Grease instance;
|
||||
private int amount;
|
||||
/**
|
||||
* The Grease class represents a unit of measurement for grease.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Grease implements IntUnit {
|
||||
|
||||
private Grease(int amount){
|
||||
this.amount = amount;
|
||||
private static Grease INSTANCE;
|
||||
|
||||
private Grease() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the Grease class
|
||||
*
|
||||
* @return The singleton instance of the Grease class
|
||||
*/
|
||||
public static Grease getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new Grease();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the Grease object
|
||||
*
|
||||
* @return A string representation of the Grease object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " units of grease";
|
||||
return "units of grease";
|
||||
}
|
||||
|
||||
public static Grease getInstance(int amount){
|
||||
if (instance == null){
|
||||
instance = new Grease(amount);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
48
src/uebung05/logistics/material/LiterDiesel.java
Normal file → Executable file
48
src/uebung05/logistics/material/LiterDiesel.java
Normal file → Executable file
@ -1,25 +1,39 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.FloatUnit;
|
||||
|
||||
public class LiterDiesel implements FloatUnit{
|
||||
private static LiterDiesel instance;
|
||||
private float amount;
|
||||
/**
|
||||
* The LiterDiesel class represents a unit of measurement for diesel fuel.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class LiterDiesel implements FloatUnit {
|
||||
|
||||
private LiterDiesel(float amount){
|
||||
this.amount = amount;
|
||||
private static LiterDiesel INSTANCE;
|
||||
|
||||
private LiterDiesel() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the LiterDiesel class
|
||||
*
|
||||
* @return The singleton instance of the LiterDiesel class
|
||||
*/
|
||||
public static LiterDiesel getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new LiterDiesel();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the LiterDiesel object
|
||||
*
|
||||
* @return A string representation of the LiterDiesel object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " liters of diesel";
|
||||
return "liters of diesel";
|
||||
}
|
||||
|
||||
public static LiterDiesel getInstance(float amount){
|
||||
if (instance == null){
|
||||
instance = new LiterDiesel(amount);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
48
src/uebung05/logistics/material/MetGallonsKerosene.java
Normal file → Executable file
48
src/uebung05/logistics/material/MetGallonsKerosene.java
Normal file → Executable file
@ -1,25 +1,39 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.FloatUnit;
|
||||
|
||||
public class MetGallonsKerosene implements FloatUnit{
|
||||
private static MetGallonsKerosene instance;
|
||||
private float amount;
|
||||
/**
|
||||
* The MetGallonsKerosene class represents a unit of measurement for kerosene.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class MetGallonsKerosene implements FloatUnit {
|
||||
|
||||
private MetGallonsKerosene(float amount){
|
||||
this.amount = amount;
|
||||
private static MetGallonsKerosene INSTANCE;
|
||||
|
||||
private MetGallonsKerosene() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the MetGallonsKerosene class
|
||||
*
|
||||
* @return The singleton instance of the MetGallonsKerosene class
|
||||
*/
|
||||
public static MetGallonsKerosene getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new MetGallonsKerosene();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the MetGallonsKerosene object
|
||||
*
|
||||
* @return A string representation of the MetGallonsKerosene object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " met gallons of kerosene";
|
||||
return "met gallons of kerosene";
|
||||
}
|
||||
|
||||
public static MetGallonsKerosene getInstance(float amount){
|
||||
if (instance == null){
|
||||
instance = new MetGallonsKerosene(amount);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
47
src/uebung05/logistics/material/Oil.java
Normal file → Executable file
47
src/uebung05/logistics/material/Oil.java
Normal file → Executable file
@ -1,24 +1,39 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
public class Oil implements IntUnit{
|
||||
private static Oil instance;
|
||||
private int amount;
|
||||
/**
|
||||
* The Oil class represents a unit of measurement for oil.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Oil implements IntUnit {
|
||||
|
||||
private Oil(int amount){
|
||||
this.amount = amount;
|
||||
private static Oil INSTANCE;
|
||||
|
||||
private Oil() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the Oil class
|
||||
*
|
||||
* @return The singleton instance of the Oil class
|
||||
*/
|
||||
public static Oil getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new Oil();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the Oil object
|
||||
*
|
||||
* @return A string representation of the Oil object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " units of oil";
|
||||
return "units of oil";
|
||||
}
|
||||
|
||||
public static Oil getInstance(int amount){
|
||||
if (instance == null){
|
||||
instance = new Oil(amount);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
62
src/uebung05/logistics/material/RocketPods.java
Normal file → Executable file
62
src/uebung05/logistics/material/RocketPods.java
Normal file → Executable file
@ -1,24 +1,58 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
public class RocketPods implements IntUnit{
|
||||
private static RocketPods instance;
|
||||
private int amount;
|
||||
/**
|
||||
* The RocketPods class represents a unit of measurement for 70mm rocket pods.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class RocketPods implements IntUnit {
|
||||
|
||||
private RocketPods(int amount){
|
||||
this.amount = amount;
|
||||
private static RocketPods INSTANCE;
|
||||
|
||||
private RocketPods() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the RocketPods class
|
||||
*
|
||||
* @return The singleton instance of the RocketPods class
|
||||
*/
|
||||
public static RocketPods getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new RocketPods();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the RocketPods object
|
||||
*
|
||||
* @return A string representation of the RocketPods object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " pods of 70mm rockets";
|
||||
return "pods of 70mm rockets";
|
||||
}
|
||||
|
||||
public static RocketPods getInstance(int amount){
|
||||
if (instance == null){
|
||||
instance = new RocketPods(amount);
|
||||
}
|
||||
return instance;
|
||||
/**
|
||||
* Set the integer value of the RocketPods object
|
||||
*
|
||||
* @param value The integer value to set
|
||||
*/
|
||||
public void setValue(int value) {
|
||||
// Implementation goes here
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the integer value of the RocketPods object
|
||||
*
|
||||
* @return The integer value of the RocketPods object
|
||||
*/
|
||||
public int getValue() {
|
||||
// Implementation goes here
|
||||
return 0;
|
||||
}
|
||||
}
|
61
src/uebung05/logistics/material/ShellBatches.java
Normal file → Executable file
61
src/uebung05/logistics/material/ShellBatches.java
Normal file → Executable file
@ -1,25 +1,58 @@
|
||||
package uebung05.logistics.material;
|
||||
package logistics.material;
|
||||
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
|
||||
public class ShellBatches implements IntUnit{
|
||||
private static ShellBatches instance;
|
||||
private int amount;
|
||||
/**
|
||||
* The ShellBatches class represents a unit of measurement for 120mm shell batches.
|
||||
* It is a singleton class, meaning only one instance of the class can exist at a time.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class ShellBatches implements IntUnit {
|
||||
|
||||
private ShellBatches(int amount){
|
||||
this.amount = amount;
|
||||
private static ShellBatches INSTANCE;
|
||||
|
||||
private ShellBatches() {}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of the ShellBatches class
|
||||
*
|
||||
* @return The singleton instance of the ShellBatches class
|
||||
*/
|
||||
public static ShellBatches getINSTANCE() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new ShellBatches();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the ShellBatches object
|
||||
*
|
||||
* @return A string representation of the ShellBatches object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount + " batches of 120mm shells";
|
||||
return "batches of 120mm shells";
|
||||
}
|
||||
|
||||
public static ShellBatches getInstance(int amount){
|
||||
if (instance == null){
|
||||
instance = new ShellBatches(amount);
|
||||
}
|
||||
return instance;
|
||||
/**
|
||||
* Set the integer value of the ShellBatches object
|
||||
*
|
||||
* @param value The integer value to set
|
||||
*/
|
||||
public void setValue(int value) {
|
||||
// Implementation goes here
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Get the integer value of the ShellBatches object
|
||||
*
|
||||
* @return The integer value of the ShellBatches object
|
||||
*/
|
||||
public int getValue() {
|
||||
// Implementation goes here
|
||||
return 0;
|
||||
}
|
||||
}
|
6
src/uebung05/logistics/quantities/FloatUnit.java
Normal file → Executable file
6
src/uebung05/logistics/quantities/FloatUnit.java
Normal file → Executable file
@ -1,5 +1,7 @@
|
||||
package uebung05.logistics.quantities;
|
||||
package logistics.quantities;
|
||||
|
||||
/**
|
||||
* @author Nikolaus Köberlein
|
||||
*/
|
||||
public interface FloatUnit {
|
||||
|
||||
}
|
||||
|
8
src/uebung05/logistics/quantities/IntUnit.java
Normal file → Executable file
8
src/uebung05/logistics/quantities/IntUnit.java
Normal file → Executable file
@ -1,5 +1,7 @@
|
||||
package uebung05.logistics.quantities;
|
||||
|
||||
public interface IntUnit {
|
||||
package logistics.quantities;
|
||||
|
||||
/**
|
||||
* @author Nikolaus Köberlein
|
||||
*/
|
||||
public interface IntUnit extends Unit {
|
||||
}
|
||||
|
108
src/uebung05/logistics/quantities/NeedCollector.java
Executable file
108
src/uebung05/logistics/quantities/NeedCollector.java
Executable file
@ -0,0 +1,108 @@
|
||||
package logistics.quantities;
|
||||
|
||||
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 java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The NeedCollector class is responsible for storing and managing the needed supplies for vehicles.
|
||||
* It contains two maps, intNeeded and floatNeeded, to store the needed supplies.
|
||||
* Users can add an integer or float amount of a specific unit to the maps using the add method.
|
||||
* The getNeed method can be used to retrieve the integer or float amount needed for a specific unit.
|
||||
* The show method prints the needed supplies for all vehicles.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class NeedCollector {
|
||||
|
||||
// Maps to store the needed supplies for vehicles
|
||||
private Map<IntUnit, Integer> intNeeded;
|
||||
private Map<FloatUnit, Float> floatNeeded;
|
||||
|
||||
/**
|
||||
* Initialize the two maps for storing the needed supplies
|
||||
*/
|
||||
public NeedCollector() {
|
||||
intNeeded = new HashMap<>();
|
||||
floatNeeded = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an integer amount of a specific IntUnit to the intNeeded map
|
||||
*
|
||||
* @param amount The integer amount to be added
|
||||
* @param unit The IntUnit to which the amount belongs
|
||||
*/
|
||||
public void add(int amount, IntUnit unit) {
|
||||
intNeeded.merge(unit, amount, Integer::sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a float amount of a specific FloatUnit to the floatNeeded map
|
||||
*
|
||||
* @param amount The float amount to be added
|
||||
* @param unit The FloatUnit to which the amount belongs
|
||||
*/
|
||||
public void add(float amount, FloatUnit unit) {
|
||||
floatNeeded.merge(unit, amount, Float::sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the integer amount needed for a specific IntUnit
|
||||
*
|
||||
* @param unit The IntUnit for which the amount is needed
|
||||
* @return The integer amount needed for the specified unit
|
||||
*/
|
||||
public int getNeed(IntUnit unit) {
|
||||
return intNeeded.getOrDefault(unit, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the float amount needed for a specific FloatUnit
|
||||
*
|
||||
* @param unit The FloatUnit for which the amount is needed
|
||||
* @return The float amount needed for the specified unit
|
||||
*/
|
||||
public float getNeed(FloatUnit unit) {
|
||||
return floatNeeded.getOrDefault(unit, 0.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the needed supplies for all vehicles
|
||||
*/
|
||||
public void show() {
|
||||
System.out.println("Needed supplies for all vehicles:");
|
||||
|
||||
// Print the needed supplies for specific units
|
||||
System.out.println("- " + getNeed(Grease.getINSTANCE()) + " " + Grease.getINSTANCE());
|
||||
System.out.println("- " + getNeed(ShellBatches.getINSTANCE()) + " " + ShellBatches.getINSTANCE());
|
||||
System.out.println("- " + getNeed(RocketPods.getINSTANCE()) + " " + RocketPods.getINSTANCE());
|
||||
System.out.println("- " + getNeed(BulletBelts.getINSTANCE()) + " " + BulletBelts.getINSTANCE());
|
||||
System.out.println("- " + getNeed(Oil.getINSTANCE()) + " " + Oil.getINSTANCE());
|
||||
System.out.println("- " + getNeed(MetGallonsKerosene.getINSTANCE()) + " " + MetGallonsKerosene.getINSTANCE());
|
||||
System.out.println("- " + getNeed(LiterDiesel.getINSTANCE()) + " " + LiterDiesel.getINSTANCE());
|
||||
|
||||
//todo
|
||||
/*
|
||||
for (Map.Entry<IntUnit, Integer> entry : collector.getIntNeeded().entrySet()) {
|
||||
IntUnit unit = entry.getKey();
|
||||
int amount = entry.getValue();
|
||||
|
||||
}
|
||||
|
||||
for (FloatUnit unit : FloatUnit.getAllUnits()) {
|
||||
float totalNeed = collector.getNeed(unit);
|
||||
System.out.println("- " + totalNeed + " " + unit.toString());
|
||||
}*/
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
7
src/uebung05/logistics/quantities/Unit.java
Executable file
7
src/uebung05/logistics/quantities/Unit.java
Executable file
@ -0,0 +1,7 @@
|
||||
package logistics.quantities;
|
||||
|
||||
/**
|
||||
* @author Nikolaus Köberlein
|
||||
*/
|
||||
public interface Unit {
|
||||
}
|
164
src/uebung05/logistics/storage/FloatStorage.java
Normal file → Executable file
164
src/uebung05/logistics/storage/FloatStorage.java
Normal file → Executable file
@ -1,60 +1,134 @@
|
||||
package uebung05.logistics.storage;
|
||||
package logistics.storage;
|
||||
|
||||
import uebung05.logistics.NeedCollector;
|
||||
import uebung05.logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.FloatUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
|
||||
public class FloatStorage implements FloatUnit{
|
||||
/**
|
||||
* The FloatStorage class represents a floating-point storage for a specific FloatUnit object.
|
||||
* It includes methods for getting the unit, stored amount, and maximum capacity, as well as
|
||||
* methods for consuming, filling, and reporting needs.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class FloatStorage {
|
||||
|
||||
private FloatUnit unit;
|
||||
private float stored;
|
||||
private final FloatUnit unit;
|
||||
private final float max;
|
||||
private 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;
|
||||
/**
|
||||
* Construct a FloatStorage object with a stored amount, FloatUnit object, and maximum capacity
|
||||
*
|
||||
* @param stored The initial stored amount for the FloatStorage object
|
||||
* @param unit The FloatUnit object for the FloatStorage object
|
||||
* @param max The maximum capacity for the FloatStorage object
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if unit is negative
|
||||
*/
|
||||
public FloatStorage(float stored, FloatUnit unit, float max) {
|
||||
if (stored < 0 || max < 0) {
|
||||
throw new IllegalArgumentException("Units cannot be negative");
|
||||
}
|
||||
if (stored > max) {
|
||||
this.stored = max;
|
||||
this.unit = unit;
|
||||
this.max = max;
|
||||
} else {
|
||||
stored = stored + amount;
|
||||
this.stored = stored;
|
||||
this.unit = unit;
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
|
||||
public void fillUp(){
|
||||
/**
|
||||
* Get the FloatUnit object for the FloatStorage object
|
||||
*
|
||||
* @return The FloatUnit object for the FloatStorage object
|
||||
*/
|
||||
public FloatUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stored amount for the FloatStorage object
|
||||
*
|
||||
* @return The stored amount for the FloatStorage object
|
||||
*/
|
||||
public float getStored() {
|
||||
return stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum capacity for the FloatStorage object
|
||||
*
|
||||
* @return The maximum capacity for the FloatStorage object
|
||||
*/
|
||||
public float getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume a specific amount from the FloatStorage object
|
||||
*
|
||||
* @param amount The amount to consume from the FloatStorage object
|
||||
* @return The amount consumed from the FloatStorage object
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if consume is negative
|
||||
*/
|
||||
public float consume(float amount) {
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Consume cannot be negative");
|
||||
}
|
||||
if (stored < amount) {
|
||||
float newStored = stored;
|
||||
stored = 0;
|
||||
return newStored;
|
||||
}
|
||||
stored -= amount;
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the FloatStorage object with a specific amount
|
||||
*
|
||||
* @param amount The amount to fill the FloatStorage object with
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if consume is negative
|
||||
*/
|
||||
public void fill(float amount) {
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Consume cannot be negative");
|
||||
}
|
||||
stored += amount;
|
||||
if (max < stored) {
|
||||
fillUp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the FloatStorage object to its maximum capacity
|
||||
*/
|
||||
public void fillUp() {
|
||||
stored = max;
|
||||
}
|
||||
|
||||
public void reportNeed(NeedCollector collector){
|
||||
/**
|
||||
* Report the needs of the FloatStorage object to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
public void reportNeed (NeedCollector collector) {
|
||||
collector.add(max-stored, unit);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Get a string representation of the FloatStorage object
|
||||
*
|
||||
* @return A string representation of the FloatStorage object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "storage with " + stored + " of " + max + " " + unit;
|
||||
}
|
||||
}
|
166
src/uebung05/logistics/storage/IntStorage.java
Normal file → Executable file
166
src/uebung05/logistics/storage/IntStorage.java
Normal file → Executable file
@ -1,58 +1,134 @@
|
||||
package uebung05.logistics.storage;
|
||||
package logistics.storage;
|
||||
|
||||
import uebung05.logistics.NeedCollector;
|
||||
import uebung05.logistics.quantities.IntUnit;
|
||||
import logistics.quantities.IntUnit;
|
||||
import logistics.quantities.NeedCollector;
|
||||
|
||||
public class IntStorage implements IntUnit {
|
||||
/**
|
||||
* The IntStorage class represents an integer storage for a specific IntUnit object.
|
||||
* It includes methods for getting the unit, stored amount, and maximum capacity, as
|
||||
* well as methods for consuming, filling, and reporting needs.
|
||||
*
|
||||
* @author Nikolaus Köberlein
|
||||
* @version 1.0
|
||||
*/
|
||||
public class IntStorage {
|
||||
|
||||
private IntUnit unit;
|
||||
private int stored;
|
||||
private final IntUnit unit;
|
||||
private final int max;
|
||||
private 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;
|
||||
/**
|
||||
* Construct an IntStorage object with a stored amount, IntUnit object, and maximum capacity
|
||||
*
|
||||
* @param stored The initial stored amount for the IntStorage object
|
||||
* @param unit The IntUnit object for the IntStorage object
|
||||
* @param max The maximum capacity for the IntStorage object
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if unit is negative
|
||||
*/
|
||||
public IntStorage(int stored, IntUnit unit, int max) {
|
||||
if (stored < 0 || max < 0) {
|
||||
throw new IllegalArgumentException("Units cannot be negative");
|
||||
}
|
||||
if (stored > max) {
|
||||
this.stored = max;
|
||||
this.unit = unit;
|
||||
this.max = max;
|
||||
} else {
|
||||
stored = stored + amount;
|
||||
this.stored = stored;
|
||||
this.unit = unit;
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
|
||||
public void fillUp(){
|
||||
/**
|
||||
* Get the IntUnit object for the IntStorage object
|
||||
*
|
||||
* @return The IntUnit object for the IntStorage object
|
||||
*/
|
||||
public IntUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stored amount for the IntStorage object
|
||||
*
|
||||
* @return The stored amount for the IntStorage object
|
||||
*/
|
||||
public int getStored() {
|
||||
return stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum capacity for the IntStorage object
|
||||
*
|
||||
* @return The maximum capacity for the IntStorage object
|
||||
*/
|
||||
public int getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume a specific amount from the IntStorage object
|
||||
*
|
||||
* @param amount The amount to consume from the IntStorage object
|
||||
* @return The amount consumed from the IntStorage object
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if consume is negative
|
||||
*/
|
||||
public int consume(int amount) {
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Consume cannot be negative");
|
||||
}
|
||||
if (stored < amount) {
|
||||
int newStored = stored;
|
||||
stored = 0;
|
||||
return newStored;
|
||||
}
|
||||
stored -= amount;
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the IntStorage object with a specific amount
|
||||
*
|
||||
* @param amount The amount to fill the IntStorage object with
|
||||
*
|
||||
* @throws java.lang.IllegalArgumentException if consume is negative
|
||||
*/
|
||||
public void fill(int amount) {
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Consume cannot be negative");
|
||||
}
|
||||
stored += amount;
|
||||
if (max < stored) {
|
||||
fillUp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the IntStorage object to its maximum capacity
|
||||
*/
|
||||
public void fillUp() {
|
||||
stored = max;
|
||||
}
|
||||
|
||||
public void reportNeed(NeedCollector collector){
|
||||
/**
|
||||
* Report the needs of the IntStorage object to a NeedCollector object
|
||||
*
|
||||
* @param collector The NeedCollector object to which to report the needs
|
||||
*/
|
||||
public void reportNeed (NeedCollector collector) {
|
||||
collector.add(max-stored, unit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string representation of the IntStorage object
|
||||
*
|
||||
* @return A string representation of the IntStorage object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "storage with " + stored + " of " + max + " " + unit;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user