二维数组模拟酒店订房系统
生活随笔
收集整理的這篇文章主要介紹了
二维数组模拟酒店订房系统
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
需求分析:顯示房間狀態(tài)、訂房、退房。
需要三個(gè)類:測(cè)試類、酒店類、房間類
房間類有三個(gè)attribute:房間編號(hào)、房間類型、房間狀態(tài)
酒店類三個(gè)operation:顯示房間狀態(tài)、訂房、退房
public class Room {private int no;private String type;private boolean occupancy;public int getNo(){return no;}public void setNo(){this.no = no;}public String getType(){return type;}public void setType(String type){this.type = type;}public boolean getOccupancy(){return occupancy;}public void setOccupancy(boolean occupancy){this.occupancy = occupancy;}public Room(int no,String type,boolean occupancy){this.no = no;this.type = type;this.occupancy = occupancy;}public String toString(){String str = occupancy ? "占用" : "空閑";return "[no="+no+",房型="+type+",狀態(tài)"+str+"]";} } public class Hotel {Room[][] rooms;public Hotel(){rooms = new Room[3][9];//3層,每層9個(gè)房間//給房間編號(hào)for(int i = 0;i < rooms.length ;i++){for(int j = 0;j < rooms[i].length;j++){//一樓單人間,二樓標(biāo)準(zhǔn)間,三樓豪華間if(i==0){rooms[i][j] = new Room((i+1)*100+j+1,"單人間",false);}if(i==1){rooms[i][j] = new Room((i+1)*100+j+1,"標(biāo)準(zhǔn)間",false);}if(i==2){rooms[i][j] = new Room((i+1)*100+j+1,"豪華間",false);}}}}public void display(){for(int i = 0;i < rooms.length;i++){for(int j =0;j < rooms[i].length;j++){if(j%3==0){//每行顯示3個(gè)房間信息System.out.println();}System.out.print(rooms[i][j]+"\t");}}}public void reserve(int no){for(int i = 0;i < rooms.length;i++){for(int j =0;j < rooms[i].length;j++){if(rooms[i][j].getNo()==no){rooms[i][j].setOccupancy(true);System.out.println("預(yù)定成功!您預(yù)定的房間編號(hào)為:"+no);return;}}}}public void checkout(int no){for(int i = 0;i < rooms.length;i++){for(int j =0;j < rooms[i].length;j++){if(rooms[i][j].getNo()==no){rooms[i][j].setOccupancy(false);System.out.println("您已成功退掉:"+no+"號(hào)房!");return;}}}}public boolean getRoomOccupancy(int no){for(int i = 0;i < rooms.length;i++){for(int j =0;j < rooms[i].length;j++){if(rooms[i][j].getNo()==no){return rooms[i][j].getOccupancy();}}}System.out.println("您輸入的房間不存在!");return false;} } import java.util.*; public class Test {public static void main(String[] agrs){Scanner input = new Scanner(System.in);Hotel hotel = new Hotel();int no;System.out.println("-------------------歡迎來(lái)到XX酒店-------------------");while(true){System.out.println("請(qǐng)選擇服務(wù)(輸入數(shù)字):1、查詢房間情況 2、預(yù)定房間 3、退房");switch(input.nextInt()){case 1:{System.out.println("------------------- 酒店房間情況--------------------");hotel.display();break;}case 2:{while(true){try{System.out.println("請(qǐng)輸入您想要入住的房間編號(hào)");no = input.nextInt();if(hotel.getRoomOccupancy(no)){System.out.println("對(duì)不起,該房間已有人入住,請(qǐng)重新選擇!");}else{hotel.reserve(no);break;}}catch(Exception e){System.out.println("您輸入的信息有誤,請(qǐng)重新進(jìn)入系統(tǒng)!");}}break;}case 3:{while(true){try{System.out.println("請(qǐng)輸入您想退的房間號(hào)");no = input.nextInt();if(hotel.getRoomOccupancy(no)){hotel.checkout(no);break;}else{System.out.println("該房間無(wú)人入住,請(qǐng)重新輸入!");}}catch(Exception e){System.out.println("您輸入的信息有誤,請(qǐng)重新進(jìn)入系統(tǒng)!");}}break;}}System.out.println("\n按任意鍵繼續(xù)使用,按q退出");if(input.next().charAt(0)=='q'){break;}}} }總結(jié)
以上是生活随笔為你收集整理的二维数组模拟酒店订房系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快打一族 免费
- 下一篇: AT89S52最小系统