-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClothingStack.java
More file actions
143 lines (126 loc) · 3.91 KB
/
ClothingStack.java
File metadata and controls
143 lines (126 loc) · 3.91 KB
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
package module5.datastructures.stackqueue;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
/**
* A Collection which implements a fixed-capacity stack of Clothing items.
*
* @implSpec This is an non-standard, incomplete implementation of the Collection interface,
* which only supports modification using the custom {@code push}, {@code pop}, and {@code peek} methods.
* The stack does not allow modification of its inner elements,
* so {@code add} and {@code remove} are not implemented.
*
* @author Mae Morella
*/
public class ClothingStack extends AbstractCollection<Clothing> implements Iterable<Clothing>, Cloneable {
private static final int DEFAULT_CAPACITY = 20;
/** An array representing the elements in the stack */
private Clothing[] elements;
/** The number of items in the stack. The topmost element is located at index {@code size - 1}. */
private int size;
/** The maximum number of items the stack can hold; this is the size of the array. */
private final int capacity;
/**
* Create a new ClothingStack with a capacity of 20
*/
public ClothingStack() {
this(null);
}
/**
* Create a new ClothingStack with a capacity of 20
*
* @param clothes A collection of Clothes, such as another Stack.
*/
public ClothingStack(Collection<Clothing> clothes) {
this.capacity = DEFAULT_CAPACITY;
this.size = 0;
elements = new Clothing[capacity];
if (clothes != null) {
for (Clothing c : clothes) {
this.push(c);
}
}
}
/** Add an element to the top of the stack */
public boolean push(Clothing c) {
if (isFull()) {
return false;
}
elements[size++] = c;
return true;
}
/** @return the element at the top of the stack, and remove it */
public Clothing pop() {
if (isEmpty()) {
return null;
}
return elements[--size];
}
/** @return the element at the top of the stack, without removing it */
public Clothing peek() {
if (isEmpty()) {
return null;
}
return elements[size - 1];
}
@Override
public int size() {
return this.size;
}
public int getCapacity() {
return this.capacity;
}
/** @return {@code true} if the stack has reached its maximum capacity */
public boolean isFull() {
return (size == capacity);
}
@Override
public Iterator<Clothing> iterator() {
return new Iterator<Clothing>() {
private int nextIndex = size - 1; // index of the next
@Override
public boolean hasNext() {
return (nextIndex != -1);
}
@Override
public Clothing next() {
if (hasNext()) {
return elements[nextIndex--];
}
throw new IndexOutOfBoundsException();
}
};
}
@Override
public ClothingStack clone() {
return new ClothingStack(this);
}
public void displayAllClothes() {
System.out.println();
for (Clothing c : this) {
System.out.println(" * " + c.toString());
}
}
public ClothingStack filterColor(String color) {
ClothingStack filtered = new ClothingStack();
for (Clothing c : this) {
if (c.getColor().matches(color)) {
filtered.push(c);
}
}
return filtered;
}
public ClothingStack filterWashable(boolean machineWashable) {
ClothingStack filtered = new ClothingStack();
for (Clothing c : this) {
if (c.isMachineWashable() == machineWashable) {
filtered.push(c);
}
}
return filtered;
}
public static void main(String[] args) {
ClothingStack c = new ClothingStack();
c.push(new Clothing("Shirt", "Red", true));
}
}