Skip to content
Snippets Groups Projects
Commit a5c250be authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Solution

parent 6a64180a
Branches monday_14-15
No related merge requests found
Pipeline #60 failed with stages
in 0 seconds
......@@ -52,5 +52,13 @@
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
......@@ -11,4 +11,17 @@ public class ColorPoint extends Point {
this.color = color;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ColorPoint) {
ColorPoint colorPoint = (ColorPoint) obj;
return super.equals(obj) && this.color.equals(colorPoint.color);
}
return false;
}
@Override
protected boolean canEqual(Point point) {
return point instanceof ColorPoint;
}
}
package koz01.java2.lab04;
import javafx.scene.paint.Color;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
public class ColorPoint2 extends Point2 {
private Color color;
public ColorPoint2(double x, double y, Color color) {
super(x, y);
this.color = color;
}
}
......@@ -3,28 +3,13 @@ package koz01.java2.lab04;
public class NutritionFacts {
private final int servingSize;// (mL)required
private final int servings;// (per container) required
private final int calories;// (per serving)optional
private final int fat;// (g/serving)optional
private final int sodium;// (mg/serving)optional
private final int carbohydrate; // (g/serving) optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings, int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
private NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
......@@ -32,4 +17,49 @@ public class NutritionFacts {
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
public static NutritionFacts.Builder builder(int servingSize, int servings) {
return new Builder(servingSize, servings);
}
public static class Builder {
//mandatory
private final int servingSize;// (mL)required
private final int servings;// (per container) required
//optional
private int calories;// (per serving)optional
private int fat;// (g/serving)optional
private int sodium;// (mg/serving)optional
private int carbohydrate; // (g/serving) optional
private Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int calories) {
this.calories = calories;
return this;
}
public Builder fat(int fat) {
this.fat = fat;
return this;
}
public Builder sodium(int sodium) {
this.sodium = sodium;
return this;
}
public Builder carbohydrate(int carbohydrate) {
this.carbohydrate = carbohydrate;
return this;
}
public NutritionFacts build() {
return new NutritionFacts(servingSize, servings, calories, fat, sodium, carbohydrate);
}
}
}
package koz01.java2.lab04;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NonNull;
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class NutritionFacts2 {
private final int servingSize;// (mL)required
private final int servings;// (per container) required
@Builder.Default
private final int calories = 0;// (per serving)optional
@Builder.Default
private final int fat = 0;// (g/serving)optional
@Builder.Default
private final int sodium = 0;// (mg/serving)optional
@Builder.Default
private final int carbohydrate = 10; // (g/serving) optional
static public NutritionFacts2Builder builder(int servingSize, int servings) {
return new NutritionFacts2Builder().servingSize(servingSize).servings(servings);
}
}
package koz01.java2.lab04;
public class NutritionFactsMain {
public static void main(String[] args) {
NutritionFacts nf = NutritionFacts.builder(1, 15)
.calories(12)
.carbohydrate(12)
.fat(150)
.build();
NutritionFacts2 nf2 = NutritionFacts2.builder(1,15)
.calories(12)
.carbohydrate(12)
.fat(150)
.build();
}
}
......@@ -3,7 +3,7 @@ package koz01.java2.lab04;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
......@@ -11,5 +11,17 @@ public class Point {
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point point = (Point) obj;
return point.canEqual(this) && point.x == x && point.y == y;
}
return false;
}
protected boolean canEqual(Point point) {
return true;
}
}
package koz01.java2.lab04;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class Point2 {
private double x;
private double y;
public Point2(double x, double y) {
this.x = x;
this.y = y;
}
}
module koz01.java2.lab04 {
requires transitive javafx.controls;
requires javafx.fxml;
requires lombok;
opens koz01.java2.lab04 to javafx.fxml;
exports koz01.java2.lab04;
}
\ No newline at end of file
package koz01.java2.lab04;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import javafx.scene.paint.Color;
class TestEquals2 {
@Test
void testRelfexity() {
Point p1 = new Point(1,2);
assertTrue(p1.equals(p1));
}
@Test
void testSymmetry() {
Point2 p1 = new Point2(1,2);
Point2 p2 = new Point2(1,2);
Point2 p3 = new ColorPoint2(1,2, Color.AZURE);
assertFalse(p2.equals(p3));
assertFalse(p3.equals(p2));
}
@Test
void testTransitivity() {
Point p1 = new ColorPoint(1,2,Color.RED);
Point p2 = new Point(1,2);
Point p3 = new ColorPoint(1,2, Color.BLUE);
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p3));
assertFalse(p1.equals(p3));
}
@Test
void testLiskovPrinciple() {
Point p1 = new ColorPoint(1, 2, Color.CYAN);
Point p2 = new ColorPoint(1, 2, Color.CYAN) {
@Override
public String toString() {
return "Moje barva";
}
};
assertTrue(p1.equals(p2));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment