聽老師講了一些ArrayBasic的一些知識,讓制作一個酒店管理系統,要求:顯示酒店所有房間列表,預訂房間....
經過老師的指導寫了一個代碼,如下:
import java.util.Scanner; public class a1{ public static void main(String[] args){ Scanner s = new Scanner(System.in);//接受客戶鍵盤輸入,在命令行中,回車結束 System.out.println( "酒店管理系統" ); Hotel h = new Hotel(); System.out.println( h ); h.print(); while (true){ System.out.println( "請輸入房間編號" ); //聲明變量,用於接受控制台輸入數據 String no = s.next(); //預訂房間 h.order(no); h.print(); } } } class Room{ //房間號 private String no; //房間類型 private String type; //是否被占用 private boolean isUse; public Room(){//默認調用 super(); } public Room(String no,String type,boolean isUse){ super(); this.no = no; this.type = type; this.isUse = isUse; } 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; } public String toString(){ //聲明輸出結果格式 return "[" + no + "," + type + "," + (isUse?"占用":"空閑") + "]"; } } class Hotel{ Room rooms[][]; public Hotel(){ rooms = new Room[5][4];//旅館有五層,每層四個房間 for(int i=0; i < rooms.length; ++i){//外層for循環是循環層,內存循環是循環的每層的房間 for(int j=0; j < rooms[i].length; ++j){ if (i == 0 || i == 1) { //后面加個空字符串,是自動數據類型轉換,這樣前面的數字會自動變成數字型字符串; rooms[i][j] = new Room((i+1)*100+j+1 +"" , "標准間",false); } if (i == 2 || i == 3) { rooms[i][j] = new Room((i+1)*100+j+1 +"" , "雙人間",false); } if (i == 4) { rooms[i][j] = new Room((i+1)*100+j+1 +"" , "豪華間",false); } } } } //對外提供房間列表的打印方式 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(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)) { //把對象的成員數據封裝,通過成員方法訪問 //1 成員變量的訪問方式rooms[i][j].no; //2 成員方法的訪問方式 rooms[i][j].setUse(true); return; } } } } }
效果:
因需要支持外部預定,用戶輸入房間號,選擇五樓包場,效果如下:
以上就是所有代碼,請大家斧正!