Preface
Program for hotels: hotel management system, simulation of booking, check out, printing all room status and other functions.
1. System User: Hotel Front Desk
2. The hotel uses a two-dimensional array to simulate.
3. Every room in the hotel is a Java object: Room
4. Each room has: number, type, room status
5. Functions that the system should provide to the outside world:
Reservations: User enters room number, reservations
Check out: User enters room number, check out
View all room states: Users enter an instruction to view all room states
thinking
Step 1 Create a room
Since each room is an object (the object is new) and the properties of the room are mentioned, you can create a Room class first.
All attributes in the Room class are privateized and get and set methods are provided externally, then two construction methods are provided, one with no arguments and one with arguments, and the toString and equals methods of the Re Object (which can be rewritten to look what you want, or of course not)
In the subclass I override the toString method to print the room state, where a trinomial operator is used:
b ? x : y
What's inside? It's a format requirement. If the value of b is true, it's result x, otherwise it's result y.
And the equals method I rewrite is the same room if both rooms have the same number.
The Room.class section code is as follows:
//It is best to override the toString and equals methods, otherwise the call is inside the Object public class Room{ private int roomnumber; private String type; private boolean flag; public Room() { } public Room(int roomnumber, String type, boolean flag) { this.roomnumber = roomnumber; this.type = type; this.flag = flag; } public int getRoomnumber() { return roomnumber; } public void setRoomnumber(int roomnumber) { this.roomnumber = roomnumber; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } //Override equals method public boolean equals(Object obj){ } //Override the toString method, which can be converted to output room information public String toString(){ //ternary operator return "["+this.getRoomnumber()+","+this.getType()+","+(this.isFlag()?"Occupy":"free")+"]"; } }
Build Hotel
Now that the rooms are created, a hotel is made up of multiple rooms. Use it to create a Hotel class in which to build the hotel.
There is only one private Room type array in the hotel. Due to the limited level now, every room in the hotel can only be written to death by the parameterless construction method. And all the methods are implemented in the hotel.
Part of the code implementation:
public class Hotel { private Room[][] rooms; //Parametric construction public Hotel(){ } //Print all room states i.e. traverse a two-dimensional array of rooms public void printRooms(){ } public void bookRoom(int roomnumber){ } public void reRoom(int roomnumber){ } }
Step 3, Write a test class
Test whether the functions required by the project are implemented
Here the while loop allows you to enter all the time and then proceed to the next step by determining the number of inputs.
tip: You cannot write the following code outside of the loop, otherwise it will continue to loop and cause the computer to crash.
System.out.println("Please enter the number:"); int i = s.nextInt();
**
Part of the code implementation for HotelMgSystem.class:
public class HotelMgSystem { public static void main(String[] args) { System.out.println("input[1]Represents viewing a list of rooms,[2]Represents a reservation.[3]Indicates check out,[0]Indicates exit from the system"); while (true){ System.out.println("Please enter the number:"); int i = s.nextInt(); if(i==1){ }else if (i==2){ System.out.println("Please enter room number:"); }else if (i==3){ System.out.println("Please enter room number:"); }else if (i==0){ System.out.println("Welcome next time!"); return; } else { System.out.println("Wrong input number, please re-enter:"); } } } }
2. Result Display
Print all room status function:
Initial state:
When a room is booked it will show occupancy and can no longer be booked:
Reservation function
If the room is idle:
Room occupied:
Check out
Exit System
summary
This is a small project and improves many small details (e.g. output of all booked or idle rooms, incorrect input, etc.) which are now possible with your capabilities)There is also the use of new knowledge learned in the future, such as databases to store booked rooms, other changes, instead of running the system every time as it is now.
Finally, Java is an object-oriented programming language that learns to write code with object-oriented thinking (it's a crying process).