I have this program where I have use arrays to book room reservations in hotel, however I'm completely stuck on what I need to do if I wanted to cancel the rooms already and returning them to the array to be re booked.
Here is part of my code.
public int reserveRoomType( int type ) {
int first, last;
switch( type ) {
case 1:
first = 0;
last = 4;
break;
case 2:
first = 5;
last = 9;
break;
case 3:
first = 10;
last = 10;
break;
case 4:
first = 11;
last = 11;
break;
default:
StdOut.println();
StdOut.println( "Invalid room type: " + type );
return -1;
}
for( int i = first; i <= last; i++ ) {
if ( !reservations[i] ) {
// A room of this type is available, tell the user!
StdOut.println();
StdOut.printf( "Room %d reserved for you!\n", 101 + i );
reservations[i] = true;
return -1;
}
}
// All rooms of this type are occupied
StdOut.println();
StdOut.println( "Sorry, but all rooms of this type are occupied!" );
do {
StdOut.println();
StdOut.printf( "Do you accept an alternative? (yes/no) " );
String response = StdIn.readString();
if ( "no".equalsIgnoreCase( response ) ) {
StdOut.println();
StdOut.println( "Reservations for other days will be available soon." );
return -1;
} else if ( "yes".equalsIgnoreCase( response ) ) {
StdOut.println();
return type;
}
StdOut.println();
StdOut.println( "Please type 'yes' or 'no'!" );
} while( true );
}
/**
* Print the reservations.
* This method prints a line for each room type and print the number of
* occupied rooms and the number of rooms of that type.
* Also it prints a line about the totals at the end.
*/
public void printReservations() {
StdOut.println();
StdOut.println( "The following rooms are occupied:");
StdOut.println();
int total = 0;
// Print info for all 4 room types
int count = 0;
for( int i = 0; i <= 4; i++ ) {
if ( reservations[i] )
{ count++; }
}
StdOut.printf( " 1 king bed and non-smoking room: %d of 5 occupied\n", count );
total += count;
count = 0;
for( int i = 5; i <= 9; i++ ) {
if ( reservations[i] )
{ count++; }
}
StdOut.printf( " 2 double beds and non-smoking room: %d of 5 occupied\n", count );
total += count;
count = ( reservations[10] ? 1 : 0 );
StdOut.printf( " 1 king bed and smoking room: %d of 1 occupied\n", count );
total += count;
count = ( reservations[11] ? 1 : 0 );
StdOut.printf( " 2 double beds and smoking room: %d of 1 occupied\n", count );
total += count;
StdOut.println();
StdOut.printf( " %d of 12 rooms occupied\n", total );
StdOut.println();
}
public void cancelreservations(){
StdOut.println();
StdOut.println( "Please enter the Room Number you want to cancel the reservation:");
StdOut.println();
int choice = StdIn.readInt();
for( int i = choice; i <= choice; i++ ) {
if (reservations[choice] = reservations[i])
reservations[i] = false;
}
}