一、編寫一個酒店管理系統 1.直接上代碼 package com.bjpowernode.java_learning; public class D69_1_ { //編寫一個程式模擬酒店的管理系統:預定房間、退房....... public static void main(String[] ...
一、編寫一個酒店管理系統
1.直接上代碼
package com.bjpowernode.java_learning; public class D69_1_ { //編寫一個程式模擬酒店的管理系統:預定房間、退房....... public static void main(String[] args) { } } class Room{ String no; String type;//“標準間”“雙人間”“豪華間” boolean isUse;//true表示占用,false表示空閑 } class Hotel{ //規定酒店:五層,每層十個房間,1,2層標準間,3,4雙人間,5層豪華間 Room[][] rooms; //無參數 Hotel(){ this(5,10); } Hotel(int rows,int cols){ rooms = new Room[rows][cols]; /* * rooms[0][0].... * rooms[4][9].... */ } //對外提供預定方法 }
二、接受用戶鍵盤輸入
1.下麵演示如何接受用戶的輸入
package com.bjpowernode.java_learning; import java.util.Scanner; public class D69_2_AcceptUserInput { public static void main(String[] args) { Scanner s = new Scanner(System.in); //程式運行到此處,停下來,等待用戶的輸入 String userInput = s.next(); System.out.println("您輸入了:"+userInput); } }
三、使用數組模擬棧Stack
package com.bjpowernode.java_learning; public class D69_3_ArraysSimulateStack { public static void main(String[] args) { Stack s = new Stack(2); User69 u1 = new User69("jfidsa",12); User69 u2 = new User69("jfiddfsa",12); User69 u3 = new User69("jfidfdsfsa",12); try { s.push(u1); s.push(u2); s.push(u3); }catch(StackOperationException e) { System.out.println(e); } // System.out.println(s.pop()); } } class Stack{ //使用數組存儲數據,註意棧可以存儲多個引用類型的元素 Object[] elements; //指向棧頂元素上方的一個幀 int index; //棧預設的初始化容量是10 Stack(){ this(10); } Stack(int max){ elements = new Object[max]; } //棧應該對外提供一個壓棧的方法 public void push(Object element) throws StackOperationException{ if (index == elements.length) { throw new StackOperationException("棧已經滿了"); } elements[index++] = element; } //對外提供一個彈棧的方法 public Object pop() throws StackOperationException{ if(index == 0) { throw new StackOperationException("棧已經空了"); } return elements[--index]; } } class User69{ String name; int age; User69(String name,int age){ this.name = name; this.age = age; } public String toString() { return "User是我"; } } class StackOperationException extends Exception{ public StackOperationException() {} public StackOperationException(String msg) { super(msg); } }
四、源碼:
D69_1_HotelManageSystem.java
D69_2_AcceptUserInput.java
D69_3_ArraysSimulateStack.java
https://github.com/ruigege66/Java/blob/master/D69_1_HotelManageSystem.java
https://github.com/ruigege66/Java/blob/master/D69_2_AcceptUserInput.java
https://github.com/ruigege66/Java/blob/master/D69_3_ArraysSimulateStack.java
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關註微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,後臺回覆”禮包“,獲取大數據學習資料