基本要求:
1:启动程序后用户可以看到欢迎界面并可以按照提示做出操作,比如输入2订房.
2:能够展示该酒店所有的房间信息,包括房间号,类型以及入住状态.
3:能够完成订房的功能,比如:输入101,控制台显示订房成功,并且101房间更改为已入住状态.
4:能够完成退房的功能,比如:输入101,控制台显示退房成功,并且101房间更改为未入住状态.
5:操作完成之后能够让用户退出系统并给出欢送语.
实现代码:
主入口代码:
package hotel;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Hotel h= new Hotel();
h.love();
Scanner sc=new Scanner(System.in);
System.out.println("===========================");
System.out.println("------欢迎使用酒店管理系统--------");
System.out.println("-----请输入对应的数字进行选择------");
System.out.println("-查看房间列表请按---1 -");
System.out.println("-订房请按--------2 -");
System.out.println("-退房请按--------3 -");
System.out.println("-退出请按--------4 -");
System.out.println("===========================");
while (true) {
System.out.println("您的选择:");
int i=sc.nextInt();
if (i==1){
h.print();
}
if (i==2){
System.out.println("请输入要预定的房间号");
int a=sc.nextInt();
h.order(a);
}
if(i==3){
System.out.println("请输入要退房的房间号");
int b=sc.nextInt();
h.exit(b);
}
if (i==4){
System.out.println("欢迎使用本系统,期待下次光临~");
return;
}
}
}
}
创建Hotel类:
package hotel;
public class Hotel {
private Room[][] rooms;
private String name;
public Hotel() {
rooms = new Room[5][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);
if (i == 1)
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准双人床", true);
if (i == 2)
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "豪华大床房", true);
if (i == 3)
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "豪华双床房", true);
if (i == 4)
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++) {
System.out.print(rooms[i][j]);
}
System.out.println();
}
}
public void order(int id) {
Room room1 =rooms[id / 100 - 1][id % 100 - 1];
room1.setStatus(false);
System.out.println("订房成功");
}
public void exit(int id){
Room room2 =rooms[id / 100 - 1][id % 100 - 1];
room2.setStatus(true);
System.out.println("退房成功");
}
public void love(){
float x,y;
for(y=1.3f;y>-1.1f;y-=0.15f){
for(x=-1.2f;x<=1.2f;x+=0.05f){
float temp=x*x+y*y-1;
if((Math.pow(temp,3)-(x*x*Math.pow(y,3)))<=0.0f){
System.out.print("*");
}else{
System.out.print(" ");
}
}System.out.println();
}
}
}
创建Room类:
package hotel;
import java.util.Objects;
public class Room {
private int id;
private String type;
private boolean status;
public Room() {
}
public Room(int id, String type, boolean status) {
this.id = id;
this.type = type;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Room room = (Room) o;
return id == room.id &&
status == room.status &&
Objects.equals(type, room.type);
}
@Override
public int hashCode() {
return Objects.hash(id, type, status);
}
@Override
public String toString() {
return "{" +
"房号:" + id +
','+"类型:" + type +
", " + "状态:"+(status?"空闲":"已预订") +
'}';
}
}
后记:
该案例将面向对象和二维数组结合了起来,涉及到成员方法的定义和调用,掌握该案例有助于更加深刻的理解和掌握面向对象在java开发过程的使用