java room management project, suitable for beginners

java room management project
This room management project is suitable for java beginners. Although the function is not much, but the content is very complete!
Like this article can pay attention to me, I will continue to update, your attention is my update power! If you need more java learning materials, you can also private me!

There are 5 floors in total, 10 rooms on each floor, marked with numbers 101-509;
It has four simple functions: check-in, check-out, search and exit;
public class Hotel {
static final int floor = 5;
static final int order = 10;
private static int countFloor;
private static int countOrder;
private static String[][] rooms = new String[floor][order];

//The main function represents the basic functions of the hotel, check-in, check-out, query and others;
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String temp = null;
    while(true){
        //Remind user to select input
        System.out.println("Please select input  in out search quit : ");
        temp = scan.next();
        int room = 0;
        if("in".equals(temp)){
            while(true){
                System.out.println("Please enter room number:");
                room = scan.nextInt();
                if(isRightRoom(room,1)){
                    break;
                }
            }
            System.out.println("Please enter a name:");
            String name = scan.next();
            if(in(room,name)){
                System.out.println("Congratulations on your successful stay");
                System.out.println(room + " : "+name);
            }
        }else if("out".equals(temp)){
            while(true){
                System.out.println("Please enter room number:");
                room = scan.nextInt();
                if(isRightRoom(room,0)){
                    break;
                }
            }
            if(out(room)){
                System.out.println("Check out successful, welcome to come next time");
            }
        }else if("search".equals(temp)){
            System.out.println("Please enter the room number to query(-1 Express all)");
            room = scan.nextInt();
            search(room);
        }else if("quit".equals(temp)){
            break;
        }else{
            System.out.println("Your input is wrong, please input again!");
        }
    }
}

//flag:1 check that the room number is entered correctly and no one is staying in 0 only check that the room number is entered correctly
private static boolean isRightRoom(int room,int flag) {
    //Check whether the room number is entered incorrectly
    countFloor = room / 100 - 1;
    countOrder = room % 100 - 1;
    if(countFloor < 0 || countFloor >= floor || countOrder < 0 || countOrder >= order ){
        System.out.println("Wrong room number entered");
        return false;
    }
    if(flag == 1){
        if(rooms[countFloor][countOrder] != null){
            System.out.println("The room has been checked in");
            return false;
        }
    }
    return true;
}

//in(room,name) countFloor: calculated floor countOrder: calculated room order 
private static boolean in(int room,String name){
    rooms[countFloor][countOrder] = name;
    return true;
}

//out(room),
private static boolean out(int room){
    //Check whether the room number is occupied
    if(rooms[countFloor][countOrder] == null){
        System.out.println("The room has not been checked in, check out is wrong");
        return false;
    }
    rooms[countFloor][countOrder] = null;
    return true;
}

//Hotel search function, search(room)
private static void search(int room){
    if(room == -1){
        int roomNum = 0;
        for(int i=0;i<floor;i++){
            for(int j=0;j<order;j++){
                roomNum = (i + 1) * 100 + j + 1;
                System.out.println(roomNum + "->" + 
                        (rooms[i][j] == null ? "empty" : rooms[i][j]));
            }
        }
    }else{
        //Check whether the room number is correct
        if(!isRightRoom(room,0)){
            System.out.println("Sorry, room input error!");               
        }else{
            System.out.println(rooms[countFloor][countOrder]);
        }
    }
}}

Keywords: Java

Added by kenrbnsn on Wed, 08 Jan 2020 16:14:28 +0200