Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions proJect/src/main/java/anlov/java/Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package anlov.java;

import java.util.ArrayList;
import java.util.List;

public class Box<T extends Fruit> {
private final List<T> boxWithFruit = new ArrayList<>();

public void add(T fruits, int count) {
for (int i = 0; i < count; i++) {
boxWithFruit.add(fruits);
}
}

public double getBoxWeight() {
return boxWithFruit.stream().mapToDouble(Fruit::getWeight).sum();
}

public boolean compare(Box<?> fruitsWeight) {
return this.getBoxWeight() == fruitsWeight.getBoxWeight();
}

public void exchange(Box<T> newBox) {
newBox.boxWithFruit.addAll(this.boxWithFruit);
this.boxWithFruit.clear();
}

}
27 changes: 27 additions & 0 deletions proJect/src/main/java/anlov/java/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package anlov.java;

public class Fruit {
private double weight;

public Fruit(double weight) {
this.weight = weight;
}

public double getWeight() {
return weight;
}
}

class Apple extends Fruit{

public Apple(double weight) {
super(weight);
}
}

class Orange extends Fruit{

public Orange(double weight) {
super(weight);
}
}
22 changes: 22 additions & 0 deletions proJect/src/main/java/anlov/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,27 @@

public class Main {
public static void main(String[] args) {

double weightApple = 1;
double weightOrange = 1.5;

Box<Orange> orangeBox = new Box<>();
orangeBox.add(new Orange(weightOrange), 7);
System.out.println(orangeBox.getBoxWeight());

Box<Apple> appleBox = new Box<>();
appleBox.add(new Apple(weightApple), 7);
System.out.println(appleBox.getBoxWeight());

Box<Apple> appleBox2 = new Box<>();
appleBox2.add(new Apple(weightApple), 8);
System.out.println(appleBox2.getBoxWeight());

System.out.println(appleBox.compare(appleBox2));

appleBox2.exchange(appleBox);
System.out.println(appleBox.getBoxWeight());
System.out.println(appleBox2.getBoxWeight());

}
}