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..995b119d --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/aaronsantiago/WardrobeTest.java @@ -0,0 +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)); + } +}