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
13 changes: 13 additions & 0 deletions Apple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.gb.lesson3;

public class Apple extends Fruit {
public Apple() {
super(1.0f);

}

@Override
public String toString() {
return "Apple{" + "weight= " + weight + "}";
}
}
56 changes: 56 additions & 0 deletions Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.gb.lesson3;

import java.util.ArrayList;
import java.util.Objects;

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

public Box() {
}
public Box(ArrayList<T> fruits) {
this.fruits = fruits;
}
public void addFruits(ArrayList<T> newFruits) {
if (newFruits != null) {
fruits.addAll(newFruits);
}
}
public Float getWeight() {
float result = 0.0f;
if (this.fruits.isEmpty()) {
return result;
}
result = fruits.size() * fruits.get(0).weight;
return result;
}
public boolean compare(Box<? extends Fruit> otherBox) { return otherBox.getWeight().equals(this.getWeight());
}
public void toAnotherBox(Box<T> box) {
if (this.fruits != null) {
box.addFruits(this.fruits);
this.fruits.clear();
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Box<?> box = (Box<?>) o;
return Objects.equals(fruits, box.fruits);
}

@Override
public int hashCode() {
return Objects.hash(fruits);
}

@Override
public String toString() {
return "Box{" +
"fruits=" + fruits +
'}';
}
}

10 changes: 10 additions & 0 deletions Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.gb.lesson3;

public class Fruit {
public Float weight;

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

}
67 changes: 67 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.gb.lesson3;

import java.util.ArrayList;
import java.util.Arrays;

public class Main {

public static void main(String[] args) {
Box<Apple> appleBox = new Box<>(new ArrayList<>(Arrays.asList(new Apple(), new Apple(), new Apple())));
Box<Apple> newAppleBox = new Box<>();
Box<Orange> orangeBox = new Box<>(new ArrayList<>(Arrays.asList(new Orange(), new Orange())));
Box<Orange> newOrangeBox = new Box<>(new ArrayList<>(Arrays.asList(new Orange())));

System.out.println("Apple box: " + appleBox);
System.out.println("New Apple box: " + newAppleBox);
System.out.println("Orange box: " + orangeBox);
System.out.println("New Orange box: " + newOrangeBox);
appleBox.addFruits(new ArrayList<>(Arrays.asList(new Apple(), new Apple())));
appleBox.toAnotherBox(newAppleBox);
System.out.println();
System.out.println("New Apple box after add apples: " + newAppleBox);
System.out.println("Apple box after remove apples: " + appleBox);
System.out.println();
System.out.println("New Apple box weight: " + newAppleBox.getWeight());
System.out.println("Apple box weight: " + appleBox.getWeight());
System.out.println("New Orange box weight: " + newOrangeBox.getWeight());
System.out.println("Orange box weight: " + orangeBox.getWeight());
System.out.println();
System.out.println(newAppleBox.compare(orangeBox));
newAppleBox.addFruits(new ArrayList<>(Arrays.asList(new Apple(), new Apple())));
//orangeBox.addFruits(new ArrayList<>(Arrays.asList(new Orange(), new Orange())));
newOrangeBox.addFruits(new ArrayList<>(Arrays.asList(new Orange(), new Orange())));
System.out.println("New Apple box weight: " + newAppleBox.getWeight());
System.out.println("Orange box weight: " + orangeBox.getWeight());
System.out.println(newAppleBox.compare(orangeBox));
//newOrangeBox.toAnotherBox(orangeBox);
orangeBox.toAnotherBox(newOrangeBox);
//System.out.println("Orange box weight: " + orangeBox.getWeight());
System.out.println("Apple box: " + appleBox);
System.out.println("New Apple box: " + newAppleBox);
System.out.println("Orange box: " + orangeBox);
System.out.println("New Orange box: " + newOrangeBox);
System.out.println();
System.out.println("New Apple box weight: " + newAppleBox.getWeight());
System.out.println("New Orange box weight: " + newOrangeBox.getWeight());
System.out.println("Apple box weight: " + appleBox.getWeight());
System.out.println("Orange box weight: " + orangeBox.getWeight());
System.out.println();

Integer[] ints = {1, 2, 3, 4, 5, 6};
Swap<Integer> integerSwap = new Swap<>();
integerSwap.swap(ints, 2, 5);

for (Integer i : ints) {
System.out.print(i + " ");
}
System.out.println();

Float[] floats = {1.6f, 2.4f, 3.7f, 4.1f, 5.8f};
Swap<Float> floatSwap = new Swap<>();
floatSwap.swap(floats, 0, 4);

for (Float i: floats) {
System.out.print(i + " ");
}
}
}
13 changes: 13 additions & 0 deletions Orange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.gb.lesson3;

public class Orange extends Fruit {
public Orange() {
super(1.5f);

}

@Override
public String toString() {
return "Orange{ " + "weight= " + weight + "}";
}
}
9 changes: 9 additions & 0 deletions Swap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.gb.lesson3;

public class Swap<T> {
public void swap(T[] array, int first, int second) {
T buffer = array[first];
array[first] = array[second];
array[second] = buffer;
}
}