酒店管理系统
为某个酒店编写程序:酒店管理系统,模拟订房,退房,打印所有房间状态等功能
1、该系统的用户是酒店的前台
2、酒店当中所有的房间使用一个二维数组来模拟
3、酒店当中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号,房间类型属性,房间是否空闲
Hotel.java
public class Hotel {
private Room[][] rooms;
public Hotel(){
//通过构造方法来盖楼层
rooms=new Room[3][10];//动态初始化,里面每个元素都还是null
//创建30个room对象,放到数组当中
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{
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.length;j++){
Room room=rooms[i][j];
System.out.println(room);
}
}
}
public void order(int roomNo){
//订房主要是将房间对象的status修改为false
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+"已经退房!");
}
}
HotelSystem.java
public class HotelSystem {
public static void main(String[] args) {
System.out.println("欢迎使用酒店管理系统,请认真阅读以下说明:");
System.out.println("请输入对应的功能编号:【1】查看房间列表。【2】订房。【3】退房。【0】退出系统");
Scanner s=new Scanner(System.in);
Hotel hotel=new Hotel();
while(true){
System.out.print("请输入功能编号:");
int i=s.nextInt();
if(i==1){
//查看房间列表
hotel.print();
}else if(i==2){
//订房
System.out.print("请输入要订房的房间号码:");
int numNo=s.nextInt();
hotel.order(numNo);
}else if(i==3){
//退房
System.out.print("请输入要退房的房间号码:");
int numNo=s.nextInt();
hotel.exit(numNo);
}else if(i==0){
//退出系统
System.out.print("已退出系统");
return;
}else {
System.out.print("输入错误,请重新输入:");
}
}
}
}
Room.java
import java.util.Objects;
import java.util.Scanner;
public class Room {
private int no;
private String type;
private boolean status;
//构造方法
public Room(int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
public Room() {
}
//set和get方法
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 isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
//equals方法重写
@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 && status == room.status && Objects.equals(type, room.type);
}
@Override
public int hashCode() {
return Objects.hash(no, type, status);
}
//toString方法重写
@Override
public String toString() {
return "[" + "房间编号:" + no + ", 房间类型:'" + type + ", 房间状态:" + status + ']';
}
}
以上就是JavaSE实战之酒店订房系统的实现的详细内容,更多关于JavaSE 酒店订房系统的资料请关注编程网其它相关文章!