import java.util.ArrayList;
class TableSafe {
ArrayList<Thing> item = new ArrayList<>();
}
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 Backpack {
double weight;
double volume;
double maxWeight;
double maxVolume;
String name;
TableSafe element = new TableSafe();
public Backpack(double weight, double volume, double maxWeight, double maxVolume) {
this.weight = weight;
this.volume = volume;
this.maxWeight = maxWeight;
this.maxVolume = maxVolume;
}
void add(Thing thing) {
element.item.add(thing);
}
void add(Backpack small) {
add(new Thing(Backpack.class.getSimpleName(), small.weight, small.volume));
}
public void remove(int index) {
element.item.remove(index - 1);
}
public void printContent() {
System.out.println(new Thing(Backpack.class.getSimpleName(), this.weight, this.volume) + " with");
double totalWeight = 0.0;
double totalVolume = 0.0;
for (int i = 0; i < element.item.size(); i++) {
System.out.print((i + 1) + ": ");
Thing thing = element.item.get(i);
System.out.println(thing);
totalWeight += thing.weight;
totalVolume += thing.volume;
}
System.out.println("Total: " + (totalWeight + this.weight) + ", " + (totalVolume + this.volume));
System.out.println();
}
public double getWeight() {
double total = 0.0;
for (int i = 0; i < element.item.size(); i++) {
total += element.item.get(i).getWeight();
}
return total + this.weight;
}
public double getVolume() {
double total = 0.0;
for (int i = 0; i < element.item.size(); i++) {
total += element.item.get(i).getVolume();
}
return total + this.volume;
}
public String getName() {
for (int i = 0; i < element.item.size(); i++) {
String name = Backpack.class.getSimpleName();
}
return name;
}
}
class Box extends Thing {
protected double height;
protected double width;
protected double depth;
protected double volume;
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;
volume = height * width * depth * 1000;
}
public double getHeight() {
return height;
}
public double getWidht() {
return width;
}
public double getDepth() {
return depth;
}
@Override
public String getName() {
return super.getName();
}
}
public class Safe {
public static void main(String[] args) {
Backpack b = new Backpack(0.100, 0.5, 40, 50);
b.add(new Thing("Ball", 1, 3));
b.add(new Thing("Torch", 0.5, 1));
b.add(new Box("Box1", 5, 0.1, 0.5, 0.5));
b.printContent();
b.remove(2);
b.printContent();
System.out.println(b.getWeight());
System.out.println(b.getVolume());
System.out.println();
Backpack small = new Backpack(0.01, 0.1, 5, 13);
// b = small;
small.add(new Box("Box2", 3, 0.2, 0.2, 0.2));
small.add(new Box("Box3", 1, 0.1, 0.2, 0.2));
b.add(small);
b.printContent();
}
}