Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson7 #233

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> clothes;
private String purchaseDate;
private String type;
private final int id;
private Map<Integer, String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}