-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampsite.java
More file actions
115 lines (95 loc) · 3.22 KB
/
Campsite.java
File metadata and controls
115 lines (95 loc) · 3.22 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
/**
* Classes
*/
import java.util.*;
import org.junit.*;
import org.junit.runner.*;
public class Solution {
static class Item {
String name;
int expiry;
int quality;
public Item(String name, int expiry, int quality) {
this.name = name;
this.expiry = expiry;
this.quality = quality;
}
}
static class StoreInventory {
List<Item> items;
public StoreInventory(List<Item> items) {
this.items = items;
}
public List<Item> updateQuality() {
for (Item item : this.items) {
if (item.name.equals("Instant Ramen")) {
// do nothing
}
if (item.name.equals("Cheddar Cheese")) {
if (item.quality < 25) {
item.quality += 1;
}
item.expiry -= 1;
}
if (!item.name.equals("Cheddar Cheese") && !item.name.equals("Instant Ramen")) {
item.expiry -= 1;
if (item.expiry > 0) {
if (item.quality > 0) {
item.quality -= 1;
}
}
if (item.expiry < 0) {
if (item.quality > 1) {
item.quality -= 2;
}
if (item.quality == 1) {
item.quality -= 1;
}
}
}
}
return this.items;
}
}
/**
* Implementation
*/
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item("Apple", 10, 10));
items.add(new Item("Banana", 7, 9));
items.add(new Item("Strawberry", 5, 10));
items.add(new Item("Cheddar Cheese", 10, 16));
items.add(new Item("Instant Ramen", 0, 5));
items.add(new Item("Organic Avocado", 5, 16));
StoreInventory storeInventory = new StoreInventory(items);
int days = 2;
for (int i = 0; i < days; i++) {
System.out.println("Day " + i + " ---------------------------------");
System.out.println(" name expiry quality");
for (Item item : items) {
System.out.println(item.name + " " + item.expiry + " " + item.quality);
}
System.out.println();
storeInventory.updateQuality();
}
JUnitCore.main("Solution");
}
/**
* Tests
*/
@Test
public void shouldDecrementQualityDaily() {
List<Item> testItems = Arrays.asList(new Item("test", 10, 10));
StoreInventory testInventory = new StoreInventory(testItems);
testInventory.updateQuality();
Assert.assertEquals(9, testItems.get(0).quality);
}
@Test
public void shouldIncrementCheddarCheeseQualityDaily() {
List<Item> testItems = Arrays.asList(new Item("Cheddar Cheese", 10, 10));
StoreInventory testInventory = new StoreInventory(testItems);
testInventory.updateQuality();
Assert.assertEquals(11, testItems.get(0).quality);
}
}