-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentDAO.java
More file actions
114 lines (105 loc) · 4.67 KB
/
Copy pathPaymentDAO.java
File metadata and controls
114 lines (105 loc) · 4.67 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
package model;
import jakarta.servlet.ServletContext;
import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class PaymentDAO {
private final String filePath;
public PaymentDAO(ServletContext context) {
this.filePath = context.getRealPath("/WEB-INF/paymentData/payments.txt");
File file = new File(filePath);
file.getParentFile().mkdirs();
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("Failed to create payments.txt", e);
}
}
}
public void savePayment(Payment payment) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
String extraData = payment instanceof OnlinePayment
? ((OnlinePayment) payment).getCardNumber()
: ((CashPayment) payment).getReceivedBy();
String line = String.format("%s|%.2f|%s|%s|%s|%s|%s|%s|%s",
payment.getTransactionId(),
payment.getAmount(),
payment.getDate(),
payment.getPaymentDate(),
payment.getBookingId(),
payment.getStatus(),
payment.getUsername(),
payment.getPaymentType(),
extraData);
writer.write(line);
writer.newLine();
}
}
public List<Payment> getAllPayments() throws IOException {
List<Payment> payments = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
String[] data = line.split("\\|");
if (data.length < 9) continue;
String transactionId = data[0];
double amount = Double.parseDouble(data[1]);
LocalDateTime date = LocalDateTime.parse(data[2]);
String paymentDate = data[3];
String bookingId = data[4];
String status = data[5];
String username = data[6];
String paymentType = data[7];
String extraData = data[8];
Payment payment = paymentType.equals("Online")
? new OnlinePayment(transactionId, amount, bookingId, username, extraData, "Unknown")
: new CashPayment(transactionId, amount, bookingId, username, extraData);
payment.setStatus(status);
payments.add(payment);
}
}
return payments;
}
public List<Payment> getPaymentsByUsername(String username) throws IOException {
return getAllPayments().stream()
.filter(p -> username.equalsIgnoreCase(p.getUsername()))
.toList();
}
public Payment getPaymentById(String transactionId) throws IOException {
return getAllPayments().stream()
.filter(p -> p.getTransactionId().equals(transactionId))
.findFirst()
.orElse(null);
}
public List<Payment> getPaymentsByBookingId(String bookingId) throws IOException {
return getAllPayments().stream()
.filter(p -> p.getBookingId().equals(bookingId))
.toList();
}
public void deletePayment(String transactionId) throws IOException {
List<Payment> payments = getAllPayments();
payments.removeIf(p -> p.getTransactionId().equals(transactionId));
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Payment payment : payments) {
String extraData = payment instanceof OnlinePayment
? ((OnlinePayment) payment).getCardNumber()
: ((CashPayment) payment).getReceivedBy();
String line = String.format("%s|%.2f|%s|%s|%s|%s|%s|%s|%s",
payment.getTransactionId(),
payment.getAmount(),
payment.getDate(),
payment.getPaymentDate(),
payment.getBookingId(),
payment.getStatus(),
payment.getUsername(),
payment.getPaymentType(),
extraData);
writer.write(line);
writer.newLine();
}
}
}
}