Java|学习|酒店预订程序

客户端类

package homework.dayfourteenth;

import java.util.Scanner;

public class Client {
	public static void main(String[] args){
		//实例化对象
		Room[][] rooms = new Room[5][10];
		Hotel hotel = new Hotel(rooms);
		
		System.out.println("欢迎来到酒店预退订客户端,请输入你的需求");
		//循环操作
		while(true){
			menu();
			Scanner sc = new Scanner(System.in);
			String select = sc.nextLine();
			if(select.equals("打印")){
				hotel.printAllRoom();
			}else if(select.charAt(0) == '预'){
				String sNo = select.substring(3);
				hotel.order(sNo);
			}else if(select.charAt(0) == '退'){
				String sNo = select.substring(3);
				hotel.unbooking(sNo);
			}else if(select.equals("离开")){
				break;
			}else{
				System.out.println("输入有误");
			}
		}
	}
	public static void menu(){
		System.out.println("显示所有房间情况请输入:打印");
		System.out.println("预订房间请输入:预订 房间号");
		System.out.println("退订房间请输入:退订 房间号");
		System.out.println("离开系统请输入:离开");
	}
}

酒店类

package homework.dayfourteenth;

public class Hotel {
	public Room[][] rooms;

	public Hotel(){

	}
	
	public Hotel(Room[][] rooms) {
		this.rooms = rooms;
		for(int i = 0; i < rooms.length; i++){
			for(int j = 0; j < rooms[0].length; j++){
				this.rooms[i][j] = new Room((i + 1) * 100 + j + 1);
			}
		}
	}
	
	public void printAllRoom(){
		for(int i = 0; i < rooms.length; i++){
			for(int j = 0; j < rooms[0].length; j++){
				System.out.print(rooms[i][j].toString());
			}
			System.out.println();
		}
	}
	
	public int[] noSwap(String sNo){
		int[] intNo = new int[2];
		intNo[0] = Integer.parseInt(sNo) / 100 - 1;
		intNo[1] = Integer.parseInt(sNo) % 100 - 1;
		return intNo;
	}
	public void order(String sNo){
		int[] intNo = noSwap(sNo);
		if(rooms[intNo[0]][intNo[1]].isOccupy() == true){
			System.out.println("预订失败!该房间已订出,无法预订!");
		}else{
			rooms[intNo[0]][intNo[1]].setOccupy(true);
			System.out.println("预订成功!您预订的房间是" + rooms[intNo[0]][intNo[1]].getNo() + " 祝您入住愉快!");
		}
	}
	
	public void unbooking(String sNo){
		int[] intNo = noSwap(sNo);
		if(rooms[intNo[0]][intNo[1]].isOccupy() == false){
			System.out.println("退订失败!该房间已退订,请您检查要退订的房间号!");
		}else{
			rooms[intNo[0]][intNo[1]].setOccupy(false);
			System.out.println("退订成功!您退订的房间是" + rooms[intNo[0]][intNo[1]].getNo() + " 期待您的再次光临!");
		}
	}

	public Room[][] getRooms() {
		return rooms;
	}

	public void setRooms(Room[][] rooms) {
		this.rooms = rooms;
	}
	
}

房间类

package homework.dayfourteenth;

public class Room{
	private int no;
	private String level;
	private boolean occupy;
	
	
	public Room() {

	}


	public Room(int no) {
		this.no = no;
		if(no >= 101 && no <= 210){
			level = "标准间";
		}else if(no >= 301 && no <= 410){
			level = "双人间";
		}else{
			level = "豪华间";
		}
		occupy = false;
	}


	public int getNo() {
		return no;
	}


	public void setNo(int no) {
		this.no = no;
	}


	public String getLevel() {
		return level;
	}


	public void setLevel(String level) {
		this.level = level;
	}


	public boolean isOccupy() {
		return occupy;
	}


	public void setOccupy(boolean occupy) {
		this.occupy = occupy;
	}
	
	@Override
	public String toString(){
		String state = occupy == true ? "占用" : "空闲";
		return "[" + no + " " + level + " " + state + "]";
	}
}

如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤

你可能感兴趣的:(不易,Java基础知识,java,学习)