-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathBasket.java
More file actions
46 lines (35 loc) · 1.15 KB
/
Basket.java
File metadata and controls
46 lines (35 loc) · 1.15 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
package com.booleanuk.core;
import java.util.ArrayList;
public class Basket {
private int maxCapcity = 5;
private ArrayList<String> items = new ArrayList<>();
public boolean add(String bagelName) {
if (items.contains(bagelName) || maxCapcity < items.size()) return false;
items.add(bagelName);
return true;
}
public boolean remove(String bagelName) {
if (!items.contains(bagelName)) return false;
items.remove(bagelName);
return true;
}
public void changeBasketCapacity(int capacity) {
if (capacity <= 0) return;
maxCapcity = capacity;
if (maxCapcity < items.size()) {
ArrayList<String> _newItemListing = new ArrayList<>();
for (int i = 0; i < maxCapcity; i++)
_newItemListing.add(items.get(i));
items = _newItemListing;
}
}
public int getCapacity() {
return maxCapcity;
}
public String getItemsAsString() {
StringBuilder _sb = new StringBuilder();
for (String item : items)
_sb.append(" - ").append(item).append("\n");
return _sb.toString();
}
}