From 44f1d5eb77af3bf38a479f997ed1a3871766cfd2 Mon Sep 17 00:00:00 2001 From: mekhi Date: Fri, 15 Mar 2024 14:10:52 +0000 Subject: [PATCH 1/3] Feat : Mekhi lesson 07 --- .../MekhiWilliams/MekhiWilliams07.java | 96 +++++++++++++++++++ .../MekhiWilliams/MekhiWilliams07Test.java | 39 ++++++++ 2 files changed, 135 insertions(+) create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java new file mode 100644 index 00000000..b9925ff6 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java @@ -0,0 +1,96 @@ +package com.codedifferently.lesson7.MekhiWilliams; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; + +@Configuration +@SpringBootApplication(scanBasePackages = "com.codedifferently") +public class MekhiWilliams07 { + + public static void main(String[] args) { + var application = new SpringApplication(MekhiWilliams07.class); + application.run(args); + + // Instantiate and use Sneaker class for demonstration + Sneaker sneaker = new Sneaker("Nike", "Air Max", 10.5, Sneaker.SneakerType.RUNNING, false); + sneaker.displaySneakerDetails(); + } +} + +// Custom class representing a sneaker +class Sneaker { + // Enum for different sneaker types + public enum SneakerType { + RUNNING, + BASKETBALL, + CASUAL + } + + // Member variables + private String brand; + private String model; + private double size; + private SneakerType type; + private boolean isLimitedEdition; + + // Constructor + public Sneaker( + String brand, String model, double size, SneakerType type, boolean isLimitedEdition) { + this.brand = brand; + this.model = model; + this.size = size; + this.type = type; + this.isLimitedEdition = isLimitedEdition; + } + + // Getter methods + public String getBrand() { + return brand; + } + + public String getModel() { + return model; + } + + public double getSize() { + return size; + } + + public SneakerType getType() { + return type; + } + + public boolean isLimitedEdition() { + return isLimitedEdition; + } + + // Function using conditional expression + public String getPopularity() { + return isLimitedEdition ? "Limited Edition" : "Regular Edition"; + } + + // Function using loop + public void displaySneakerDetails() { + System.out.println("Brand: " + brand); + System.out.println("Model: " + model); + System.out.println("Size: " + size); + System.out.println("Type: " + type); + System.out.println("Edition: " + getPopularity()); + } + + // Function throwing custom exception + public void validateSize() throws InvalidSizeException { + if (size <= 0) { + throw new InvalidSizeException("Size should be greater than 0"); + } + } + + // Custom exception class + public static class InvalidSizeException extends Exception { + public InvalidSizeException(String message) { + super(message); + } + } +} + diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java new file mode 100644 index 00000000..150176d5 --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java @@ -0,0 +1,39 @@ +package com.codedifferently.lesson7.MekhiWilliams; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class MekhiWilliams07Test { + + @Test + void testSneakerDetails() { + Sneaker sneaker = new Sneaker("Nike", "Air Max", 10.5, Sneaker.SneakerType.RUNNING, false); + assertEquals("Nike", sneaker.getBrand()); + assertEquals("Air Max", sneaker.getModel()); + assertEquals(10.5, sneaker.getSize()); + assertEquals(Sneaker.SneakerType.RUNNING, sneaker.getType()); + assertEquals(false, sneaker.isLimitedEdition()); + } + + @Test + void testPopularity() { + Sneaker sneaker1 = new Sneaker("Adidas", "Yeezy", 9.5, Sneaker.SneakerType.CASUAL, true); + Sneaker sneaker2 = new Sneaker("Nike", "Air Force 1", 11.0, Sneaker.SneakerType.CASUAL, false); + + assertEquals("Limited Edition", sneaker1.getPopularity()); + assertEquals("Regular Edition", sneaker2.getPopularity()); + } + + @Test + void testValidateSize() { + Sneaker sneaker = new Sneaker("Puma", "RS-X", -1, Sneaker.SneakerType.CASUAL, false); + + try { + sneaker.validateSize(); + } catch (Sneaker.InvalidSizeException e) { + assertEquals("Size should be greater than 0", e.getMessage()); + } + } +} + From 0427e2ae336aff69032a9a9ff76fbcd4b83ce64d Mon Sep 17 00:00:00 2001 From: mekhi Date: Fri, 15 Mar 2024 14:16:17 +0000 Subject: [PATCH 2/3] Feat: Mekhi Lesson 7 --- .../codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java | 1 + .../lesson7/MekhiWilliams/MekhiWilliams07Test.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java index b9925ff6..990af15d 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java @@ -94,3 +94,4 @@ public InvalidSizeException(String message) { } } + diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java index 150176d5..ebdde1fa 100644 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java @@ -36,4 +36,3 @@ void testValidateSize() { } } } - From 37055933051b7402e356098d205faa47b2770692 Mon Sep 17 00:00:00 2001 From: mekhi Date: Fri, 15 Mar 2024 14:21:42 +0000 Subject: [PATCH 3/3] Feat: Mekhi Lesson 7 --- .../codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java | 2 -- .../lesson7/MekhiWilliams/MekhiWilliams07Test.java | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java index 990af15d..88260efa 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07.java @@ -93,5 +93,3 @@ public InvalidSizeException(String message) { } } } - - diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java index ebdde1fa..150176d5 100644 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MekhiWilliams/MekhiWilliams07Test.java @@ -36,3 +36,4 @@ void testValidateSize() { } } } +