From 5b6f9fa15d3b021bdfdd75f82e43575a17c78446 Mon Sep 17 00:00:00 2001 From: Aaron2278 Date: Mon, 18 Mar 2024 05:40:06 +0000 Subject: [PATCH 1/2] feat: Wardrobe and WardrobeTest.java --- .../lesson7/aaronsantiago/Wardrobe.java | 122 ++++++++++++++++++ .../lesson7/aaronsantiago/WardrobeTest.java | 52 ++++++++ 2 files changed, 174 insertions(+) create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/aaronsantiago/Wardrobe.java create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/aaronsantiago/Wardrobe.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/aaronsantiago/Wardrobe.java new file mode 100644 index 00000000..985cfdf6 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/aaronsantiago/Wardrobe.java @@ -0,0 +1,122 @@ +package com.codedifferently.lesson7.aaronsantiago; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Wardrobe { + + private String ownerName; + private int capacity; + private List clothes; + private String purchaseDate; + private String type; + private final int id; + private Map additionalInfo = new HashMap<>(); + + public Wardrobe(String ownerName, int capacity, String purchaseDate, String type, int id) { + this.ownerName = ownerName; + this.capacity = capacity; + this.purchaseDate = purchaseDate; + this.type = type; + this.id = id; + this.clothes = new ArrayList<>(); + } + + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public List getClothes() { + return clothes; + } + + public String getPurchaseDate() { + return purchaseDate; + } + + public void setPurchaseDate(String purchaseDate) { + this.purchaseDate = purchaseDate; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getId() { + return id; + } + + public void addAdditionalInfo(int key, String value) { + additionalInfo.put(key, value); + } + + public String getAdditionalInfo(int key) { + return additionalInfo.get(key); + } + + public void removeAdditionalInfo(int key) { + additionalInfo.remove(key); + } + + public void addClothes(String item) throws Exception { + if (clothes.size() < capacity) { + clothes.add(item); + } else { + throw new Exception("Wardrobe is full! Cannot add more clothes."); + } + } + + public void removeClothes(String item) throws Exception { + if (clothes.contains(item)) { + clothes.remove(item); + } else { + throw new Exception("Item not found in the wardrobe."); + } + } + + public void checkClothes() { + for (int i = 0; i < clothes.size(); i++) { + String item = clothes.get(i); + System.out.println(item); + } + } + + @Override + public String toString() { + return "Owner: " + + ownerName + + "\n" + + "Capacity: " + + capacity + + "\n" + + "Current Clothes: " + + clothes + + "\n" + + "Purchase Date: " + + purchaseDate + + "\n" + + "Type: " + + type + + "\n" + + "ID: " + + id; + } +} diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java new file mode 100644 index 00000000..921fe1dc --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java @@ -0,0 +1,52 @@ +package com.codedifferently.lesson7.aaronsantiago; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class WardrobeTest { + + @Test + public void testAddClothes() throws Exception { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addClothes("T-shirt"); + assertEquals(1, wardrobe.getClothes().size()); + } + + @Test + public void testRemoveClothes() throws Exception { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addClothes("T-shirt"); + wardrobe.removeClothes("T-shirt"); + assertEquals(0, wardrobe.getClothes().size()); + } + + @Test + public void testAddClothesExceedingCapacity() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 1, "2024-01-01", "Walk-in", 12345); + assertDoesNotThrow(() -> wardrobe.addClothes("T-shirt")); + Exception exception = assertThrows(Exception.class, () -> wardrobe.addClothes("Jeans")); + assertEquals("Wardrobe is full! Cannot add more clothes.", exception.getMessage()); + } + + @Test + public void testRemoveClothesNotInWardrobe() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + Exception exception = assertThrows(Exception.class, () -> wardrobe.removeClothes("Sweater")); + assertEquals("Item not found in the wardrobe.", exception.getMessage()); + } + + @Test + public void testAddAdditionalInfo() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); + assertEquals("Preferred style: Casual", wardrobe.getAdditionalInfo(1)); + } + + @Test + public void testRemoveAdditionalInfo() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); + wardrobe.removeAdditionalInfo(1); + assertNull(wardrobe.getAdditionalInfo(1)); + } +} From 1fe9719991174793c6472a7f72e7048040a92723 Mon Sep 17 00:00:00 2001 From: Aaron2278 Date: Mon, 18 Mar 2024 05:43:37 +0000 Subject: [PATCH 2/2] fix: formatting --- .../lesson7/aaronsantiago/WardrobeTest.java | 89 ++++++++++--------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java index 921fe1dc..995b119d 100644 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java @@ -1,52 +1,53 @@ package com.codedifferently.lesson7.aaronsantiago; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; public class WardrobeTest { - @Test - public void testAddClothes() throws Exception { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); - wardrobe.addClothes("T-shirt"); - assertEquals(1, wardrobe.getClothes().size()); - } - - @Test - public void testRemoveClothes() throws Exception { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); - wardrobe.addClothes("T-shirt"); - wardrobe.removeClothes("T-shirt"); - assertEquals(0, wardrobe.getClothes().size()); - } - - @Test - public void testAddClothesExceedingCapacity() { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 1, "2024-01-01", "Walk-in", 12345); - assertDoesNotThrow(() -> wardrobe.addClothes("T-shirt")); - Exception exception = assertThrows(Exception.class, () -> wardrobe.addClothes("Jeans")); - assertEquals("Wardrobe is full! Cannot add more clothes.", exception.getMessage()); - } - - @Test - public void testRemoveClothesNotInWardrobe() { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); - Exception exception = assertThrows(Exception.class, () -> wardrobe.removeClothes("Sweater")); - assertEquals("Item not found in the wardrobe.", exception.getMessage()); - } - - @Test - public void testAddAdditionalInfo() { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); - wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); - assertEquals("Preferred style: Casual", wardrobe.getAdditionalInfo(1)); - } - - @Test - public void testRemoveAdditionalInfo() { - Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); - wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); - wardrobe.removeAdditionalInfo(1); - assertNull(wardrobe.getAdditionalInfo(1)); - } + @Test + public void testAddClothes() throws Exception { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addClothes("T-shirt"); + assertEquals(1, wardrobe.getClothes().size()); + } + + @Test + public void testRemoveClothes() throws Exception { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addClothes("T-shirt"); + wardrobe.removeClothes("T-shirt"); + assertEquals(0, wardrobe.getClothes().size()); + } + + @Test + public void testAddClothesExceedingCapacity() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 1, "2024-01-01", "Walk-in", 12345); + assertDoesNotThrow(() -> wardrobe.addClothes("T-shirt")); + Exception exception = assertThrows(Exception.class, () -> wardrobe.addClothes("Jeans")); + assertEquals("Wardrobe is full! Cannot add more clothes.", exception.getMessage()); + } + + @Test + public void testRemoveClothesNotInWardrobe() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + Exception exception = assertThrows(Exception.class, () -> wardrobe.removeClothes("Sweater")); + assertEquals("Item not found in the wardrobe.", exception.getMessage()); + } + + @Test + public void testAddAdditionalInfo() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); + assertEquals("Preferred style: Casual", wardrobe.getAdditionalInfo(1)); + } + + @Test + public void testRemoveAdditionalInfo() { + Wardrobe wardrobe = new Wardrobe("Aaron Santiago", 5, "2024-01-01", "Walk-in", 12345); + wardrobe.addAdditionalInfo(1, "Preferred style: Casual"); + wardrobe.removeAdditionalInfo(1); + assertNull(wardrobe.getAdditionalInfo(1)); + } }