-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentServer.java
More file actions
145 lines (117 loc) · 5 KB
/
Copy pathPaymentServer.java
File metadata and controls
145 lines (117 loc) · 5 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
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.math.BigDecimal;
import java.util.List;
public class PaymentServer {
private static final int PORT = 8888;
private StudentDAO studentDAO;
private ProductDAO productDAO;
private TransactionService transactionService;
public PaymentServer() {
this.studentDAO = new StudentDAO();
this.productDAO = new ProductDAO();
this.transactionService = new TransactionService();
}
public void start() {
System.out.println("=== Campus Payment Server Starting ===");
System.out.println("Listening on port " + PORT);
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket.getInetAddress());
new Thread(new ClientHandler(clientSocket)).start();
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
e.printStackTrace();
}
}
class ClientHandler implements Runnable {
private Socket socket;
private ObjectInputStream in;
private ObjectOutputStream out;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
while (true) {
Request request = (Request) in.readObject();
Response response = handleRequest(request);
out.writeObject(response);
out.flush();
}
} catch (EOFException e) {
System.out.println("Client disconnected");
} catch (Exception e) {
System.err.println("Error handling client: " + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Response handleRequest(Request request) {
System.out.println("Processing request: " + request.getType());
switch (request.getType()) {
case AUTHENTICATE:
return handleAuthentication(request);
case GET_BALANCE:
return handleGetBalance(request);
case GET_PRODUCTS:
return handleGetProducts(request);
case PROCESS_PURCHASE:
return handlePurchase(request);
case HEARTBEAT:
return new Response(true, "Server alive");
default:
return new Response(false, "Unknown request type");
}
}
private Response handleAuthentication(Request request) {
String studentId = (String) request.getData("studentId");
String pin = (String) request.getData("pin");
StudentDTO student = studentDAO.authenticateStudent(studentId, pin);
if (student != null) {
studentDAO.addWeeklyAllowance(studentId);
BigDecimal currentBalance = studentDAO.getBalance(studentId);
student = new StudentDTO(student.getStudentId(), student.getName(), currentBalance);
Response response = new Response(true, "Authentication successful");
response.setData("student", student);
return response;
} else {
return new Response(false, "Invalid credentials");
}
}
private Response handleGetBalance(Request request) {
String studentId = (String) request.getData("studentId");
BigDecimal balance = studentDAO.getBalance(studentId);
Response response = new Response(true, "Balance retrieved");
response.setData("balance", balance);
return response;
}
private Response handleGetProducts(Request request) {
String machineId = (String) request.getData("machineId");
List<ProductDTO> products = productDAO.getAvailableProducts(machineId);
Response response = new Response(true, "Products retrieved");
response.setData("products", products);
return response;
}
private Response handlePurchase(Request request) {
String studentId = (String) request.getData("studentId");
String machineId = (String) request.getData("machineId");
String productId = (String) request.getData("productId");
return transactionService.processPurchase(studentId, machineId, productId);
}
}
public static void main(String[] args) {
PaymentServer server = new PaymentServer();
server.start();
}
}