-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dresser.java
177 lines (176 loc) · 4.1 KB
/
Dresser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Created by Noah Shaw
*/
public class Dresser {
//Instance variables
private Clothing[][] clothes;
public static final int numOfDrawers = 5;
public static final int numOfClothes = 10;
//Constructors
public Dresser()
{
clothes = new Clothing[numOfDrawers][numOfClothes];
}
//No accessors/mutators
//Methods
public void addClothing(Clothing aClothing) //0-Undergarment 1-Socks or Stockings 2-Top 3-Bottom 4-Cape
{
//Clothing is a undergarment
if(aClothing.getType().equalsIgnoreCase("Undergarment"))
{
//If the drawer is full, state it
if(clothes[0][numOfClothes - 1] != null) //Array is full
{
System.out.println("Sorry that drawer is full.");
return;
}
//Else place the clothing in the drawer
else
{
for(int i = 0; i < numOfClothes; i++) //Found an empty space
{
if(clothes[0][i] == null)
{
clothes[0][i] = aClothing;
break;
}
}
}
}
//Clothing is socks or stockings
else if(aClothing.getType().equalsIgnoreCase("Socks") || aClothing.getType().equalsIgnoreCase("Stockings"))
{
//If the drawer is full, state it
if(clothes[1][numOfClothes-1] != null) //Array is full
{
System.out.println("Sorry that drawer is full.");
return;
}
//Else place the clothing in the drawer
else
{
for(int i = 0; i < numOfClothes; i++) //Found an empty space
{
if(clothes[1][i] == null)
{
clothes[1][i] = aClothing;
break;
}
}
}
}
//Clothing is a top
else if(aClothing.getType().equalsIgnoreCase("Top"))
{
//If the drawer is full, state it
if(clothes[2][numOfClothes-1] != null) //Array is full
{
System.out.println("Sorry that drawer is full.");
return;
}
//Else place the clothing in the drawer
else
{
for(int i = 0; i < numOfClothes; i++) //Found an empty space
{
if(clothes[2][i] == null)
{
clothes[2][i] = aClothing;
break;
}
}
}
}
//Clothing is a bottom
else if(aClothing.getType().equalsIgnoreCase("Bottom"))
{
//If the drawer is full, state it
if(clothes[3][numOfClothes-1] != null) //Array is full
{
System.out.println("Sorry that drawer is full.");
return;
}
//Else place the clothing in the drawer
else
{
for(int i = 0; i < numOfClothes; i++) //Found an empty space
{
if(clothes[3][i] == null)
{
clothes[3][i] = aClothing;
break;
}
}
}
}
//Clothing is a cape
else if(aClothing.getType().equalsIgnoreCase("Cape"))
{
//If the drawer is full, state it
if(clothes[4][numOfClothes-1] != null) //Array is full
{
System.out.println("\nSorry that drawer is full.");
return;
}
//Else place the clothing in the drawer
else
{
for(int i = 0; i < numOfClothes; i++) //Found an empty space
{
if(clothes[4][i] == null)
{
clothes[4][i] = aClothing;
break;
}
}
}
}
}
public void removeClothing(Clothing aClothing)
{
int rmIndex = -1; //Sentinel value
int rmIndex2 = -1;
for(int i = 0; i < numOfDrawers; i++) //Find the index of the item to remove
{
for(int j = 0; j < numOfClothes; j++)
{
if(clothes[i][j] != null && clothes[i][j].equals(aClothing))
{
rmIndex = i;
rmIndex2 = j;
break;
}
}
}
if(rmIndex == -1 && rmIndex2 == -1) //Item was not found
{
return;
}
for(int i = rmIndex; i < numOfDrawers; i++) //Shift all items forward
{
for(int j = rmIndex2; j < numOfClothes - 1; j++)
{
clothes[i][j] = clothes[i][j + 1];
}
}
clothes[numOfDrawers - 1][numOfClothes - 1] = null; //Set the last item to null
}
public void print()
{
for(int i = 0; i < numOfDrawers; i++)
{
System.out.println("\nDrawer: "+ (i+1)); //Print the drawer
for(int j = 0; j < numOfClothes; j++)
{
if(clothes[i][j] == null) //Don't print the null items
{
break;
}
else
{
System.out.println(clothes[i][j]); //Print each item
}
}
}
}
}