Skip to content

Commit eb78119

Browse files
Karen AlabiKaren Alabi
authored andcommitted
feat: implements remove item method with custom exception for items not found in closet
1 parent 4c78dbb commit eb78119

File tree

1 file changed

+34
-21
lines changed
  • lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection

1 file changed

+34
-21
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/wardrobecollection/PersonalCloset.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,52 @@ public PersonalCloset (String ownerName, int maxCapacity) {
3636

3737
//core methods
3838

39-
// adds item to closet
39+
// method adds item to closet
4040
public boolean addItem(ClothingItem item) {
41-
// if closet is full, cannot add item
42-
if (items.size(0) >= maxCapacity) {
41+
// if closet is full, cannot add item
42+
if (items.size(0) >= maxCapacity) {
4343
return false;
4444
}
4545

46-
//adding item to closet and increasing closet total value of closet
47-
items.add(item);
48-
totalValue += item.getValue();
46+
//adding item to closet and increasing closet total value of closet
47+
items.add(item);
48+
totalValue += item.getValue();
4949

50-
// checks what season item is meant for and keeps track of number of items in that season
51-
Season seaason = item.getSeason();
52-
seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1);
50+
// checks what season item is meant for and keeps track of number of items in that season
51+
Season seaason = item.getSeason();
52+
seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1);
5353

54-
// returns true if item is added
55-
return true;
54+
// returns true if item is added
55+
return true;
5656
}
5757

58-
// removes item from closet
59-
public void removeItem(ClothingItem item) {
60-
return;
61-
}
58+
// method removes item from closet
59+
public void removeItem(ClothingItem item) throws ItemNotFoundException {
60+
// if item is not in closet, throws an error
61+
if (!item.contains(item)) {
62+
throw new ItemNotFoundException("Item is not in closet.");
63+
}
6264

63-
// throws exception if item is not found in closet
64-
public static class ItemNotFoundException extends Exception{
65-
}
65+
//remove item from closet and decreases toal value of closet
66+
item.remove(item);
67+
totalValue -= item.getValue();
6668

69+
//grab clothing item based on season and decrease count
70+
Season season = item.getSeason();
71+
seasonalItems.put(season, seasonalItems.get(season) - 1);
72+
}
6773

68-
// creates outfit by selecting items based on the season
74+
75+
// method creates outfit by selecting items based on the season
6976
public List<Clothing Item> createOutfit(Season season) {
7077
return new ArrayList<>();
7178
}
7279

73-
// organizes closet by type of item and color
80+
// method organizes closet by type of item and color
7481
public void organizeCloset() {
7582
}
7683

77-
// calculates amount of clothing items are in closet based on season
84+
// method calculates amount of clothing items are in closet based on season
7885
public Map<Season, Double> getSeasonalItem() {
7986
return new HashMap<>();
8087
}
@@ -104,5 +111,11 @@ public List<ClothingItem> getItems() {
104111
return new ArrayList<>(items);
105112
}
106113

114+
// custom exception if item is not found in closet
115+
public static class ItemNotFoundException extends Exception{
116+
public ItemNotFoundException(String message) {
117+
super(message);
118+
}
119+
}
107120
}
108121

0 commit comments

Comments
 (0)