-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
224 lines (199 loc) · 7.93 KB
/
Inventory.java
File metadata and controls
224 lines (199 loc) · 7.93 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.nio.file.*;
import java.io.*;
import java.util.*;
/**
* Uses the linked list data type to create and maintain a list of stock in the inventory.
*
* @author Hasti Abbasi Kenarsari
*/
public class Inventory {
private static LinkedList<Produce> inventory = new LinkedList<Produce>();
private static Path filePath = Paths.get("inventory.txt");
/**
* Utilizes a file scanner to gather information from the inventory text file. Breaks the
* file into tokens that contain information about the available products, and adds
* the data regarding the product name, count, and season to the inventory.
*
* @throws IOException Throws an exception if an error occured while reading the file.
*/
public Inventory() throws IOException {
if (Files.exists(filePath)) {
Scanner fileScanner = new Scanner(filePath);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.isEmpty()) {
continue;
}
String[] tokens = line.split(";");
for (int i = 0; i < tokens.length; i += 3) {
String produceName = tokens[i];
int count = Integer.parseInt(tokens[i + 1]);
String season = tokens[i + 2];
addProduce(new Produce(produceName, season), count);
}
}
fileScanner.close();
} else {
Files.createFile(filePath);
throw new IOException("An error occured while reading the file.");
}
}
/**
* Removes a specified quantity of produce items from the inventory.
*
* @param produce the String representation of the produce item
* @param quantity the # of produce items desired to be removed
* @return returns true if the specified quantity has been removed, and false otherwise.
*/
public static Produce removeProduce(String produce, int quantity) {
int count = quantity;
Produce temp = null;
// goes through linked list
for(int i = 1; i < inventory.getLength() + 1; i++) {
// gets current produce
String current = inventory.getEntry(i).getName();
/*
* if produce is the same as the one specified in the parameter & all desired instances
* haven't been removed, it gets removed & count is decreased
*/
if(current.equalsIgnoreCase(produce)) {
temp = inventory.decreaseProduce(i, count);
}
}
try {
saveToFile();
} catch (IOException e) {
System.out.println("Error saving to file");
}
return temp;
}
/**
* Checks if a specific quantity of a produce item is in the inventory.
*
* @param produce the String representation of the produce item
* @param quantity the # of produce items desired to be checked
* @return returns true if the quantity of items is in stock
*/
public static boolean inStock(String produce, int quantity) {
Produce temp = getProduce(produce);
if (temp == null) {
return false;
}
int pos = inventory.getPosition(temp);
if (pos == -1) {
return false;
} else {
return inventory.inStock(pos, quantity);
}
}
/**
* Adds a specified quantity of produce items to the inventory.
*
* @param produce the String representation of the produce item
* @param quantity the # of produce items desired to be checked
* @return true if the addition(s) are successful
*/
public static boolean addProduce(Produce produce, int quantity) {
Produce temp = null;
// goes through linked list
for(int i = 1; i < inventory.getLength() + 1; i++) {
// gets current produce
String current = inventory.getEntry(i).getName();
// if produce is the same as the one specified in the parameter, it gets added
if(current.equalsIgnoreCase(produce.getName())) {
temp = inventory.increaseProduce(i, quantity);
try {
saveToFile();
} catch (IOException e) {
System.out.println("Error saving to file");
}
return true;
}
}
if (temp == null) {
inventory.add(produce);
for(int i = 1; i < inventory.getLength() + 1; i++) {
// gets current produce
String current = inventory.getEntry(i).getName();
// if produce is the same as the one specified in the parameter, it gets added
if(current.equalsIgnoreCase(produce.getName())) {
temp = inventory.increaseProduce(i, quantity);
try {
saveToFile();
} catch (IOException e) {
System.out.println("Error saving to file");
}
return true;
}
}
}
return false;
}
/**
* Iterates through the inventory to find the desired produce item.
*
* @param produce The name of the produce in the form of a String.
* @return The desired Produce object. Returns null if not found.
*/
public static Produce getProduce(String produce) {
Produce temp = null;
for (int i = 1; i < inventory.getLength() + 1; i++) {
if (inventory.getEntry(i).getName().equalsIgnoreCase(produce)) {
temp = inventory.getEntry(i);
}
}
return temp;
}
/**
* Determines wether the desired produce item is found in the inventory.
*
* @param produce The name of the produce in the form of a String.
* @return True if the produce item is found, and false otherwise.
*/
public static boolean contains(String produce) {
if(getProduce(produce) == null) {
return false;
}
return inventory.contains(getProduce(produce));
}
public static int getCount(String produce) {
return inventory.getStock(inventory.getPosition(getProduce(produce)));
}
/**
* Stores the contents of the inventory as a String. Contains information regarding the name of the product &
* how many items are in stock.
*
* @return The String representation of the inventory.
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= inventory.getLength(); i++) {
Produce produce = inventory.getEntry(i);
String season = "Out of Season";
if (produce.getInSeason() == true)
season = "In Season";
sb.append(produce.getName()).append(": \n Count: ").append(inventory.getStock(i)).append("\n Status: ").append(season).append("\n");
}
return sb.toString();
}
/**
* Goes through all the Produce nodes in the inventory & prints the name, quantity, and seaosn
* in a new file. Allows information to be stored and reused.
*
* @throws IOException Throws an exception if there is n issue creating a new file.
*/
private static void saveToFile() throws IOException {
PrintWriter writer = new PrintWriter(filePath.toFile());
// goes through all Produce nodes in inventory
for(int i = 1; i <= inventory.getCount(); i++) {
// stores current produce in a var
Produce current = inventory.getEntry(i);
// prints name, quantity, and season
writer.print(current.getName() + ";");
writer.print(getCount(current.getName()) + ";");
writer.print(current.getSeason() + ";");
writer.println();
}
writer.close();
}
}