Java language realizes simple hotel front desk management small function of example code

  • 2021-08-31 08:04:11
  • OfStack

The author is a new Macey who has just hit the road. I hope you can correct any problems!

The following are the topics:
Write a program for a hotel: hotel management system, simulate booking, check-out, print all room status and other functions.
1. The user of this system is: hotel front desk.
2. The hotel uses a 2-dimensional array to simulate. "Room [] [] rooms;"
3. Every room in the hotel should be an java object: Room
4. Every room Room should have: room number, room type and whether the room is free.
5. Functions that the system should provide externally:
You can book a room: the user enters the room number and makes a reservation.
Can check out: The user enters the room number and checks out.
You can view the status of all rooms: The user should be able to view the status of all rooms by entering a command.

The following is the source code of this function:

Room Class (Hotel Room Class)


package com.kukudeyu.hotelsystem;

public class Room {
 private int id;  // Room number 
 private String type;  // Type of room 
 private boolean status;  // Room status: true Indicates idle, false Indicate occupation 

 public Room() {
 }

 public Room(int id, String type, boolean status) {
 this.id = id;
 this.type = type;
 this.status = status;
 }

 public int getId() {
 return id;
 }

 public void setId(int id) {
 this.id = id;
 }

 public String getType() {
 return type;
 }

 public void setType(String type) {
 this.type = type;
 }

 public boolean getStatus() {
 return status;
 }

 public void setStatus(boolean status) {
 this.status = status;
 }

 /*
 *  Rewrite toString Method 
 *  Print out room details, including room number, type and status 
 * */
 @Override
 public String toString() {
 return "[" + this.id + " , " + this.type + " , " + (this.status ? " Idle ":" Occupation " ) + "]";
 }

 //  By convention, rewrite equals Method, the function is to judge whether two rooms are 1 Rooms 
 @Override
 public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || !(o instanceof Room)) return false;
 Room room = (Room)o;
 if(this.id == room.id){
  return true;
 }
 return false;
 }
}

Hotel Class (Hotel Class)


package com.kukudeyu.hotelsystem;

public class Hotel {
 private Room[][] rooms; // Utilization 2 Create an Array of Hotel Rooms from Dimensional Array 

 /*
  Using construction method to arrange hotel rooms 
  Using array traversal, create hotel room objects and put them into the hotel room array 
  Among them, 
 1 The floor is a single room, 2 The floor is a double room, 3 The first floor is the presidential suite 
 */
 public Hotel() {
 rooms = new Room[3][10];

 for (int i = 0; i < rooms.length; i++) {
  for (int j = 0; j < rooms[i].length; j++) {
  if (i == 0) {
   rooms[i][j] = new Room((i + 1) * 100 + j + 1, " Single room ", true);
  } else if (i == 1) {
   rooms[i][j] = new Room((i + 1) * 100 + j + 1, " Double room ", true);
  } else if (i == 2) {
   rooms[i][j] = new Room((i + 1) * 100 + j + 1, " Presidential Suite ", true);
  }
  }
 }
 }

 /*
 print Method provides the function of viewing a list of rooms and can query the current status of all rooms 
  Use a loop to call all room objects Room Class toString Method to query the room status 
 */
 public void print(){
 for(int i = 0 ; i< rooms.length ; i++){
  for(int j = 0 ; j<rooms[i].length ; j++){
  System.out.print(rooms[i][j].toString()); // Call Room Class overridden toString Method to view the status of a single room 
  }
  System.out.println();
 }
 }

 /*
  Provide booking Method to modify the state of the room 
  Instant reservation 
  Call getStatus Method to query room status 
  If is true If it is free, it will prompt the reservation to be successful 
  If is false For occupation, prompt room occupation 
 */
 public void booking(int id){
 if(rooms[id / 100 -1][id % 100 -1].getStatus()){
  rooms[id / 100 - 1][id % 100 -1].setStatus(false);  // Call setStatus Method to modify the room status 
  System.out.println(" Successful reservation! ");
 }else{
  System.out.println(" The room is occupied, please change it to another one 1 A room! ");
 }
 }

 /*
  Provide cancelBooking Method to modify the state of the room 
  Check out immediately 
  Right getStatus The return value of the method uses logical non to query the room status 
  If is false To occupy, 
 */
 public void cancelBooking(int id){
 if( rooms[id / 100 -1][id % 100 -1].getStatus() ){
  System.out.println(" Room free, no need to check out! ");
 }else{
  rooms[id / 100 - 1][id % 100 -1].setStatus(true);
  System.out.println(" Check out successfully! ");
 }
 }
}

HotelSystem Class (Hotel Systems Class)


package com.kukudeyu.hotelsystem;

import java.util.Scanner;

public class HotelSystem {
 public static void main(String[] args) {
 Hotel hotel = new Hotel();  // Create 1 Hotel objects 

 System.out.println("----------------------------------------------------------------------------");
 System.out.println(" Welcome to the hotel management system, please read the following instructions carefully! ");
 System.out.println(" Function number: " 1 "Check the list of rooms." 2 "Reservation." 3 "Check out." 4 "Exiting the hotel management system. ");
 System.out.println("----------------------------------------------------------------------------");
 Scanner s = new Scanner(System.in);

 while(true){
  System.out.print(" Please enter the function number: ");
  int i = s.nextInt();
  if(i == 1){
  hotel.print();
  }else if(i == 2 ){
  System.out.print(" Please enter the room number to book: ");
  int roomid = s.nextInt();
  hotel.booking(roomid); // Call booking Method to make a reservation 
  }else if(i == 3){
  System.out.print(" Please enter the room number to unsubscribe: ");
  int roomid = s.nextInt();
  hotel.cancelBooking(roomid); // Call cancelBooking Method to check out 
  }else if(i == 4){
  return;
  }
 }
 }
}

Related articles: