-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHotelReservationSystem.java
More file actions
208 lines (192 loc) · 9.54 KB
/
Copy pathHotelReservationSystem.java
File metadata and controls
208 lines (192 loc) · 9.54 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
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
206
207
208
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Scanner;
import java.sql.Statement;
import java.sql.ResultSet;
public class HotelReservationSystem {
private static final String url = "jdbc:mysql://localhost:3306/hotel_db";
private static final String username = "root";
private static final String password = "1234nitesh@#$";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
try {
Class.forName("mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection connection = DriverManager.getConnection(url, username, password);
while (true) {
System.out.println();
System.out.println("Hotel Management System");
Scanner scanner = new Scanner(System.in);
System.out.println("1. Reserve a room");
System.out.println("2. view Reservation");
System.out.println("3. Get Room Number");
System.out.println("4. Update Reservation");
System.out.println("5. Delete Reservation");
System.out.println("0. Exit");
System.out.println("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
reserveRoom(connection, scanner);
break;
case 2:
viewReservations(connection);
break;
case 3:
getRoomNumber(connection, scanner);
break;
case 4:
updateReservation(connection, scanner);
break;
case 5:
exit();
scanner.close();
return;
default:
System.out.println("Invalid choice . try Again.");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private static void reserveRoom(Connection connection, Scanner scanner) {
try {
System.out.println("Enter guest name: ");
String guestName = scanner.next();
scanner.nextLine();
System.out.println(" Enter room number");
int roomNumber = scanner.nextInt();
System.out.println(" Enter contact number");
String contactNumber = scanner.next();
String sql = "insert into reservation (guest_name,, room_number, contact_number)" + "values ('" + guestName + "', " + roomNumber + " , '" + contactNumber + "')";
try (Statement statement = connection.createStatement()) {
int affectedRows = statement.executeUpdate(sql);
if (affectedRows > 0) {
System.out.println("Reservation successfully");
} else {
System.out.println("Reservation Failed");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void viewReservations(Connection connection) throws SQLException {
String sql = "Select reservation_id, guest_name, room_number, contact_number, reservation_date From reservations";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
System.out.println("Current Reservation");
System.out.println("+----------------+-------------+-------------+----------------+-------------------+");
System.out.println("+ Reservation Id | Guest | Room Number | Contact Number | Reservation Date +");
System.out.println("+----------------+-------------+-------------+----------------+-------------------+");
while (resultSet.next()) {
int reservationId = resultSet.getInt("reservation_id");
String guestName = resultSet.getString("guest_name");
int roomNumber = resultSet.getInt("room_number");
String contactNumber = resultSet.getString("contact_number");
String reservationDate = resultSet.getTimestamp("reservation_date").toString();
System.out.printf("| %-14d | %-11s | %-11d | %-14s | %-17s | \n", reservationId, guestName, roomNumber, contactNumber, reservationDate);
}
System.out.println("+------------------+-------------+------+-----+-------------------+-------------------+");
}
}
private static void getRoomNumber(Connection connection, Scanner scanner) {
try {
System.out.println("Enter reservation ID: ");
int reservationId = scanner.nextInt();
System.out.println("Enter Guest Name: ");
String guestName = scanner.nextLine();
String sql = "SELECT room_number FROM reservations" +
"WHERE reservation_id = " + reservationId + "AND guest_name = '" + guestName + "'";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
if (resultSet.next()) {
int roomNumber = resultSet.getInt("room_number");
System.out.println("Room number for reservation ID" + reservationId + "and Guest " + guestName + "is: " + roomNumber);
} else {
System.out.println("Reservation not Found for the given ID and guest name.");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void updateReservation(Connection connection, Scanner scanner) {
try {
System.out.println("Enter reservation ID to Update: ");
int reservationId = scanner.nextInt();
scanner.nextLine();
if (!reservationExists(connection, reservationId)) {
System.out.println("Reservation not found for the given ID");
return;
}
System.out.println("Enter new guest name");
String newGuestName = scanner.nextLine();
System.out.println("Enter new room number");
int newRoomNumber = scanner.nextInt();
System.out.println("Enter new contact number");
String newContactNumber = scanner.next();
String sql = "UPDATE reservations SET guest_name = '" + newGuestName + "', " + "room_number=" + newRoomNumber + ", " + "contact_number = '" + newContactNumber + "' " + "WHERE reservation _id = " + reservationId;
try (Statement statement = connection.createStatement()) {
int affectedRows = statement.executeUpdate(sql);
if (affectedRows > 0) {
System.out.println("Reservation updated successfully!");
} else {
System.out.println("Reservation updated failed.");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void deleteReservation(Connection connection, Scanner scanner) {
try {
System.out.println("Enter reservation ID to delete: ");
int reservationId = scanner.nextInt();
if (!reservationExists(connection, reservationId)) {
System.out.println("Reservation not found for the found for the given ID");
return;
}
String sql = "Delete from reservation where reservation_id= " + reservationId;
try (Statement statement = connection.createStatement()) {
int affectedRows = statement.executeUpdate(sql);
if (affectedRows > 0) {
System.out.println("Reservation deleted successfully!");
} else {
System.out.println("Reservation deletion failed!");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static boolean reservationExists(Connection connection, int reservationId){
try {
String sql = "SELECT reservation_id FROM reservations WHERE reservations_id = " + reservationId;
try(Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)){
return resultSet.next();
}
}catch (SQLException e){
e.printStackTrace();
return false;
}
}
public static void exit() throws InterruptedException{
System.out.print("exiting System");
int i = 5 ;
while (i!=0){
System.out.print(".");
Thread.sleep(450);
i--;
}
System.out.println();
System.out.println("Thank you for using Hotel Reservation System!!!");
}
}