-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPayment.java
205 lines (179 loc) · 7.48 KB
/
Payment.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package net.javaguides.swing;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.*;
import java.util.concurrent.TimeUnit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Payment extends JFrame implements ActionListener {
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/swing_demo";
private static final String USERNAME = "root";
private static final String PASSWORD = "Aben5099";
private JTextField bookIdTextField;
public Payment() {
setTitle("Payment");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLayout(null);
JLabel bookIdLabel = new JLabel("Book ID");
bookIdLabel.setBounds(30, 30, 100, 20);
add(bookIdLabel);
bookIdTextField = new JTextField();
bookIdTextField.setBounds(140, 30, 200, 20);
add(bookIdTextField);
JButton payButton = new JButton("Pay");
payButton.setBounds(140, 70, 100, 30);
payButton.addActionListener(this);
add(payButton);
JButton undoButton = new JButton("Undo");
undoButton.setBounds(250, 70, 100, 30);
undoButton.addActionListener(this);
add(undoButton);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Pay")) {
addPaymentDetails();
} else if (e.getActionCommand().equals("Undo")) {
undoPaymentDetails();
}
}
private void addPaymentDetails() {
int bookId = Integer.parseInt(bookIdTextField.getText());
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
// Retrieve the existing payment details for the given book ID
// Retrieve the existing payment details for the given book ID
String selectQuery = "SELECT * FROM fees_management WHERE book_id = ?";
preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, bookId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
String bookTitle = resultSet.getString("book_title");
double fees = resultSet.getDouble("fees");
String timeBorrowed = resultSet.getString("time_borrowed");
String timeReturned = resultSet.getString("time_returned");
// Calculate the fine if the book is returned late
double fine = 0.0;
LocalDateTime borrowedTime = LocalDateTime.parse(timeBorrowed, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime returnedTime = LocalDateTime.parse(timeReturned, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long hoursDifference = Duration.between(returnedTime, borrowedTime).toHours();
if (hoursDifference > 24) {
double lateFee = fees * 0.4; // 40% fine
fine = lateFee;
fees += lateFee;
}
// Update the payment details
String updateQuery = "UPDATE fees_management SET name = ?, fees = ?, time_returned = CURRENT_TIMESTAMP WHERE book_id = ?";
preparedStatement = connection.prepareStatement(updateQuery);
preparedStatement.setString(1, "library");
preparedStatement.setDouble(2, fees);
preparedStatement.setInt(3, bookId);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "Payment details updated successfully\n\n" +
"Book Title: " + bookTitle + "\n" +
"Fee Paid: $" + fees + "\n" +
"Fine: $" + fine);
} else {
JOptionPane.showMessageDialog(this, "Payment details not found for the given book ID");
}
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error updating payment details");
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
private void undoPaymentDetails() {
int bookId = Integer.parseInt(bookIdTextField.getText());
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
// Update fees and time to default values, and change name to "library"
String updateQuery = "UPDATE fees_management SET name = ?, fees = 0, time_returned = CURRENT_TIMESTAMP WHERE book_id = ?";
preparedStatement = connection.prepareStatement(updateQuery);
preparedStatement.setString(1, "library");
preparedStatement.setInt(2, bookId);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "Payment details undone successfully");
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error undoing payment details");
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Payment();
}
}