-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathBasket.java
More file actions
73 lines (61 loc) · 1.8 KB
/
Basket.java
File metadata and controls
73 lines (61 loc) · 1.8 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
package com.booleanuk.core;
import java.util.ArrayList;
import java.util.List;
public class Basket {
private static int capacity = 5;
private List<Item> items = new ArrayList<>();
public boolean addItem(String sku, double price, String name, String variant, int size) {
Item item = new Item(sku, price, name, variant, size);
return addItem(item);
}
public boolean addItem(Item item) {
if (canFitItem(item)) {
items.add(item);
return true;
}
return false;
}
public boolean canFitItem(Item item) {
if (items.size() + item.getSize() > capacity) {
System.out.println("You cannot fit more items in your basket.");
return false;
}
return true;
}
public boolean addItem(String sku, double price, String name, String variant) {
// Shorthand, assuming size=1
return addItem(sku, price, name, variant, 1);
}
public boolean removeItem(String sku) {
int indexToRemove = 0;
boolean foundItem = false;
for (int i=0; i<items.size(); i++) {
if (sku.equals(items.get(i).getSku())) {
indexToRemove = i;
foundItem = true;
break;
}
}
if (foundItem) {
items.remove(indexToRemove);
return true;
}
return false;
}
public double getTotalCost() {
double totalCost = 0;
for (Item item : items) {
totalCost += item.getPrice();
}
return totalCost;
}
public List<Item> getItems() {
return items;
}
public static void setCapacity(int newCapacity) {
capacity = newCapacity;
}
public static int getCapacity() {
return capacity;
}
}