-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataStore.java
More file actions
153 lines (131 loc) · 5.29 KB
/
Copy pathDataStore.java
File metadata and controls
153 lines (131 loc) · 5.29 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
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.util.ArrayList;
public class DataStore {
private static final String ORDERS_FILE = "orders.csv";
private static final String CUSTOMERS_FILE = "customers.csv";
private static final String PAYMENTS_FILE = "payments.csv";
// ---------------------------- LOAD METHODS ----------------------------
public OrderManager loadOrders() {
OrderManager om = new OrderManager();
File f = new File(ORDERS_FILE);
if (!f.exists()) return om;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
// Format:
// id,customerID,customerName,phone,email,address,delivery,orderType,status,paymentStatus,amount,method,address
String[] x = line.split(",");
Customer c = new Customer(
x[1], x[2], x[3], x[4], x[5]
);
Order o = new Order(
Integer.parseInt(x[0]),
c,
LocalDate.parse(x[6]),
x[7],
x[11],
x[12],
Double.parseDouble(x[10])
);
o.setStatus(x[8]);
o.setPaymentStatus(x[9]);
om.getOrdersList().add(o);
}
} catch (Exception ignored) {}
return om;
}
public CustomerManager loadCustomers() {
CustomerManager cm = new CustomerManager();
File f = new File(CUSTOMERS_FILE);
if (!f.exists()) return cm;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
String[] x = line.split(",");
Customer c = new Customer(x[0], x[1], x[2], x[3], x[4]);
cm.getCustomersList().add(c);
}
} catch (Exception ignored) {}
return cm;
}
public PaymentManager loadPayments() {
PaymentManager pm = new PaymentManager();
File f = new File(PAYMENTS_FILE);
if (!f.exists()) return pm;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
String[] x = line.split(",");
Payment p = new Payment(
Integer.parseInt(x[0]),
Integer.parseInt(x[1]),
x[2],
Double.parseDouble(x[3])
);
p.setStatus(x[4]);
pm.getPaymentsList().add(p);
}
} catch (Exception ignored) {}
return pm;
}
// ---------------------------- SAVE METHODS ----------------------------
public void saveOrders(OrderManager om) {
backup(ORDERS_FILE);
try (PrintWriter pw = new PrintWriter(new FileWriter(ORDERS_FILE))) {
for (Order o : om.getOrdersList()) {
Customer c = o.customer;
pw.println(
o.getOrderID() + "," +
c.getCustomerID() + "," +
c.getName() + "," +
c.getPhoneNumber() + "," +
c.getEmail() + "," +
c.getAddress() + "," +
o.getDeliveryDate() + "," +
o.getOrderType() + "," +
o.getStatus() + "," +
o.getPaymentStatus() + "," +
o.totalAmount + "," +
o.method + "," +
o.address
);
}
} catch (Exception e) { System.out.println("Error saving orders CSV."); }
}
public void saveCustomers(CustomerManager cm) {
backup(CUSTOMERS_FILE);
try (PrintWriter pw = new PrintWriter(new FileWriter(CUSTOMERS_FILE))) {
for (Customer c : cm.getCustomersList()) {
pw.println(c.getCustomerID() + "," +
c.getName() + "," +
c.getPhoneNumber() + "," +
c.getEmail() + "," +
c.getAddress());
}
} catch (Exception ignored) {}
}
public void savePayments(PaymentManager pm) {
backup(PAYMENTS_FILE);
try (PrintWriter pw = new PrintWriter(new FileWriter(PAYMENTS_FILE))) {
for (Payment p : pm.getPaymentsList()) {
pw.println(p.getPaymentID() + "," +
p.getOrderID() + "," +
p.method + "," +
p.amount + "," +
p.status);
}
} catch (Exception ignored) {}
}
// ---------------------------- BACKUP ----------------------------
private void backup(String filename) {
File f = new File(filename);
if (!f.exists()) return;
try {
Files.copy(f.toPath(), new File(filename + ".bak").toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ignored) {}
}
}