利用数组创建酒店管理系统
该项目只是入门基础学习,项目已通过测试,有什么不明白的地方可以站内联系,互相讨论
Room类
/**
* 酒店中的房间类;
* */
public class Room {
private String no;//房间编号
private String type;//房间种类“标准间”“双人间”“豪华间”
private boolean isUse;//true表示占用,false表示空闲
//私有成员变量的set and get method
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
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;
}
//无参Constructor
public Room() {
}
public Room(String no,String type,boolean isUse){
this.no = no;
this.type = type;
this.isUse = isUse;
}
//重写toString方法,print输出的时候会调用toString方法
public String toString(){
return "["+no+","+type+","+(isUse?"占用":"空闲")+"]";
}
}
Hotel类
/**
* 酒店类
* */
public class Hotel {
Room[][] rooms;
public Hotel(){
//5层 每层10个房间
rooms = new Room[5][10];
//对酒店属性赋值(赋房间的值)
for (int i = 0; i < rooms.length; i++) {//1~4层
for (int j = 0; j < rooms[i].length; j++) {//每层的10个房间
if ( i==0 || i==1 ) {//1~2层
rooms[i][j] = new Room((i+1)*100+j+1+"", "标准间", false);//将房间这个对象属性放入数组中
}
if ( i==2 || i==3 ) {//3~4层
rooms[i][j] = new Room((i+1)*100+j+1+"", "双人间", false);
}
if ( i==4 ) {//5层
rooms[i][j] = new Room((i+1)*100+j+1+"", "豪华间", false);
}
}
}
}
//遍历酒店房间的属性
public void searchHotel(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j]);
}
System.out.println();
}
}
//预定房间的method
public void reservation(String no){
for (int i = 0; i < rooms.length; i++) {//遍历每个房间查看是否和输入的编号相等,如果相等则预定成功。
for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].getNo().equals(no)) {
if (rooms[i][j].isUse() == false) {
rooms[i][j].setUse(true);
System.out.println(rooms[i][j].getNo()+"预定成功!");
}else{
System.out.println("该房间已经被预定,不能再次预定!");
}
break;
}
}
}
}
//退订房间的method
public void outRoom(String no){
for (int i = 0; i < rooms.length; i++) {//遍历每个房间查看是否和输入的编号相等,如果相等则退订成功。
for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].getNo().equals(no)) {
if (rooms[i][j].isUse() == true) {
rooms[i][j].setUse(false);
System.out.println(rooms[i][j].getNo()+"退订成功!");
}else{
System.out.println("该房间未被预定,不能退订!");
}
break;
}
}
}
}
}
测试类
import java.util.Scanner;
public class HotelTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("欢迎使用酒店管理系统");
Hotel h = new Hotel();//创建酒店对象
h.searchHotel();//遍历酒店,查看酒店房间
System.out.print("请输入您要预定房间的编号:");
h.reservation(input.next());
h.searchHotel();//遍历酒店,查看酒店房间
System.out.print("请输入您要退订房间的编号:");
h.outRoom(input.next());
h.searchHotel();
}
}
希望对每个人都有帮助!