-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveAccess.java
More file actions
90 lines (77 loc) · 2.01 KB
/
SaveAccess.java
File metadata and controls
90 lines (77 loc) · 2.01 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
package server;
import java.io.IOException;
import java.util.List;
public class SaveAccess implements Accessable {
private ShoppingList shoppingList;
public SaveAccess()
{
shoppingList = new ShoppingList();
}
@Override
public void serve(Connection connection) {
String isNewPage = connection.getValue("isnew");
extractParams(connection);
if(isNewPage.isEmpty() || isNewPage.equals("0")){
updateShoppingList();
}else{
saveNewShoppingList();
}
try {
connection.redirect("index");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void updateShoppingList() {
ShoppingListModel shoplistM = new ShoppingListModel();
shoplistM.loadFromFile();
shoplistM.updateList(shoppingList);
shoplistM.saveToFile();
updateNewTags();
}
private void updateNewTags() {
PlaceModel placeM = new PlaceModel();
placeM.addPlace(shoppingList.getPlace());
placeM.saveToFile();
ItemModel itemM = new ItemModel();
List<Item> items = shoppingList.getAllItems();
for(int i=0; i<items.size(); i++)
{
itemM.addItem(items.get(i));
}
itemM.saveToFile();
}
private void saveNewShoppingList() {
ShoppingListModel shoplistM = new ShoppingListModel();
shoplistM.loadFromFile();
shoplistM.addShoppingList(shoppingList);
shoplistM.saveToFile();
updateNewTags();
}
private void extractParams(Connection connection) {
shoppingList = new ShoppingList();
Place place = new Place();
place.setName(connection.getValue("shop"));
shoppingList.setPlace(place);
for(int i=0; ;i++){
String itemName = connection.getValue("item"+i);
String itemQuan = connection.getValue("quentity"+i);
String itemOpt = connection.getValue("optional"+i);
if(itemName.isEmpty()){
if(i > 20)
break;
else
continue;
}
Item item = new Item();
item.setName(itemName);
item.setQuantity(itemQuan);
if(itemOpt.equals("on"))
item.setOptional("1");
else
item.setOptional("0");
shoppingList.addItem(item);
}
}
}