-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.java
118 lines (102 loc) · 2.61 KB
/
Inventory.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
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
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Inventory implements Constants{
private ArrayList<Item> items;
public Inventory()
{
items = new ArrayList<Item>();
}
public void addItemsFromFile(String fileName) throws IOException
{
FileInputStream fileIn= new FileInputStream(fileName);
BufferedReader read = new BufferedReader(new InputStreamReader(fileIn));
String line = read.readLine();
while (line != null){
Item a = readValues(line);
items.add(a);
line = read.readLine();
}
}
public Item readValues(String r)
{
Scanner s = new Scanner(r).useDelimiter(";");
int id = s.nextInt();
String desc = s.next();
int quantity = s.nextInt();
double price = s.nextDouble();
int supID = s.nextInt();
return new Item (id, desc, quantity, price, supID);
}
public Item findItem(int id)
{
for (Item a: items) {
if(a.getId()==id)
return a;
}
return null;
}
public Item findItem(String desc)
{
for (Item a: items){
if (a.getDesc().equals(desc))
return a;
}
return null;
}
public boolean checkIfOrder()
{
for (Item i: items){
if (i.getQuantity()<THRESHOLD)
return true;
}
return false;
}
public void generateOrder(Order o, boolean stars)throws FileNotFoundException
{
for (Item i: items){
if (i.getQuantity()<THRESHOLD){
o.addLine(i);
i.setQuantity(50);
}
}
o.createOrder(stars);
}
public int decreaseItemQuan(int id, int quantity)
{
Item i = findItem(id);
if (i!=null) {
if (i.getQuantity() >= quantity) {
i.setQuantity(i.getQuantity() - quantity);
return 1;
} else
return 3;
}
return 2;
}
public String getItemInfo(int id)
{
Item i = findItem(id);
if (i!=null)
return i.toString();
else
return "Item not found!";
}
public String getItemInfo(String desc)
{
Item i = findItem(desc);
if (i!=null)
return i.toString();
else
return "Item not found!";
}
public void printAllItems()
{
for (Item i: items)
System.out.println(i);
}
public ArrayList<Item> getItems()
{
return items;
}
}