为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。Room[][]rooms;"
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
5、系统应该对外提供的功能:
可以预定房间:用户输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。
主方法:
package com.company.Hotel;
/*为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。Room[][]rooms;"
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
5、系统应该对外提供的功能:
可以预定房间:用户输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。*/
public class HotelSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
hotel.print();
}
}
定义room类
package com.company.Hotel;
import java.util.Objects;
public class Room {
private int no;//房间编号
private String type;//房间类型
private boolean status;//房间状态,true表示空闲,false表示占用
public Room() {
}//无参构造方法
public Room(int no, String type, boolean status) {//构造方法
this.no = no;
this.type = type;
this.status = status;
}
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和toString方法,用toSting方法将java对象转换成字符串形式
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Room)) return false;
Room room = (Room) o;
return getNo() == room.getNo() && isStatus() == room.isStatus() && Objects.equals(getType(), room.getType());
}//equals方法
@Override
public int hashCode() {
return Objects.hash(getNo(), getType(), isStatus());
}
public String toString() {
return "Room{" +
"no=" + no +
", type='" + type + '\'' +
", status=" + status +
'}';
}
//快捷键 ctrl+shift+/ 多行注释
/* public static void main(String[] args) {
Room room = new Room();
System.out.println(room);
}*/
//查看一个类的属性和方法快捷键 ctrl+F12
}
定义hotel类
package com.company.Hotel;
//酒店对象,酒店中有二维数组,二维数组模拟大厦中的所有房间
public class Hotel {
private Room[][] rooms;
public Hotel(){
rooms = new Room[3][10];//数组三行十列,三层楼,每层楼10个房间
//将30个roon对象放到数组当中,即二维数组的遍历
for (int i = 0; i < rooms.length; i++) {//i是编号,i+1是楼层
for (int j = 0; j < rooms[i].length; j++) {
int no;
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(){
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();
}
}
}
上述程序运行结果输出所有房间信息:
Room{no=101, type=‘单人间’, status=true}Room{no=102, type=‘单人间’, status=true}Room{no=103, type=‘单人间’, status=true}Room{no=104, type=‘单人间’, status=true}Room{no=105, type=‘单人间’, status=true}Room{no=106, type=‘单人间’, status=true}Room{no=107, type=‘单人间’, status=true}Room{no=108, type=‘单人间’, status=true}Room{no=109, type=‘单人间’, status=true}Room{no=110, type=‘单人间’, status=true}
Room{no=201, type=‘标准间’, status=true}Room{no=202, type=‘标准间’, status=true}Room{no=203, type=‘标准间’, status=true}Room{no=204, type=‘标准间’, status=true}Room{no=205, type=‘标准间’, status=true}Room{no=206, type=‘标准间’, status=true}Room{no=207, type=‘标准间’, status=true}Room{no=208, type=‘标准间’, status=true}Room{no=209, type=‘标准间’, status=true}Room{no=210, type=‘标准间’, status=true}
Room{no=301, type=‘套件’, status=true}Room{no=302, type=‘套件’, status=true}Room{no=303, type=‘套件’, status=true}Room{no=304, type=‘套件’, status=true}Room{no=305, type=‘套件’, status=true}Room{no=306, type=‘套件’, status=true}Room{no=307, type=‘套件’, status=true}Room{no=308, type=‘套件’, status=true}Room{no=309, type=‘套件’, status=true}Room{no=310, type=‘套件’, status=true}
加上订房和退房程序,无非就是把房间状态statue改变,订房由true改为false,退房反之。
修改后的hotel类为
package com.company.Hotel;
//酒店对象,酒店中有二维数组,二维数组模拟大厦中的所有房间
public class Hotel {
private Room[][] rooms;
public Hotel(){
rooms = new Room[3][10];//数组三行十列,三层楼,每层楼10个房间
//将30个roon对象放到数组当中,即二维数组的遍历
for (int i = 0; i < rooms.length; i++) {//i是楼层
for (int j = 0; j < rooms[i].length; j++) {
int no;
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(){
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();
}
}
//订房方法,订房方法就是将房间对象的状态修改为false
public void order(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(false);
}
//退房方法,退房方法就是将房间对象的状态修改为ture
public void exit(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(true);
}
}
最终程序,完成题目要求所有功能,即可以订房,退房,显示所有房间信息
主方法
package com.company.Hotel;
/*为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。Room[][]rooms;"
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
5、系统应该对外提供的功能:
可以预定房间:用户输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。*/
import java.util.Scanner;
public class HotelSystem {
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) {//加一个while循环之后,系统可以一直使用,输入0退出,否则系统只能使用一次
System.out.print("请输入对应的功能编号: ");
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("欢迎使用");
} else {
System.out.println("输入有误,请重新输入");
}
}
}
}
Room类
package com.company.Hotel;
import java.util.Objects;
public class Room {
private int no;//房间编号
private String type;//房间类型
private boolean status;//房间状态,true表示空闲,false表示占用
public Room() {
}//无参构造方法
public Room(int no, String type, boolean status) {//构造方法
this.no = no;
this.type = type;
this.status = status;
}
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和toString方法,用toSting方法将java对象转换成字符串形式
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Room)) return false;
Room room = (Room) o;
return getNo() == room.getNo() && isStatus() == room.isStatus() && Objects.equals(getType(), room.getType());
}//equals方法
@Override
public int hashCode() {
return Objects.hash(getNo(), getType(), isStatus());
}
public String toString() {
return "Room{" +
"no=" + no +
", type='" + type + '\'' +
", status=" + status +
'}';
}
//快捷键 ctrl+shift+/ 多行注释
/* public static void main(String[] args) {
Room room = new Room();
System.out.println(room);
}*/
//查看一个类的属性和方法快捷键 ctrl+F12
}
Hotel类
package com.company.Hotel;
//酒店对象,酒店中有二维数组,二维数组模拟大厦中的所有房间
public class Hotel {
private Room[][] rooms;
public Hotel(){
rooms = new Room[3][10];//数组三行十列,三层楼,每层楼10个房间
//将30个roon对象放到数组当中,即二维数组的遍历
for (int i = 0; i < rooms.length; i++) {//i是楼层
for (int j = 0; j < rooms[i].length; j++) {
int no;
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(){
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();
}
}
//订房方法,订房方法就是将房间对象的状态修改为false
public void order(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(false);
}
//退房方法,退房方法就是将房间对象的状态修改为ture
public void exit(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(true);
}
}