-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHotel_Booking.java
187 lines (173 loc) · 5.63 KB
/
Hotel_Booking.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
/**
* Hotel Booking
*
* @author PP-Namias
*
* Licensed under the MIT
*
*/
/**
* @param [bookedList] the booked list.
* @param [scanner] the scanner for user input.
* @param [str] the string to check.
*/
/*
* Features:
* -> Add Room
* -> Remove Room
* -> View Available Rooms
* -> Exit
*
* [Future Features]
* Admin
* -> create Room
* -> display Room
* -> search Room
* -> update Room
* -> delete Room
*
* Public
* -> Add Room
* -> Purchase Room
* -> Remove Room
* -> View Room
* -> Exit
*
* System:
* -> [Login History]
* -> [Product Information]
* -> [Product Order History]
*
*/
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Scanner;
public class Hotel_Booking {
public static void main(String[] args) {
ArrayList<Room> bookedList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String choice;
do {
System.out.println("____________________________");
System.out.println("| Welcome to Hotel Booking |");
System.out.println("----------------------------");
System.out.println("[ 1 ] Add Room");
System.out.println("[ 2 ] Remove Room");
System.out.println("[ 3 ] View Available Rooms");
System.out.println("[ 4 ] Exit");
System.out.print(">>>: ");
choice = scanner.nextLine().trim();
switch (choice) {
case "1":
addRoom(bookedList, scanner);
break;
case "2":
removeRoom(bookedList, scanner);
break;
case "3":
viewAvailableRooms(bookedList);
break;
case "4":
System.out.println("Thank you");
System.exit(0);
}
} while (true);
}
/**
* Adds a room to the bookedList.
* @param bookedList the booked list where the room will be added.
* @param scanner the scanner for user input.
*/
private static void addRoom(ArrayList<Room> bookedList, Scanner scanner) {
int roomNumber;
boolean isAvailable = false;
System.out.println("____________");
System.out.println("| Add Room |");
System.out.println("------------");
System.out.print("Enter Room Number: ");
String temporaryRoomNumber = scanner.nextLine().trim();
if (isNumber(temporaryRoomNumber)) {
roomNumber = Integer.parseInt(temporaryRoomNumber);
System.out.print("Is the room available? (Y | N): ");
String choice = scanner.nextLine().trim();
if (choice.equalsIgnoreCase("Y")) {
isAvailable = true;
}
bookedList.add(new Room(roomNumber, isAvailable)); // add a new room to the list.
}
else System.out.println("Please enter a number");
}
/**
* Prints all the available rooms.
* @param bookedList the booked list.
*/
private static void viewAvailableRooms(ArrayList<Room> bookedList) {
if (bookedList.isEmpty()) System.out.println("No Rooms Available");
else {
System.out.println("___________________");
System.out.println("| Available Rooms |");
System.out.println("-------------------");
for (Room room : bookedList) {
if (room.isAvailable()) System.out.println(room);
}
}
}
/**
* Removes an available room from the booked list.
* @param bookedList the booked list where the available rooms are.
* @param scanner the scanner for user input.
*/
private static void removeRoom(ArrayList<Room> bookedList, Scanner scanner) {
System.out.print("Enter room number to remove: ");
String temporaryRoomNumber = scanner.nextLine().trim();
if (isNumber(temporaryRoomNumber)) {
int roomNumber = Integer.parseInt(temporaryRoomNumber);
// removes the available room from the booked list.
bookedList.remove(new Room(roomNumber, true));
}
}
/**
* Checks if a string is a positive number.
* @param str the string to check.
* @return true if it is a positive number.
*/
private static boolean isNumber(String str) {
return Pattern.compile("^\\d+$").matcher(str).matches();
}
static class Room {
private int roomNumber;
private boolean isAvailable;
public Room(int roomNumber, boolean isAvailable) {
this.roomNumber = roomNumber;
this.isAvailable = isAvailable;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
@Override
public String toString() {
return "Room Number: " + getRoomNumber();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Room room = (Room) o;
return roomNumber == room.roomNumber && isAvailable == room.isAvailable;
}
@Override
public int hashCode() {
return Objects.hash(roomNumber, isAvailable);
}
}
}