-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFine.java
More file actions
127 lines (93 loc) · 3.79 KB
/
Copy pathFine.java
File metadata and controls
127 lines (93 loc) · 3.79 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
package LMS.com;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Fine {
private Scanner sc = new Scanner(System.in);
// Insert Fine
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO fine
(fine_id, member_id, amount, status)
VALUES (?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Fine ID: ");
int fineId = sc.nextInt();
System.out.print("Enter Member ID: ");
int memberId = sc.nextInt();
System.out.print("Enter Amount: ");
double amount = sc.nextDouble();
sc.nextLine(); // consume newline
System.out.print("Enter Status (Paid/Unpaid): ");
String status = sc.nextLine();
prestmt.setInt(1, fineId);
prestmt.setInt(2, memberId);
prestmt.setDouble(3, amount);
prestmt.setString(4, status);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Fine Inserted Successfully!");
} else {
System.out.println("❌ Fine Insert Failed!");
}
}
}
// Delete Fine
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM fine WHERE fine_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Fine ID to Delete: ");
int fineId = sc.nextInt();
prestmt.setInt(1, fineId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Fine Deleted Successfully!");
} else {
System.out.println("❌ Fine ID Not Found!");
}
}
}
// Update Fine (Pay / Unpay)
void update(Connection connection) throws SQLException {
String query = "UPDATE fine SET status = ? WHERE fine_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Fine ID to Update: ");
int fineId = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter New Status (Paid/Unpaid): ");
String status = sc.nextLine();
prestmt.setString(1, status);
prestmt.setInt(2, fineId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Fine Updated Successfully!");
} else {
System.out.println("❌ Fine ID Not Found!");
}
}
}
// Get All Fines
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM fine";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 💰 Fine List =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Fine ID: " + rs.getInt("fine_id") +
" | Member ID: " + rs.getInt("member_id") +
" | Amount: ₹" + rs.getDouble("amount") +
" | Status: " + rs.getString("status")
);
}
if (!found) {
System.out.println("No fines found.");
}
}
}
}