-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
71 lines (59 loc) · 1.3 KB
/
Inventory.java
File metadata and controls
71 lines (59 loc) · 1.3 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
import java.util.ArrayList;
import java.util.*;
import java.io.*;
public class Inventory
{
private static Inventory INSTANCE;
public ArrayList<String> names;
public ArrayList<Float> prices;
public ArrayList<Integer> amounts;
private Inventory()
{
names = new ArrayList<String>();
prices = new ArrayList<Float>();
amounts = new ArrayList<Integer>();
String item;
try{
File input = new File("inventoryReport.txt");
Scanner in = new Scanner(input);
while(in.hasNextLine()){
item = in.nextLine();
String[] itemInfo = item.split("/");
names.add(itemInfo[0]);
amounts.add(Integer.parseInt(itemInfo[1]));
prices.add(Float.parseFloat(itemInfo[2]));
}
in.close();
}
catch (IOException e){
}
}
public static Inventory getInstance()
{
if (INSTANCE == null)
INSTANCE = new Inventory();
return INSTANCE;
}
public String getItemName(int index)
{
return names.get(index);
}
public float getItemPrice(int index)
{
return prices.get(index);
}
public int getItemAmount(int index)
{
return amounts.get(index);
}
public String toString()
{
int num = 1;
String buff = "-------Inventory-------\n";
for (int i = 0; i < names.size(); i++)
{
buff += String.format("%d: %-10s%10.2f%n", num++, names.get(i), prices.get(i));
}
return buff;
}
}