Dodałem sobie rzeczy do sejfu za pomocą listy i w funkcji add() jestem w stanie je odczytać, ale nie potrafię ich odczytać za pomocą funkcji printContent() no i połączyć tego z toString w klasie Thing.
To pewnie bardzo proste, ale ja nie mam jeszcze takiej biegłości w tym. Pewnie analogiczne byłoby stworzenie funkcji kasującej jakiś element z listy, ale wtedy i jej efekt trzeba by było jakoś połączyć z funkcją zwracającą ostateczną zawartość listy.
Czy mógłbym prosić o jakieś rozjaśnienie mojego przyćmionego umysłu?
import java.util.ArrayList;
class Safe {
double weight;
double volume;
double maxWeight;
double maxVolume;
public Safe(double weight, double volume, double maxWeight, double maxVolume){
this.weight = weight;
this.volume = volume;
this.maxWeight = maxWeight;
this.maxVolume = maxVolume;
}
public void add(Thing thing) {
ArrayList<String> item_name = new ArrayList<String>();
item_name.add(thing.getName());
ArrayList<Double> item_weight = new ArrayList<Double>();
item_weight.add(thing.getWeight());
ArrayList<Double> item_volume = new ArrayList<Double>();
item_volume.add(thing.getVolume());
for (int i = 0; i < item_name.size(); i++) {
System.out.println(item_name.get(i));
System.out.println(item_weight.get(i));
System.out.println(item_volume.get(i));
}
}
public Thing remove(int index) {
}
public void printContent(){
}
}
class Thing {
protected double weight;
protected double volume;
protected String name;
public Thing(String name, double weight, double volume){
if(name != null)
this.name = name;
else
this.name = "";
this.weight = Math.max(weight, 0.0);
this.volume = Math.max(volume, 0.0);
}
public double getWeight(){
return weight;
}
public double getVolume(){
return volume;
}
public String getName(){
return name;
}
@Override
public String toString() {
return name + " (" + weight + " kg, " + volume + " l)";
}
}
class Box extends Thing {
protected double height;
protected double width;
protected double depth;
public Box(String name, double weight,
double height, double width, double depth){
super(name, weight, height*width*depth*1000);
this.height = height;
this.width = width;
this.depth = depth;
}
public double getHeight(){
return height;
}
public double getWidht(){
return width;
}
public double getDepth(){
return depth;
}
}
public class safe1{
public static void main(String args[]){
Safe subject = new Safe(0.100, 0.5, 40, 50);
subject.add(new Thing("Ball", 1, 3));
subject.add(new Thing("Torch", 0.5, 1));
subject.add(new Box("Box1", 5, 0.1,0.5,0.5));
subject.printContent();
// subject.remove(2); //to bylby drugi element z listy
}
}