Skip to content

Commit c8b3b9d

Browse files
Karen AlabiKaren Alabi
authored andcommitted
fix: corrects items.size() and seasonal counts in Personal Closet class
1 parent b2d760c commit c8b3b9d

File tree

1 file changed

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

1 file changed

+47
-24
lines changed

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,70 +34,93 @@ public PersonalCloset (String ownerName, int maxCapacity) {
3434
this.seasonalItems = new HashMap<>();
3535
}
3636

37-
//core methods
37+
// Core methods
3838

39-
// method 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() >= maxCapacity) {
4343
return false;
4444
}
4545

46-
//adding item to closet and increasing closet total value of closet
46+
// Adding item to closet and increasing total value of closet
4747
items.add(item);
4848
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();
50+
// Checks what season item is meant for and keeps track of number of items in that season
51+
Season season = item.getSeason();
5252
seasonalItems.put(season, seasonalItems.getOrDefault(season, 0) + 1);
5353

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

58-
// method removes item from closet
58+
// Method removes item from closet
5959
public void removeItem(ClothingItem item) throws ItemNotFoundException {
60-
// if item is not in closet, throws an error
61-
if (!item.contains(item)) {
60+
// If item is not in closet, throws an error
61+
if (!items.contains(item)) {
6262
throw new ItemNotFoundException("Item is not in closet.");
6363
}
6464

65-
//remove item from closet and decreases toal value of closet
66-
item.remove(item);
65+
// Remove item from closet and decreases toal value of closet
66+
items.remove(item);
6767
totalValue -= item.getValue();
6868

69-
//grab clothing item based on season and decrease count
69+
// Grab clothing item based on season and decrease count
7070
Season season = item.getSeason();
7171
seasonalItems.put(season, seasonalItems.get(season) - 1);
7272
}
7373

7474

75-
// method creates outfit by selecting items based on the season
75+
// Method creates outfit by selecting items based on the season
7676
public List<ClothingItem> createOutfit(Season season) {
77-
// creating empty list that stores clothing items
77+
// Creating empty list that stores clothing items
7878
List<ClothingItem> outfit = new ArrayList<>();
7979

80-
// iterating through all items in the closet and grabbing item at index
80+
// Iterating through all items in the closet and grabbing item at index
8181
for (int i = 0; i < items.size(); i++) {
8282
ClothingItem item = items.get(i);
8383

84-
//check if clothign item matches particular season or is good for all seasons
84+
// Check if clothing item matches particular season or is good for all seasons
8585
if (item.getSeason() == season || item.getSeason() == Season.ALL_SEASON) {
8686
//add item to list
8787
outfit.add(item);
8888
}
8989
}
90-
// returns final list of clothing items in an outfit
91-
return outfits;
90+
// Returns final list of clothing items in an outfit
91+
return outfit;
9292
}
9393

94-
// method organizes closet by type of item and color
94+
// Method organizes closet by type of item and color
9595
public void organizeCloset() {
96+
// Create a map where key is type of clothing and value is list of clothing items of that type
97+
Map<String, List<ClothingItem>> organized = new HashMap<>();
98+
99+
100+
// Iterate through every item in closet
101+
for (int i = 0; i < items.size(); i++) {
102+
ClothingItem item = items.get(i);
103+
String type = item.getType();
104+
105+
// Check if that type of clothing item is in the list
106+
List<ClothingItem> itemList = organized.get(type);
107+
if (itemList == null) {
108+
// If it doesn’t exist, make a new list and put it in the map
109+
itemList = new ArrayList<>();
110+
organized.put(type, itemList);
111+
}
112+
113+
// Adding items to list by type
114+
itemList.add(item);
115+
}
116+
117+
// Mark closet as organized
118+
isOrganized = true;
96119
}
97120

98-
// method calculates amount of clothing items are in closet based on season
121+
// method returns a map shwoing how many items are in closet based on season
99122
public Map<Season, Double> getSeasonalItem() {
100-
return new HashMap<>();
123+
return new HashMap<>(seasonalItems);
101124
}
102125

103126
// getters and setters
@@ -125,7 +148,7 @@ public List<ClothingItem> getItems() {
125148
return new ArrayList<>(items);
126149
}
127150

128-
// custom exception if item is not found in closet
151+
// Custom exception if item is not found in closet
129152
public static class ItemNotFoundException extends Exception{
130153
public ItemNotFoundException(String message) {
131154
super(message);

0 commit comments

Comments
 (0)