Java类与对象习题
问题:编写一个酒店管理系统
要求:
1.首先规定酒店5层,每层10间房间
2.1,2层“标准间”,3,4层“双人间”,第5层“豪华间”。
3.酒店提供退房预定房间两种方法,其他的方法自己可以随意添加。
4.运用类与对象方面的知识进行编写
下面是答案以及源码
1.Room.java(房间类)
package com.jvstudy.day04.text1;
/**
* 房间类:房间提供3种类型“标准间,双人间,豪华间”
* id: 房间号
* type: 类型“标准间,双人间,豪华间”
* isUse: 是否已经有人,默认没有
*/
public class Room {
private String id;
private String type;
private boolean isUse = false;
public Room() {
super();
}
public Room(String id, String type) {
super();
this.id = id;
this.type = type;
}
//getter and setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isUse() {
return isUse;
}
public void setUse(boolean isUse) {
this.isUse = isUse;
}
}
2.Hotel类(酒店类)
package com.jvstudy.day04.text1;
/**
* 酒店类:规定总共5层,每层10个房间
* 1,2标志间
* 3,4双人间
* 5 豪华间
* 提供定房退房方法
*/
public class Hotel {
public static final String STANDARD_ROOM = "标准间";
public static final String TWIN_ROOM = "双人间";
public static final String DELUXE_ROOM = "豪华间";
Room[][] rooms;
public Hotel() {
this(5,10);
}
public Hotel(int rows,int cols) {
this.rooms = new Room[rows][cols];
this.hotelInit(rows, cols);
}
//酒店初始化
public void hotelInit(int rows,int cols) {
for(int row=0;row<rows;row++)
for(int col=0;col<cols;col++)
{
this.rooms[row][col] = new Room();
if(row<2)
this.rooms[row][col].setType(STANDARD_ROOM);
else if(row<4)
this.rooms[row][col].setType(TWIN_ROOM);
else
this.rooms[row][col].setType(DELUXE_ROOM);
this.rooms[row][col].setId((row+1)+"0"+col);
}
}
//预定房间
public void bookRoom(String type) {
if(STANDARD_ROOM.equals(type)) {
for(int row=0;row<2;row++)
for(int col=0;col<10;col++)
if(!this.rooms[row][col].isUse()) {
System.out.println("预定成功,你的房间号是:"+this.rooms[row][col].getId());
this.rooms[row][col].setUse(true);
return;
}
System.out.println(STANDARD_ROOM+"已经没有多余的空房了,请选择其他房间");
}
else if(TWIN_ROOM.equals(type)) {
for(int row=2;row<4;row++)
for(int col=0;col<10;col++)
if(!this.rooms[row][col].isUse()) {
System.out.println("预定成功,你的房间号是:"+this.rooms[row][col].getId());
this.rooms[row][col].setUse(true);
return;
}
System.out.println(TWIN_ROOM+"已经没有多余的空房了,请选择其他房间");
}
else if(DELUXE_ROOM.equals(type)) {
for(int row=4;row<5;row++)
for(int col=0;col<10;col++)
if(!this.rooms[row][col].isUse()) {
System.out.println("预定成功,你的房间号是:"+this.rooms[row][col].getId());
this.rooms[row][col].setUse(true);
return;
}
System.out.println(DELUXE_ROOM+"已经没有多余的空房了,请选择其他房间");
}
else {
System.out.println("本酒店没有此类房间,请选择下面几种\t");
System.out.println("\t\t\t1:"+STANDARD_ROOM);
System.out.println("\t\t\t2:"+TWIN_ROOM);
System.out.println("\t\t\t1:"+DELUXE_ROOM);
}
}
//退房
public void check(String id) {
if(id.length()==3) {
this.rooms[(int)id.charAt(0)-49-1][(int)id.charAt(2)-49].setUse(false);
System.out.println("退房成功");
}
else
System.out.println("房间号输入错误,退房失败");
}
}
3.测试
package com.jvstudy.day04.text1;
/**
* 测试
*/
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hotel ht = new Hotel();
ht.bookRoom("ddd");
int i=0;
while(i<11) {
ht.bookRoom("豪华间");
i++;
}
ht.check("5555");
ht.check("501");
}
}