-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotelReservationSystem.java
More file actions
135 lines (122 loc) · 4.13 KB
/
Copy pathHotelReservationSystem.java
File metadata and controls
135 lines (122 loc) · 4.13 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
import java.util.*;
class Room {
int roomNumber;
String category;
double price;
boolean isAvailable;
Room(int number, String type, double cost) {
roomNumber = number;
category = type;
price = cost;
isAvailable = true;
}
}
class Reservation {
int id;
Room room;
String guest;
int days;
double amount;
boolean paid;
Reservation(int rid, Room r, String g, int n) {
id = rid;
room = r;
guest = g;
days = n;
amount = r.price * n;
paid = false;
r.isAvailable = false;
}
void pay() {
paid = true;
}
}
class Hotel {
ArrayList<Room> rooms = new ArrayList<>();
ArrayList<Reservation> reservations = new ArrayList<>();
int resCounter = 1;
void addRoom(Room r) {
rooms.add(r);
}
Room searchRoom(String type) {
for (Room r : rooms) {
if (r.isAvailable && r.category.equalsIgnoreCase(type)) {
return r;
}
}
return null;
}
Reservation bookRoom(String name, String type, int days) {
Room r = searchRoom(type);
if (r != null) {
Reservation res = new Reservation(resCounter++, r, name, days);
reservations.add(res);
return res;
}
return null;
}
Reservation getReservation(int id) {
for (Reservation r : reservations) {
if (r.id == id) {
return r;
}
}
return null;
}
}
public class HotelReservationSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Hotel hotel = new Hotel();
hotel.addRoom(new Room(101, "Single", 100));
hotel.addRoom(new Room(102, "Double", 150));
hotel.addRoom(new Room(103, "Luxury", 250));
while (true) {
System.out.println("\n1. Search Room\n2. Book Room\n3. Pay for Booking\n4. Exit");
int choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1:
System.out.print("Enter room type: ");
String type = input.nextLine();
Room room = hotel.searchRoom(type);
if (room != null) {
System.out.println("Room available: " + room.roomNumber);
} else {
System.out.println("No rooms available.");
}
break;
case 2:
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter room type: ");
type = input.nextLine();
System.out.print("Enter number of days: ");
int days = input.nextInt();
Reservation res = hotel.bookRoom(name, type, days);
if (res != null) {
System.out.println("Booking successful! ID: " + res.id + " | Amount: $" + res.amount);
} else {
System.out.println("No rooms available.");
}
break;
case 3:
System.out.print("Enter booking ID: ");
int resId = input.nextInt();
Reservation reservation = hotel.getReservation(resId);
if (reservation != null && !reservation.paid) {
reservation.pay();
System.out.println("Payment successful for Booking ID: " + resId);
} else {
System.out.println("Invalid booking ID or already paid.");
}
break;
case 4:
input.close();
return;
default:
System.out.println("Invalid choice, try again.");
}
}
}
}