-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachineDaoInMemImpl.java
More file actions
138 lines (110 loc) · 4.15 KB
/
VendingMachineDaoInMemImpl.java
File metadata and controls
138 lines (110 loc) · 4.15 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package VendingMachinemvc.dao;
import VendingMachinemvc.model.Item;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author Kelsey
*/
public class VendingMachineDaoInMemImpl implements VendingMachineDao {
//Item itemList = new Item();
private Map<Long, Item> itemMap = new HashMap<>();
public static final String VENDINGMACHINE_FILE = "VendingMachineItems.txt";
public static final String DELIMITER = "::";
List<Item> items = new ArrayList<>();
private void loadVendingMachine() throws VendingMachineException {
items.clear();
Scanner scanner;
try {
// Create Scanner for reading the file
scanner = new Scanner(
new BufferedReader(
new FileReader(getClass().getClassLoader().getResource(VENDINGMACHINE_FILE).getFile())));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
throw new VendingMachineException(
"-_- Could not load roster data into memory.", e);
}
String currentLine;
String[] currentTokens;
while (scanner.hasNextLine()) {
// get the next line in the file
currentLine = scanner.nextLine();
// break up the line into tokens
currentTokens = currentLine.split(DELIMITER);
if (currentTokens.length < 2) {
continue;
}
Item currentItem = new Item();
int itemId = Integer.parseInt(currentTokens[0]);
currentItem.setItemId(itemId);
currentItem.setItemName(currentTokens[1]);
currentItem.setItemPrice(new BigDecimal(currentTokens[2]));
int numAvailable = Integer.parseInt(currentTokens[3]);
currentItem.setNumOfItems(numAvailable);
itemMap.put(currentItem.getItemId(), currentItem);
items.add(currentItem);
}
// close scanner
scanner.close();
}
private void writeInventoryFile() throws VendingMachineException {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(getClass().getClassLoader().getResource(VENDINGMACHINE_FILE).getFile()));
} catch (IOException e) {
}
for (Item currentItem : items) {
// write the item object to the file
out.println(currentItem.getItemId() + DELIMITER
+ currentItem.getItemName() + DELIMITER
+ currentItem.getItemPrice() + DELIMITER
+ currentItem.getNumOfItems()
);
// force PrintWriter to write line to the file
out.flush();
}
// Clean up
out.close();
}
@Override
public List<Item> getAllItems() throws VendingMachineException {
loadVendingMachine();
return items;
}
@Override
public Item getItemById(int itemId) throws VendingMachineException {
loadVendingMachine();
return itemMap.get((long) itemId);
}
@Override
public void updateItem(String itemName) {
try {
int sc=Integer.parseInt(itemName);
Item item = getItemById(sc);
item.setNumOfItems(item.getNumOfItems() - 1);
items.set(items.indexOf(item), item);
try {
writeInventoryFile();
} catch (VendingMachineException ex) {
System.out.println("Error");
}
} catch (VendingMachineException ex) {
}
}
}