为某个酒店编写程序:酒店管理系统,要求模拟订房、退房、打印所有房间状态等功能。
思路:
1.该系统用户是:酒店前台
2.酒店使用一个二维数组来模拟。如Room[][] rooms;
3.酒店的每一个房间都应该是一个java对象:Room
4.每一个房间Room都应该有:房间编号、房间类型、房间是否空闲等属性
5.系统应该对外提供的功能:
可以预定房间:用户通过输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可 以查看所有房间的状态。
========================================
Room类
public class Room {
//房间所具备的属性
//房间编号
private int no;
//房间类型
private String type;
//房间是否空闲
private boolean free;
//get/set方法
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 isFree() {
return free;
}
public void setFree(boolean free) {
this.free = free;
}
//构造方法
public Room() {
}
public Room(int no, String type, boolean free) {
this.no = no;
this.type = type;
this.free = free;
}
//toString 方法重写
public String toString(){
return "["+no+","+type+","+ (free ?"空闲":"占用")+"]";
}
//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();
}
}
================================================
Hotel类
import java.util.Scanner;
public class Hotel {
private Room[][] rooms;
public Hotel(){
//一共几层,每层的房间类型是什么,每个房间的编号是什么?
rooms = new Room[3][10]; // 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() {
System.out.println("=====下面显示全部房间信息=====");
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 book(int no) {
System.out.println("*****正在查询中请稍候*****");
//获得楼层数、房间数
int i = no / 100 -1;
int j = no % 100 -1;
if (i<0 || i>=rooms.length || j <0 || j>= rooms[i].length){
System.out.println("输入的房间号有误!");
}else if (rooms[i][j].isFree()) {
//如果房间为空,则将该房间置为使用,并输出
rooms[i][j].setFree(false);
System.out.println("*****预定成功!*****");
} else {
System.out.println("*****该房间已被预定!*****");
}
}
public void cheeckout(int no) {
System.out.println("*****正在查询中请稍候*****");
//获得楼层数、房间数
int i = no / 100 -1;
int j = no % 100 -1;
if (i<0 || i>=rooms.length || j <0 || j>= rooms[i].length){
System.out.println("输入的房间号有误!");
}else if (rooms[i][j].isFree()){
System.out.println("*****退房失败!该房间为空*****");
}else{
rooms[i][j].setFree(true);
System.out.println("*****退房成功!*****");
}
}
}
================================================
main方法类
import java.util.Scanner;
public class User {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String index;
Hotel hotel = new Hotel();
while (true) {
menu();
index = scan.nextLine();
switch (index) {
case "1": {
System.out.println("请输入房间号");
int no = scan.nextInt();
scan.nextLine();
hotel.book(no);
break;
}
case "2": {
System.out.println("请输入房间号");
int no = scan.nextInt();
scan.nextLine();
hotel.cheeckout(no);
break;
}
case "3": {
hotel.print();
break;
}
case "4": {
System.out.println("=====程序结束!=====");
return;
}
default: {
System.out.println("您输入的指令有误!请重新输入!");
break;
}
}
}
}
public static void menu() {
System.out.println("=====欢迎使用酒店管理系统=====");
System.out.println("=====您可以输入以下指令完成操作=====");
System.out.println("输入1**********预定房间");
System.out.println("输入2**********退掉房间");
System.out.println("输入3**********查看所有房间");
System.out.println("输入4**********退出");
}
}