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
116
117
118
119
120
121
|
/*
* IceBox - inventory management software for restaurants
* Copyright (C) 2016 Delwink, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.delwink.icebox;
import com.delwink.icebox.QuantityUpdate.Record;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* An update to inventory quantity.
* @author David McMackins II
*/
public class QuantityUpdate implements Iterable<Record> {
protected final List<Record> RECORDS;
protected Date updateDate;
public QuantityUpdate(Date date) {
RECORDS = new ArrayList<>();
updateDate = date;
}
public Record getItemByID(int itemID) {
for (Record r : RECORDS)
if (r.getItemID() == itemID)
return r;
return null;
}
public void addItem(int itemID, int sold, int waste) {
Record r = getItemByID(itemID);
if (r == null) {
RECORDS.add(new Record(itemID, sold, waste));
} else {
r.setSold(r.getSold() + sold);
r.setWaste(r.getWaste() + waste);
}
}
public Date getDate() {
return updateDate;
}
public void setDate(Date updateDate) {
this.updateDate = updateDate;
}
public List<Record> getRecords() {
return RECORDS;
}
@Override
public Iterator<Record> iterator() {
return RECORDS.iterator();
}
/**
* A single record of this quantity update.
*/
public class Record {
protected final int itemID;
protected int sold, waste;
/**
* Creates a new record with unknown quantities.
* @param itemID The ID of this record's item.
*/
public Record(int itemID) {
this(itemID, 0, 0);
}
/**
* Creates a new record with known quantities.
* @param itemID The ID of this record's item.
* @param sold The number of units sold.
* @param waste The number of units wasted.
*/
public Record(int itemID, int sold, int waste) {
this.itemID = itemID;
this.sold = sold;
this.waste = waste;
}
public int getItemID() {
return itemID;
}
public int getSold() {
return sold;
}
public void setSold(int sold) {
this.sold = sold;
}
public int getWaste() {
return waste;
}
public void setWaste(int waste) {
this.waste = waste;
}
}
}
|