-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalesHistory.java
70 lines (59 loc) · 2.2 KB
/
SalesHistory.java
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
/*
This class will allow you to store product sales history.
Products (Item class in this case) have a name and number sold
Sales history is sorted according to the date of sales and will allow for easy additions as records become available
*/
import java.util.Date;
import java.util.HashMap;
import java.util.TreeMap;
/**
*
* @author Vincent Tereso
*/
public class SalesHistory {
//Item class just to simplify parameters
static class Item{
String name;
int numberSold;
Item(String name, int numberSold){
this.name=name;
this.numberSold=numberSold;
}
}
TreeMap<Date,HashMap<String,Integer>> sales;
public SalesHistory(){
this.sales=new TreeMap<>();
}
public void getHistory(Date date){
HashMap<String,Integer> daySales=sales.get(date);
if(daySales==null)System.out.println("No records for this date");
else{
System.out.printf("Date: %s\n",date);
for(String key:daySales.keySet())System.out.printf("Item: %-10s Sold: %d\n",key,daySales.get(key));
}
System.out.println();
}
public void getFullHistory(){
sales.keySet().forEach(this::getHistory);
}
public void addToRecord(Date date, Item item){
HashMap<String,Integer> daySales=sales.get(date);
if(daySales==null){
daySales=new HashMap<>();
sales.put(date, daySales);
}
daySales.merge(item.name,item.numberSold,Integer::sum);
}
public static void main(String[]args){
SalesHistory history=new SalesHistory();
//This Date constructor is deprecated, but just for example purposes
//Demo data
history.addToRecord(new Date(2000,12,10),new Item("Apples",200));
history.addToRecord(new Date(2000,12,10),new Item("Oranges",200));
history.addToRecord(new Date(2000,12,10),new Item("Apples",200));
history.addToRecord(new Date(2018,3,16),new Item("Nuts",5));
history.addToRecord(new Date(2000,12,11),new Item("Apples",201));
history.addToRecord(new Date(2000,12,12),new Item("Apples",202));
history.getFullHistory();
}
}