1.先创建一个房间类,属性为房间号、是否占用、房间的种类,并创建相应的构造方法。
public class Room {
private int no;
private String type;
private boolean status;
public Room() {
}
public Room (int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
public void setNo(int no) {
this.no = no;
}
public void setType(String type) {
this.type = type;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getNo() {
return no;
}
public String getType() {
return type;
}
public boolean isStatus() {
return status;
}
重写Object中的toString方法,并进行测试。
public String toString(){
return "["+no+","+type+","+(status?"空闲":"占用")+"]";
}
//测试程序
/*public static void main(String[] args) {
Room room = new Room(101,"单人间",false);
System.out.println(room);
}*/
重新Object中的equals方法,并进行测试。
public boolean equals(Object obj){
if(obj == null ||!(obj instanceof Room)) return false;
if(this == obj) return true;
Room room = (Room)obj;
return this.getNo() == room.getNo();
}
/*测试程序
public static void main(String[] args) {
Room room = new Room(101,"单人间",false);
System.out.println(room);
Room room1 = new Room(101,"单人间",false);
System.out.println(room.equals(room1));
}
*/
再添加一个Hotel类。创建房间编号,并对相应构造方法进行设置。
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);
}
if(i == 1){
rooms[i][j] = new Room((i+1)*100+j+1,"标准间",true);
}
if(i ==2){
rooms[i][j] = new Room((i+1)*100+j+1,"豪华间",true);
}
}
}
}
public Room[][] getRooms() {
return rooms;
}
public void setRooms(Room[][] rooms) {
this.rooms = rooms;
}
编写打印、订房、退房的方法。
public void print(){
for(int i=0;i<rooms.length;i++){
for(int j=0;j<rooms[i].length;j++){
Room room = rooms[i][j];
System.out.print(room);
}
System.out.println();
}
}
public void order(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100- 1];
room.setStatus(false);
System.out.println(roomNo+"已经订房");
}
public void exit(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100-1];
room.setStatus(true);
System.out.println(roomNo+"已经退房");
}
}
创建一个测试程序的主方法。输入相应的编号表示相应的功能。
import java.util.Scanner;
public class HotelMgtSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println("欢迎使用酒店管理系统,请认真阅读相关说明");
System.out.println("功能对应的编号:[1]查看房间列表[2]表示订房[3]表示退房[0]表示提出系统");
Scanner s = new Scanner(System.in);
while(true){
System.out.println("请输入相关编号");
int i = s.nextInt();
if(i == 1){
hotel.print();
}
else if(i == 2){
System.out.println("请输入订房编号");
int roomNo = s.nextInt();
hotel.order(roomNo);
}
else if(i == 3){
System.out.println("请输入退房编号");
int roomNo = s.nextInt();
hotel.exit(roomNo);
}
else if(i == 0){
System.out.println("欢迎下次在此光临");
return;
}
else{
System.out.println("输入错误,请重新进行输入");
}
}
}
}