代码思路:
1.优先创建父类接口,供后续创建大容纳的数组
2.创建房间类,创建有参构造,用于设置类型
3.理清先后顺序,逐个功能部件写代码
代码优势:
1.防止绝大部分异常
2.入住退房清晰可见
3.浏览全局无压力
4.代码功能部件简洁,基础,粗暴
源码(建议尝试复制下来跑一遍(笑)):
interface Hotel{
public boolean getStatus();
public int getNumber();
public String getType();
public void setStatus(boolean status);
}
interface jjzx extends Hotel{}
interface hxx extends Hotel{}
class Room implements jjzx,hxx{
private boolean status;
private String type;
private int number;
public Room(boolean a,String b,int c){
this.status = a;
this.type = b;
this.number = c;
}
public void setStatus(boolean status){
this.status = status;
}
public boolean getStatus(){
return status;
}
public String getType(){
return type;
}
public int getNumber(){
return number;
}
}
class Zone{
public static int Zone(){
java.util.Scanner s = new java.util.Scanner(System.in);
System.out.println("==========================");
System.out.println("目前已有以下区:");
System.out.println(" 1.锦江之星");
System.out.println(" 2.幻想乡");
System.out.println("请输入您要执行操作的区域:");
int a = 0;
boolean b;
do {
b = false;
switch (s.next()) {
case "锦江之星":
case "1":
a = 0;
break;
case "幻想乡":
case "2":
a = 1;
break;
default:
System.out.println("无效的输入!");
b = true;
}
}while (b);
return a;
}
}
class CheckOut_CheckIn {
private static jjzx[] j = {new Room(true, "钟点房", 1), new Room(false, "钟点房", 2)};
private static hxx[] h = {new Room(true, "行政房", 1), new Room(false, "行政房", 2)};
private static Hotel[][] jd = {j, h};
public static void CheckIn() {
java.util.Scanner s = new java.util.Scanner(System.in);
boolean doswitch = true;
do {
int num;//房间号
int quyu = Zone.Zone();//区域
String m = " ";//这个就是接受汉字
boolean dos = true;//循环
do {
System.out.println("==========================");
System.out.print("请输入您要入住的房间号:");
num = s.nextInt() - 1;
//这一行报错!
if (num >= jd[quyu].length | num < 0) {
System.out.println("抱歉,该房间不存在!请输入小于:" + (jd[quyu].length + 1) + "大于0的值!");
} else {
dos = false;
}
} while (dos);
switch (quyu) {
case 0:
m = "锦江之星";
break;
case 1:
m = "幻想乡";
break;
}
if (jd[quyu][num].getStatus()) {
System.out.println("区域:" + m + " " + (num + 1) + "号房间已满,请重新选择!");
} else {
System.out.println(m + (num + 1) + "号房间已成功入住!");
jd[quyu][num].setStatus(true);
doswitch = false;
}
} while (doswitch);
}
public static void CheckOut(){
java.util.Scanner s = new java.util.Scanner(System.in);
boolean doswitch = false;
do {
int num;//房间号
int quyu = Zone.Zone();
String m = " ";
boolean dos = true;
do {
System.out.println("==========================");
System.out.print("请输入您要退房的房间号:");
num = s.nextInt() - 1;
if (num >= jd[quyu].length | num < 0) {
System.out.println("抱歉,该房间不存在!请输入小于:" + (jd[quyu].length + 1) + "大于0的值!");
}else {
dos = false;
}
}while (dos);
if (!(jd[quyu][num].getStatus())){
System.out.println("此屋还没有人居住哦~请重新输入!");
}else {
jd[quyu][num].setStatus(false);
System.out.println("退房成功!欢迎下次光临!");
doswitch = false;
}
}while (doswitch);
}
public static void Reading(){
System.out.println("==========================");
System.out.println("以下为查询结果:");
String m = " ";
for(int i = 0;i < jd.length;i++){
for(int o = 0;o < jd[i].length;o++){
switch (i) {
case 0:
m = "锦江之星";
break;
case 1:
m = "幻想乡";
break;
}
System.out.println("房间区域:" + m + " 房间号:" + jd[i][o].getNumber() + " 房间类型:" + jd[i][o].getType() + " 房间状态:" + jd[i][o].getStatus());
}
}
}
}
public class Main{
public static void main(String[] args) {
java.util.Scanner s = new java.util.Scanner(System.in);
boolean doswitch = true;
do{
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("请输入您要执行的操作:");
switch (s.next()){
case "入住":
case "1":
CheckOut_CheckIn.CheckIn();
break;
case "退房":
case "2":
CheckOut_CheckIn.CheckOut();
break;
case "查询所有房间位置及其状态":
case "3":
CheckOut_CheckIn.Reading();
break;
case "退出程序":
case "4":
doswitch = false;
}
}while (doswitch);
}
}