-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhotelarray.txt
264 lines (215 loc) · 7.44 KB
/
hotelarray.txt
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Hotelproject {
//main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String hotelroomname;
//declaring room number
int roomnumber = 1;
//creating room array
String[] hotelroom = new String[11];
initialise(hotelroom);
while ( roomnumber < 11 )
{
//invoking user menu
System.out.println("\n");
System.out.println("*************************************");
System.out.println("Welcome to hotel Amaya Reef");
System.out.println("*************************************");
System.out.println("Enter your Selection");
System.out.println();
System.out.println("E: Display empty rooms");
System.out.println("D: Delete customer from room");
System.out.println("S: Store data");
System.out.println("L: Load program");
System.out.println("O: Sort customers");
System.out.println("A: Add customer to a room");
System.out.println("V: View all rooms");
System.out.println("X: Exit the program");
//getting user input for method executions
String inputletter = input.next();
//Display - empty rooms
if (inputletter.equalsIgnoreCase("E")){
System.out.println("Displaying Empty rooms :");
for (int x = 1; x < 11; x++ ){
if (hotelroom[x].equals("-"))System.out.println("Room " + x + " is EMPTY");
}
}
//adding customer to room
if (inputletter.equalsIgnoreCase("A")){
System.out.println("Enter room number (1-10) or Q to exit the program:" );
roomnumber = input.nextInt();
System.out.println("Enter a customer name " + roomnumber +" :" ) ;
hotelroomname = input.next().toUpperCase();
hotelroom[roomnumber] = hotelroomname;
}
//removing customer from room
if (inputletter.equalsIgnoreCase("D")){
System.out.println("Enter room number to delete the customer: (1-10)" );
roomnumber = input.nextInt();
hotelroom[roomnumber] = "-" ;
System.out.println("System Updated");
}
//find room
if (inputletter.equalsIgnoreCase("F")){
find(hotelroom);
}
//program exit
if (inputletter.equalsIgnoreCase("X")){
System.out.println("Thank you for usuing our program");
System.exit(0);
}
//view all rooms
if (inputletter.equalsIgnoreCase("V")){
System.out.println("View All Rooms :");
for (int x = 1; x < 11; x++ ){
if(hotelroom[x] != "-"){
System.out.println("Room " + x + " occupied by " + hotelroom[x]);
}else{
System.out.println("Room " + x + " is EMPTY");
}
}
}
//store room data
if (inputletter.equalsIgnoreCase("S")){
storedata(hotelroom);
}
//load room data from file
if (inputletter.equalsIgnoreCase("L")){
loaddata(hotelroom);
}
//arrange rooms in order
if (inputletter.equalsIgnoreCase("O")){
alphabaticalorder(hotelroom);
}
}
}
//method for arrange rooms in order
private static void alphabaticalorder(String hotel[])
{
List<String> orderedrooms = new ArrayList<>();
String temp;
//sorting rooms
for (int i = 0; i < 11; i++)
{
for (int j = i + 1; j < 11; j++)
{
if (hotel[i] != null)
{
if (hotel[i].compareTo(hotel[j]) > 0)
{
temp = hotel[i];
hotel[i] = hotel[j];
hotel[j] = temp;
}
}
}
}
//displaying the result
System.out.println("Names in alphabetical order : ");
for (int i = 0; i < 11 - 1; i++)
{
if (hotel[i] != null)
{
if (!hotel[i].equals("-"))
{
orderedrooms.add(hotel[i]);
}
}
}
orderedrooms.add(hotel[(11) - 1]);
orderedrooms.stream().forEach((name) ->
{
System.out.println(name);
}
);
}
//initialization
private static void initialise( String hotelRef[] ) {
for(int x = 1 ; x < 11 ; x++){
hotelRef[x] = "-";
}
}
//find room method
private static void find(String hotel[])
{
try
{
Scanner input = new Scanner(System.in);
System.out.println("Enter customer name for room:" );
String consumersname;
consumersname = input.next(); //stores name they enter as consumers name
for (int x = 1; x < 11; x++ )
{
//prompting the result
if (hotel[x].equalsIgnoreCase(consumersname))
System.out.println("Room " + x + " is occupied by "+hotel[x]);
}
}
catch (Exception e )
{
//prompt for wrong input
JOptionPane.showMessageDialog(null, "The input is wrong Please enter again.");
}
}
//method for storing in to a file
public static void storedata(String hotelroom[])
{
try {
PrintWriter print = new PrintWriter("hotelReception.txt"); //file location
if(hotelroom.length <= 0){
System.out.println("No records to write!");
}
for (int i=1; i<hotelroom.length ; i++){
print.println(hotelroom[i]); //printing the room data to the file }
}
print.close();
}
//exception handling
catch (Exception e)
{
e.printStackTrace();
System.out.println("File Not Found Exception!");
}
System.out.println("Data Saved Successfully");
}
//method for loading data from file
private static void loaddata(String[] hotelroom) {
System.out.print("Load program data back from the file into the array.");
System.out.println();
File hotelfile = new File("hotelReception.txt");
BufferedReader br = null;
FileReader fr = null;
try{
fr = new FileReader(hotelfile);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(hotelfile));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
hotelprogram.txt
Open with Google Docs
Displaying Mayumi.txt.