基于数组的简易宾馆系统

package Com.czie.iot1913;

import java.util.Scanner;

/**
* @author 1944900433@qq.com
* @date 2022-02-20 18:56
*/
public class HotelManagementSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println("欢迎来到刘品水的宾馆系统");
System.out.println("输入1为查看房间 输入2为订房 输入3退房 输入0退出系统");
while (true){
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (input==1){
hotel.print();
}
else if (input==2){
int roomno=sc.nextInt();
hotel.order(roomno);
}
else if (input==3){
int roomno=sc.nextInt();
hotel.exit(roomno);
}
else if (input==0){
System.out.println("再见");
System.exit(0);

/**
*System.out.println("再见");
*return;
*/
}
else {
System.out.println("你输入错误啦,请重新输入");
}
}

}
}





package Com.czie.iot1913;

/**
* @author 1944900433@qq.com
* @date 2022-02-20 18:56
*/
public class Hotel {
private Room[][]rooms;

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, "单人间", true);
} else if (i == 1) {
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准间", true);
} else if (i == 2) {
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "总统套房", true);
}

}
}
}
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]);
}
System.out.println();
}
}
public void order(int roomno){
Room room=rooms[roomno/100-1][roomno%100-1];
room.setState(false);
System.out.println(roomno+"已订房");
}
public void exit(int roomno){
Room room=rooms[roomno/100-1][roomno%100-1];
room.setState(true);
System.out.println(roomno+"已退房");
}

}



package Com.czie.iot1913;

import java.util.Objects;

/**
* @author 1944900433@qq.com
* @date 2022-02-20 18:56
*/
public class Room {
private int no;
private String type;
private boolean state;

public Room() {
}

public Room(int no, String type, boolean state) {
this.no = no;
this.type = type;
this.state = state;
}

public int getNo() {
return no;
}

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

public String getType() {
return type;
}

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

public boolean getState() {
return state;
}

public void setState(boolean state) {
this.state = state;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Room room = (Room) o;
return no == room.no && state == room.state && Objects.equals(type, room.type);
}

/* @Override
public String toString() {
return "Room{" +
"no=" + no +
", type='" + type + '\'' +
", state=" + state +
'}';
}*/

@Override
public String toString() {
//写死了
//return "[102,单人间,空闲]";
//动态(把一个变量塞到一个字符串当中)
//加一个双引号,双引号中间加两个加号 两个加号之间加变量名
return "[" + no + "," + type + "," + (state ? "空闲" : "占用") + "]";
}
}

 

posted @ 2022-03-19 00:03  刘品水  阅读(35)  评论(0编辑  收藏  举报