Hallo Java Forum,
Ich soll für eine Aufgabe eine Methode implementieren um Waren einer Palette hinzuzufügen. Die Waren sollen von "konkreter Art" also typgebunden sein.
Nun habe ich aber das problem, dass ich die Methode add der ArrayList nur nutzen kann wenn ich Objekte des Typs <T> hinzufüge, aber wenn ich das dahingend ändere kann ich nicht mehr die Methode getWeight() aus der Klasse Good verwenden. Ich hoffe mir kann jemand zeigen wo mein Fehler liegt.
Ich soll für eine Aufgabe eine Methode implementieren um Waren einer Palette hinzuzufügen. Die Waren sollen von "konkreter Art" also typgebunden sein.
Nun habe ich aber das problem, dass ich die Methode add der ArrayList nur nutzen kann wenn ich Objekte des Typs <T> hinzufüge, aber wenn ich das dahingend ändere kann ich nicht mehr die Methode getWeight() aus der Klasse Good verwenden. Ich hoffe mir kann jemand zeigen wo mein Fehler liegt.
Java:
package edu.hm.cs.swe2.generics;
import java.util.ArrayList;
import edu.hm.cs.swe2.exceptions.NothingToAddException;
import edu.hm.cs.swe2.exceptions.StillageAlreadyClosedForShippingException;
import edu.hm.cs.swe2.exceptions.StillageWouldBeOverweightException;
public class Stillage<T> {
private int maxWeight;
private ArrayList<T> collection;
private boolean readyToShip = false;
public Stillage(int maxWeight) {
this.maxWeight = maxWeight;
}
public String toString() {
return "Lieferschein /n" + this.collection;
}
public void printListOfContents() {
System.out.print(this.toString());
}
private int getLoadedWeight() {
collection.forEach(null);
}
public boolean fitsOntoStillage(T good) {
if (maxWeight >= (good.getWeight() + this.getLoadedWeight())) { //hier werde ich aufgefordert einen cast zu adden
return true;
} else {
return false;
}
}
public void addGood(T good) throws StillageAlreadyClosedForShippingException, NothingToAddException,
StillageWouldBeOverweightException {
if (this.readyToShip) {
throw new StillageAlreadyClosedForShippingException();
}
if (good == null) {
throw new NothingToAddException();
}
if (this.fitsOntoStillage(good)) {
int overweight = good.getWeight() - (this.maxWeight - this.getLoadedWeight()); //hier werde ich aufgefordert einen cast zu adden
throw new StillageWouldBeOverweightException();
// hier noch Übergewicht ausgeben
}
collection.add(good);
if (this.maxWeight == this.getLoadedWeight()) {
this.readyToShip = true;
}
}
public ArrayList<T> loadStillage(T... goods) {
ArrayList<T> notLoadedGoods = new ArrayList<T>();
int counter = 0;
try {
for (int i = 0; i < goods.length; i++) {
counter += 1;
this.addGood(goods[i]);
}
} catch (NothingToAddException e) {
e.printStackTrace();
System.out.println("");
} catch (StillageAlreadyClosedForShippingException e) {
e.printStackTrace();
System.out.println("");
notLoadedGoods.add(goods[counter - 1]);
} catch (StillageWouldBeOverweightException e) {
e.printStackTrace();
System.out.println("");
notLoadedGoods.add(goods[counter - 1]);
}
if (notLoadedGoods.isEmpty()) {
return null;
}
return notLoadedGoods;
}
}
Java:
package edu.hm.cs.swe2.generics;
public class Good<T> {
int weight;
String description;
public Good(int weight, String description) {
this.weight = weight;
this.description = description;
}
public int getWeight() {
return weight;
}
public String toString() {
return "- " + this.description + ", " + "Gesamtgewicht " + this.weight + " kg";
}
}