Repository navigation
Expand file tree
/
Copy pathUser.java
More file actions
122 lines (99 loc) · 3.22 KB
/
Copy pathUser.java
File metadata and controls
122 lines (99 loc) · 3.22 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
package com.kushal.LibraryManagement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class User {
private int userId;
private String name;
private String email;
private String phone;
static Scanner scanner = new Scanner(System.in);
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
// Method to add a new user
static void addUser(){
User u = new User();
Scanner sc = new Scanner(System.in);
System.out.println("--Add a new User--");
System.out.println("Enter Book Details");
System.out.print("Enter Name: ");
u.setName(sc.nextLine());
System.out.print("Enter Email: ");
u.setEmail(sc.nextLine());
System.out.print("Enter Phone: ");
u.setPhone(sc.nextLine());
try {
Database.addUser(u);
} catch (SQLException e){
e.printStackTrace();
}
}
// Method to display all users
static void displayAllUsers() throws SQLException {
ResultSet rs = Database.getUsers();
String format = "| %-8s | %-25s | %-20s | %-20s |\n";
String separator = "+----------+---------------------------+----------------------+----------------------+\n";
System.out.println(separator);
System.out.printf(format, "User ID", "Name", "Email", "Phone");
System.out.println(separator);
assert rs != null;
if (rs.next()) {
while (rs.next()) {
System.out.printf(format,
rs.getInt("user_id"),
rs.getString("name"),
rs.getString("email"),
rs.getString("phone")
);
}
} else {
System.out.println("| No users found in the database. |");
}
System.out.println(separator);
}
// Method to delete a user
static void deleteUser() throws SQLException {
System.out.print("Enter User ID to delete: ");
int userId = scanner.nextInt();
int result = Database.deleteUser(userId);
if (result > 0) {
System.out.println("User deleted successfully!");
} else {
System.out.println("User ID not found.");
}
}
static void issueBook(int loggedInUserId){
System.out.println("Enter Book ID You want to issue: ");
Scanner sc = new Scanner(System.in);
int book_id=sc.nextInt();
Database.issueBook(loggedInUserId,book_id);
}
static void returnBook(int loggedInUserId){
System.out.println("Enter Book ID You want to return: ");
Scanner sc = new Scanner(System.in);
int book_id=sc.nextInt();
Database.returnBook(loggedInUserId,book_id);
}
}