1.酒店管理demo:
//Room.java
package hotel;
public class Room {
private int num;
private boolean isOut;
public Room(int n) {
this.num = n;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean isOut() {
return isOut;
}
public void setOut(boolean isOut) {
this.isOut = isOut;
}
public String toString() {
return "[" + this.getNum() + "," + (this.isOut() ? "是" : "否") + "] ";
}
}
//Hotel.java
package hotel;
public class Hotel {
private int colum;
private int row;
Room[][] rooms;
public Hotel(int row, int colum) {
this.colum = colum;
this.row = row;
this.rooms = new Room[row][colum];
// 初始化房间号
this.initRooms();
}
private void initRooms() {
for (int i = 0; i < this.rooms.length; i++) {
for (int j = 0; j < this.rooms[i].length; j++) {
this.rooms[i][j] = new Room((i + 1) * 100 + (j + 1));
}
}
}
/**
* 入住酒店
*/
public void setRooms(int roomNum) {
// 楼层
int row = roomNum / 100 - 1;
// 房间号
int colum = roomNum % 100 - 1;
if (!this.rooms[row][colum].isOut()) {
this.rooms[row][colum].setOut(true);
System.out.println("房间订阅完成");
} else {
System.out.println("房间已被订");
return;
}
}
/**
* 退订酒店
*/
public void setRoomsOut(int roomNum) {
// 楼层
int row = roomNum / 100 - 1;
// 房间号
int colum = roomNum % 100 - 1;
this.rooms[row][colum].setOut(false);
System.out.println("房间退订完成");
}
/**
* 查询房间状态
*/
public void seeRooms() {
if (this.rooms.length == 0) {
System.out.println("酒店未初始化");
}
for (int i = 0; i < this.rooms.length; i++) {
for (int j = 0; j < this.rooms[i].length; j++) {
System.out.print(this.rooms[i][j]);
}
System.out.println("");
}
}
}
//App.java
import java.util.Scanner;
import hotel.Hotel;
import hotel.Room;
public class App {
public static void main(String[] args) throws Exception {
if (args.length == 2) {
if ("admin".equals(args[0]) && "123".equals(args[1])) {
Hotel h = new Hotel(10, 11);
System.out.println("欢迎登录管理系统");
while (true) {
System.out.println("输入以下指令操作系统:[1]查看所有房间;[2]订房;[3]退房;[4]退出系统;");
Scanner s = new Scanner(System.in);
switch (s.nextInt()) {
case 1:
h.seeRooms();
break;
case 2:
System.out.println("订房:请输入房间号");
Scanner s2 = new Scanner(System.in);
h.setRooms(s2.nextInt());
break;
case 3:
System.out.println("退房:请输入房间号");
Scanner s3 = new Scanner(System.in);
h.setRoomsOut(s3.nextInt());
break;
case 4:
System.out.println("欢迎下次再来");
return;
}
}
} else {
System.out.println("账号密码错误");
return;
}
} else {
System.out.println("非法进入");
return;
}
}
}