diff --git a/out/production/JavaLab1_Clothes/com/Jeans/BackPocket.class b/out/production/JavaLab1_Clothes/com/Jeans/BackPocket.class index 31b1b9a..857f6d8 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/BackPocket.class and b/out/production/JavaLab1_Clothes/com/Jeans/BackPocket.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/BottomHem.class b/out/production/JavaLab1_Clothes/com/Jeans/BottomHem.class index cf8ff25..209702d 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/BottomHem.class and b/out/production/JavaLab1_Clothes/com/Jeans/BottomHem.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/Button.class b/out/production/JavaLab1_Clothes/com/Jeans/Button.class index d5f9ce8..5986def 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/Button.class and b/out/production/JavaLab1_Clothes/com/Jeans/Button.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/FrontPocket.class b/out/production/JavaLab1_Clothes/com/Jeans/FrontPocket.class index 861bded..6ab1eb5 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/FrontPocket.class and b/out/production/JavaLab1_Clothes/com/Jeans/FrontPocket.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/InsidePart.class b/out/production/JavaLab1_Clothes/com/Jeans/InsidePart.class index 5bdb965..94e2e4f 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/InsidePart.class and b/out/production/JavaLab1_Clothes/com/Jeans/InsidePart.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/Jeans.class b/out/production/JavaLab1_Clothes/com/Jeans/Jeans.class index e93100b..ce879a4 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/Jeans.class and b/out/production/JavaLab1_Clothes/com/Jeans/Jeans.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/JeansParts.class b/out/production/JavaLab1_Clothes/com/Jeans/JeansParts.class index a9c7393..a4e7c28 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/JeansParts.class and b/out/production/JavaLab1_Clothes/com/Jeans/JeansParts.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/Main.class b/out/production/JavaLab1_Clothes/com/Jeans/Main.class index 18411d2..d11ec34 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/Main.class and b/out/production/JavaLab1_Clothes/com/Jeans/Main.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/OutsidePart.class b/out/production/JavaLab1_Clothes/com/Jeans/OutsidePart.class index a864104..0ef49e5 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/OutsidePart.class and b/out/production/JavaLab1_Clothes/com/Jeans/OutsidePart.class differ diff --git a/out/production/JavaLab1_Clothes/com/Jeans/Zipper.class b/out/production/JavaLab1_Clothes/com/Jeans/Zipper.class index a226478..58b2bd4 100644 Binary files a/out/production/JavaLab1_Clothes/com/Jeans/Zipper.class and b/out/production/JavaLab1_Clothes/com/Jeans/Zipper.class differ diff --git a/src/com/Jeans/BackPocket.java b/src/com/Jeans/BackPocket.java index fe8b736..6b5ee25 100644 --- a/src/com/Jeans/BackPocket.java +++ b/src/com/Jeans/BackPocket.java @@ -3,8 +3,8 @@ public class BackPocket extends JeansParts{ private Button backPocketButton; - public BackPocket(String material, double materialSize, String color, Button backPocketButton) { - super(material, materialSize, color); + public BackPocket(String material, double materialSize, String color, Button backPocketButton, int price) { + super(material, materialSize, color, price); this.backPocketButton = backPocketButton; } diff --git a/src/com/Jeans/BottomHem.java b/src/com/Jeans/BottomHem.java index fb14b24..04465f8 100644 --- a/src/com/Jeans/BottomHem.java +++ b/src/com/Jeans/BottomHem.java @@ -2,8 +2,8 @@ public class BottomHem extends JeansParts{ - public BottomHem(String material, double materialSize, String color) { - super(material, materialSize, color); + public BottomHem(String material, double materialSize, String color, int price) { + super(material, materialSize, color, price); } @Override diff --git a/src/com/Jeans/Button.java b/src/com/Jeans/Button.java index 312c920..bfcd092 100644 --- a/src/com/Jeans/Button.java +++ b/src/com/Jeans/Button.java @@ -3,8 +3,8 @@ public class Button extends JeansParts{ private String buttonType; - public Button(String material, double materialSize, String color, String buttonType) { - super(material, materialSize, color); + public Button(String material, double materialSize, String color, String buttonType, int price) { + super(material, materialSize, color, price); this.buttonType = buttonType; } diff --git a/src/com/Jeans/FrontPocket.java b/src/com/Jeans/FrontPocket.java index 1e1ebc5..1531750 100644 --- a/src/com/Jeans/FrontPocket.java +++ b/src/com/Jeans/FrontPocket.java @@ -2,8 +2,8 @@ public class FrontPocket extends JeansParts { - public FrontPocket(String material, double materialSize, String color) { - super(material, materialSize, color); + public FrontPocket(String material, double materialSize, String color, int price) { + super(material, materialSize, color, price); } @Override diff --git a/src/com/Jeans/InsidePart.java b/src/com/Jeans/InsidePart.java index 183f6e4..3098b2f 100644 --- a/src/com/Jeans/InsidePart.java +++ b/src/com/Jeans/InsidePart.java @@ -2,8 +2,8 @@ public class InsidePart extends JeansParts{ - public InsidePart(String material, double materialSize, String color) { - super(material, materialSize, color); + public InsidePart(String material, double materialSize, String color, int price) { + super(material, materialSize, color, price); } @Override diff --git a/src/com/Jeans/Jeans.java b/src/com/Jeans/Jeans.java index 4832b43..776c30f 100644 --- a/src/com/Jeans/Jeans.java +++ b/src/com/Jeans/Jeans.java @@ -10,9 +10,10 @@ public class Jeans { private double width; private double length; private final JeansType type; + private int price; public Jeans(Collection parts, String name, double width, double length, JeansType type) throws JeansException { - if(name.equals("")) { + if (name.equals("")) { throw new JeansException(); } this.parts = parts; @@ -23,6 +24,18 @@ public Jeans(Collection parts, String name, double width, double len } + public Collection getParts() { + return parts; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + public String getName() { return name; } @@ -49,6 +62,7 @@ public JeansType getType() { public Jeans addPart(JeansParts part) { this.parts.add(part); + this.price += part.price; return this; } diff --git a/src/com/Jeans/JeansParts.java b/src/com/Jeans/JeansParts.java index 47e9284..22c734d 100644 --- a/src/com/Jeans/JeansParts.java +++ b/src/com/Jeans/JeansParts.java @@ -4,11 +4,21 @@ public abstract class JeansParts implements JeansComponent { protected String material; protected double materialSize; protected String color; + protected int price; - public JeansParts(String material, double materialSize, String color) { + public JeansParts(String material, double materialSize, String color, int price) { this.material = material; this.materialSize = materialSize; this.color = color; + this.price = price; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; } public String getMaterial() { @@ -35,5 +45,11 @@ public void setColor(String color) { this.color = color; } - public void print(){} + @Override + public String toString() { + return this.material; + } + + public void print() { + } } diff --git a/src/com/Jeans/Main.java b/src/com/Jeans/Main.java index cd11ee3..b8b0b7b 100644 --- a/src/com/Jeans/Main.java +++ b/src/com/Jeans/Main.java @@ -1,6 +1,10 @@ package com.Jeans; import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.OptionalInt; +import java.util.stream.Collectors; public class Main { @@ -14,41 +18,84 @@ public static void main(String[] args) { Jeans myJeans2 = new Jeans(new ArrayList<>(), "Name", 28, 30, JeansType.flare); myJeans.addPart(new BackPocket("Denim", 0.5, "blue", - new Button("steel", 0.02, "black", "Pocket Button"))) - .addPart(new BottomHem("Denim", 0.1, "blue")) - .addPart(new Button("steel", 0.05, "black", "Common Button")) - .addPart(new FrontPocket("Denim", 0.3, "blue")) - .addPart(new InsidePart("Cotton", 5, "black")) + new Button("steel", 0.02, "black", "Pocket Button", 150), 150)) + .addPart(new BottomHem("Denim", 0.1, "blue", 150)) + .addPart(new Button("steel", 0.05, "black", "Common Button", 250)) + .addPart(new FrontPocket("Denim", 0.3, "blue", 150)) + .addPart(new InsidePart("Cotton", 5, "black", 150)) .addPart(new OutsidePart("Denim", 5, "blue", - new OutsidePart.Pattern("Round", "Medium", "white"))) - .addPart(new Zipper("Steel", 0.1, "black", 0.15)); + new OutsidePart.Pattern("Round", "Medium", "white"), 150)) + .addPart(new Zipper("steel", 0.1, "black", 0.15, 150)); printJeans(myJeans); myJeans2.addPart(new BackPocket("Denim", 0.6, "blue", - new Button("steel", 0.02, "black", "Pocket Button"))) - .addPart(new BottomHem("Denim", 0.1, "blue")) - .addPart(new Button("steel", 0.04, "black", "Common Button")) - .addPart(new FrontPocket("Denim", 0.2, "blue")) - .addPart(new InsidePart("Cotton", 6, "black")) + new Button("steel", 0.02, "black", "Pocket Button", 200), 200)) + .addPart(new BottomHem("Denim", 0.1, "blue", 200)) + .addPart(new Button("steel", 0.04, "black", "Common Button", 200)) + .addPart(new FrontPocket("Denim", 0.2, "blue", 200)) + .addPart(new InsidePart("Cotton", 6, "black", 200)) .addPart(new OutsidePart("Denim", 6, "blue", - new OutsidePart.Pattern("Round", "Medium", "white"))) - .addPart(new Zipper("Steel", 0.1, "black", 0.2)); + new OutsidePart.Pattern("Round", "Medium", "white"), 200)) + .addPart(new Zipper("steel", 0.1, "black", 0.2, 200)); System.out.println(myJeans.equals(myJeans2)); System.out.println(); + + int price = myJeans.getParts().stream().filter(part -> part.getMaterial().equals("steel")) + .mapToInt(part -> part.getPrice()).reduce(0, (left, right) -> left + right); + + System.out.println("Price: " + price); + + + OptionalInt maxPrice = myJeans.getParts().stream().mapToInt(part -> { + if (part.getMaterial().equals("steel")) { + return part.getPrice(); + } + return 0; + }).max(); + + System.out.println("Max Price: " + maxPrice.getAsInt()); + + + int averagePrice = myJeans.getParts().stream().filter(part -> part.getMaterial().equals("steel")) + .mapToInt(part -> part.getPrice()).reduce(0, (left, right) -> left + right) + / myJeans.getParts().stream().filter(part -> part.getMaterial() == "steel").toArray().length; + + System.out.println("Average Price: " + averagePrice); + + + var arr = myJeans.getParts(). + stream().collect(Collectors.groupingBy(part -> part.getMaterial().equals("steel") ? "suitable" : "unsuitable")); + + System.out.println("Suitable: " + arr.get("suitable")); + System.out.println("Unsuitable: " + arr.get("unsuitable")); + + + ArrayList jeansList = new ArrayList<>(); + jeansList.add(myJeans); + jeansList.add(myJeans2); + + int totalPrice = jeansList.stream().flatMap(jeans -> jeans.getParts().stream()) + .mapToInt(part -> part.getPrice()).reduce(0, Integer::sum); + + System.out.println("Total price: " + totalPrice); + System.out.println(); + + } catch (JeansException e) { System.err.println(e.getMessage()); } - Button button = new Button("steel", 0.04, "black", "Common Button"); - Button button2 = new Button("plastic", 0.05, "grey", "Common Button"); + Button button = new Button("steel", 0.04, "black", "Common Button", 200); + Button button2 = new Button("plastic", 0.05, "grey", "Common Button", 200); System.out.println(button.equals(button2)); System.out.println(); + int number1 = 10; int number2 = 20; String word = "Hello number "; diff --git a/src/com/Jeans/OutsidePart.java b/src/com/Jeans/OutsidePart.java index 7855e81..50261c6 100644 --- a/src/com/Jeans/OutsidePart.java +++ b/src/com/Jeans/OutsidePart.java @@ -3,8 +3,8 @@ public class OutsidePart extends JeansParts { private Pattern pattern; - public OutsidePart(String material, double materialSize, String color, Pattern pattern) { - super(material, materialSize, color); + public OutsidePart(String material, double materialSize, String color, Pattern pattern, int price) { + super(material, materialSize, color, price); this.pattern = pattern; } diff --git a/src/com/Jeans/Zipper.java b/src/com/Jeans/Zipper.java index 492d782..e7aa2c0 100644 --- a/src/com/Jeans/Zipper.java +++ b/src/com/Jeans/Zipper.java @@ -3,8 +3,8 @@ public class Zipper extends JeansParts { private double zipperLength; - public Zipper(String material, double materialSize, String color, double zipperLength) { - super(material, materialSize, color); + public Zipper(String material, double materialSize, String color, double zipperLength, int price) { + super(material, materialSize, color, price); this.zipperLength = zipperLength; } diff --git a/test/com/Jeans/BackPocketTest.java b/test/com/Jeans/BackPocketTest.java index 547d6a3..f6f9d0d 100644 --- a/test/com/Jeans/BackPocketTest.java +++ b/test/com/Jeans/BackPocketTest.java @@ -10,7 +10,7 @@ public class BackPocketTest { public void checkBackPocketConstructor() { BackPocket backPocket = new BackPocket("Denim", 0.5, "blue", - new Button("steel", 0.02, "black", "Pocket Button")); + new Button("steel", 0.02, "black", "Pocket Button", 200), 200); Assertions.assertEquals("Denim", backPocket.getMaterial()); Assertions.assertEquals(0.5, backPocket.getMaterialSize()); @@ -28,7 +28,7 @@ void testBackPocket() { Button button = Mockito.mock(Button.class); button.setButtonType("Pocket Button"); Mockito.when(button.getButtonType()).thenReturn(str); - BackPocket backPocket = new BackPocket("Denim", 0.5, "blue", button); + BackPocket backPocket = new BackPocket("Denim", 0.5, "blue", button, 200); Assertions.assertEquals(backPocket.getBackPocketButton().getButtonType(), str); Mockito.verify(button).getButtonType(); } diff --git a/test/com/Jeans/BottomHemTest.java b/test/com/Jeans/BottomHemTest.java index 5398e37..63eb6a2 100644 --- a/test/com/Jeans/BottomHemTest.java +++ b/test/com/Jeans/BottomHemTest.java @@ -8,7 +8,7 @@ public class BottomHemTest { @Test public void checkBottomHemConstructor() { - BottomHem bottomHem = new BottomHem("Denim", 0.1, "blue"); + BottomHem bottomHem = new BottomHem("Denim", 0.1, "blue", 200); Assertions.assertEquals("Denim", bottomHem.getMaterial()); Assertions.assertEquals(0.1, bottomHem.getMaterialSize()); diff --git a/test/com/Jeans/ButtonTest.java b/test/com/Jeans/ButtonTest.java index a15a959..487a192 100644 --- a/test/com/Jeans/ButtonTest.java +++ b/test/com/Jeans/ButtonTest.java @@ -8,7 +8,7 @@ public class ButtonTest { @Test public void checkButtonConstructor() { - Button button = new Button("steel", 0.04, "black", "Common Button"); + Button button = new Button("steel", 0.04, "black", "Common Button", 200); Assertions.assertEquals(0.04, button.getMaterialSize()); Assertions.assertEquals("Common Button", button.getButtonType()); diff --git a/test/com/Jeans/FrontPocketTest.java b/test/com/Jeans/FrontPocketTest.java index c6e15eb..e898d89 100644 --- a/test/com/Jeans/FrontPocketTest.java +++ b/test/com/Jeans/FrontPocketTest.java @@ -8,7 +8,7 @@ public class FrontPocketTest { @Test public void checkFrontPocketConstructor() { - FrontPocket frontPocket = new FrontPocket("Denim", 0.3, "blue"); + FrontPocket frontPocket = new FrontPocket("Denim", 0.3, "blue", 200); Assertions.assertEquals("Denim", frontPocket.getMaterial()); Assertions.assertEquals(0.3, frontPocket.getMaterialSize()); diff --git a/test/com/Jeans/InsidePartTest.java b/test/com/Jeans/InsidePartTest.java index a70aae7..1dd426b 100644 --- a/test/com/Jeans/InsidePartTest.java +++ b/test/com/Jeans/InsidePartTest.java @@ -8,7 +8,7 @@ public class InsidePartTest { @Test public void checkInsidePartConstructor() { - InsidePart insidePart = new InsidePart("Cotton", 5, "black"); + InsidePart insidePart = new InsidePart("Cotton", 5, "black", 200); Assertions.assertEquals("Cotton", insidePart.getMaterial()); Assertions.assertEquals(5, insidePart.getMaterialSize()); diff --git a/test/com/Jeans/OutsidePartTest.java b/test/com/Jeans/OutsidePartTest.java index 857cdcc..8238aae 100644 --- a/test/com/Jeans/OutsidePartTest.java +++ b/test/com/Jeans/OutsidePartTest.java @@ -9,7 +9,7 @@ public class OutsidePartTest { public void checkOutsidePartConstructor() { OutsidePart outsidePart = new OutsidePart("Denim", 5, "blue", - new OutsidePart.Pattern("Round", "Medium", "white")); + new OutsidePart.Pattern("Round", "Medium", "white"), 200); Assertions.assertEquals("Denim", outsidePart.getMaterial()); Assertions.assertEquals(5, outsidePart.getMaterialSize()); diff --git a/test/com/Jeans/PatternTest.java b/test/com/Jeans/PatternTest.java index 6303c85..1a5d956 100644 --- a/test/com/Jeans/PatternTest.java +++ b/test/com/Jeans/PatternTest.java @@ -11,7 +11,7 @@ void testPattern() { OutsidePart.Pattern pattern = Mockito.mock(OutsidePart.Pattern.class); pattern.setPattern("Round"); Mockito.when(pattern.getPattern()).thenReturn(str); - OutsidePart outsidePart = new OutsidePart("Denim", 5, "blue", pattern); + OutsidePart outsidePart = new OutsidePart("Denim", 5, "blue", pattern, 200); Assertions.assertEquals(outsidePart.getPattern().getPattern(), str); Mockito.verify(pattern).getPattern(); } diff --git a/test/com/Jeans/ZipperTest.java b/test/com/Jeans/ZipperTest.java index 6d3cdba..3f37ccf 100644 --- a/test/com/Jeans/ZipperTest.java +++ b/test/com/Jeans/ZipperTest.java @@ -8,7 +8,7 @@ public class ZipperTest { @Test public void checkZipperConstructor() { - Zipper zipper = new Zipper("Steel", 0.1, "black", 0.15); + Zipper zipper = new Zipper("Steel", 0.1, "black", 0.15, 200); Assertions.assertEquals("Steel", zipper.getMaterial()); Assertions.assertEquals(0.1, zipper.getMaterialSize());