-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.java
More file actions
119 lines (101 loc) · 2.23 KB
/
Copy pathStorage.java
File metadata and controls
119 lines (101 loc) · 2.23 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
package Koebman;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class Storage
{
String filename = "StorageData";
ArrayList<Goods> allGoods = new ArrayList<>();
Storage()
{
allGoods = loadStorage();
}
public void addGoods(int ID, int moreGoods)
{
for(Goods g : allGoods)
{
if(g.getID() == ID)
{
g.addGoods(moreGoods);
break;
}
}
}
//Check and remove an item from the goods list, and return true if done.
public boolean checkAndRemove(Goods g, int removeQuantity)
{
boolean done = false;
for(Goods gn : allGoods)
{
if(gn.getID() == g.getID())
{
gn.removeGoods(1);;
done = true;
break;
}
}
return done;
}
public ArrayList<Goods> getList()
{
return allGoods;
}
//Updates the ID for all goods.
private void updateID()
{
int ID = 1;
for(Goods g : allGoods)
{
g.setID(ID);
ID++;
}
}
//Add a new item to the store
private void makeGoods(String Name, int Price, int quantity)
{
updateID();
allGoods.add(new Goods((allGoods.size()+1) ,Name ,Price ,quantity));
}
private ArrayList<Goods> loadStorage()
{
ArrayList<Goods> goodsList = new ArrayList<Goods>();
try {
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
try {
goodsList = (ArrayList<Goods>) in.readObject();
System.out.println ("Succes");
}
catch (Exception e) {
System.out.println("Failed!");
}
in.close();
fileIn.close();
}
catch (IOException i) {
}
return goodsList;
}
private void saveStorage(ArrayList<Goods> storageList)
{
FileOutputStream fileOut = null;
ObjectOutputStream out = null;
try {
File storagefile = new File(filename);
storagefile.createNewFile();
fileOut = new FileOutputStream(filename, false);
out = new ObjectOutputStream(fileOut);
out.writeObject(storageList);
out.close();
fileOut.close();
System.out.println("Succes");
}
catch (Exception e) {
System.out.println("Failed saving");
}
}
}