Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 410ea85

Browse files
committed
Sorted Collections ,Challenge
1 parent 6d9c8ca commit 410ea85

File tree

26 files changed

+952
-0
lines changed

26 files changed

+952
-0
lines changed

‎CODE/076_Sorted-Collections/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CODE/076_Sorted-Collections/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CODE/076_Sorted-Collections/.idea/workspace.xml

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
2.69 KB
Binary file not shown.
3.39 KB
Binary file not shown.
2.68 KB
Binary file not shown.
3.47 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Collections;
2+
import java.util.Map;
3+
import java.util.TreeMap;
4+
5+
public class Basket {
6+
private final String name;
7+
private final Map<StockItem, Integer> list;
8+
9+
public Basket(String name) {
10+
this.name = name;
11+
this.list = new TreeMap<>();
12+
}
13+
14+
public int addToBasket(StockItem item, int quantity) {
15+
if ((item != null) && (quantity > 0)) {
16+
int inBasket = list.getOrDefault(item, 0);
17+
list.put(item, inBasket + quantity);
18+
return inBasket;
19+
}
20+
return 0;
21+
}
22+
23+
public Map<StockItem, Integer> Items() {
24+
return Collections.unmodifiableMap(list);
25+
}
26+
27+
@Override
28+
public String toString() {
29+
String s = "\nShopping basket " + name + " contains " + list.size() + ((list.size() == 1) ? " item" : " items") + "\n";
30+
double totalCost = 0.0;
31+
for (Map.Entry<StockItem, Integer> item : list.entrySet()) {
32+
s = s + item.getKey() + ". " + item.getValue() + " purchased\n";
33+
totalCost += item.getKey().getPrice() * item.getValue();
34+
}
35+
return s + "Total cost " + totalCost;
36+
}
37+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.util.Map;
2+
3+
public class Main {
4+
private static StockList stockList = new StockList();
5+
6+
public static void main(String[] args) {
7+
StockItem temp = new StockItem("bread", 0.86, 100);
8+
stockList.addStock(temp);
9+
10+
temp = new StockItem("cake", 1.10, 7);
11+
stockList.addStock(temp);
12+
13+
temp = new StockItem("car", 12.50, 2);
14+
stockList.addStock(temp);
15+
16+
temp = new StockItem("chair", 62.0, 10);
17+
stockList.addStock(temp);
18+
19+
temp = new StockItem("cup", 0.50, 200);
20+
stockList.addStock(temp);
21+
temp = new StockItem("cup", 0.45, 7);
22+
stockList.addStock(temp);
23+
24+
temp = new StockItem("door", 72.95, 4);
25+
stockList.addStock(temp);
26+
27+
temp = new StockItem("juice", 2.50, 36);
28+
stockList.addStock(temp);
29+
30+
temp = new StockItem("phone", 96.99, 35);
31+
stockList.addStock(temp);
32+
33+
temp = new StockItem("towel", 2.40, 80);
34+
stockList.addStock(temp);
35+
36+
temp = new StockItem("vase", 8.76, 40);
37+
stockList.addStock(temp);
38+
39+
System.out.println(stockList);
40+
41+
for(String s: stockList.Items().keySet()) {
42+
System.out.println(s);
43+
}
44+
45+
Basket timsBasket = new Basket("Tim");
46+
sellItem(timsBasket, "car", 1);
47+
System.out.println(timsBasket);
48+
49+
sellItem(timsBasket, "car", 1);
50+
System.out.println(timsBasket);
51+
52+
if(sellItem(timsBasket, "car", 1) != 1) {
53+
System.out.println("There are no more cars in stock");
54+
}
55+
56+
sellItem(timsBasket, "spanner", 5);
57+
System.out.println(timsBasket);
58+
59+
sellItem(timsBasket, "juice", 4);
60+
sellItem(timsBasket, "cup", 12);
61+
sellItem(timsBasket, "bread", 1);
62+
System.out.println(timsBasket);
63+
64+
System.out.println(stockList);
65+
66+
//temp = new StockItem("pen", 1.12);
67+
//stockList.Items().put(temp.getName(), temp);
68+
stockList.Items().get("car").adjustStock(2000);
69+
stockList.get("car").adjustStock(-1000);
70+
System.out.println(stockList);
71+
for(Map.Entry<String, Double> price: stockList.PriceList().entrySet()) {
72+
System.out.println(price.getKey() + " costs " + price.getValue());
73+
}
74+
75+
76+
}
77+
78+
public static int sellItem(Basket basket, String item, int quantity) {
79+
//retrieve the item from stock list
80+
StockItem stockItem = stockList.get(item);
81+
if(stockItem == null) {
82+
System.out.println("We don't sell " + item);
83+
return 0;
84+
}
85+
if(stockList.sellStock(item, quantity) != 0) {
86+
basket.addToBasket(stockItem, quantity);
87+
return quantity;
88+
}
89+
return 0;
90+
}
91+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /