一、编写一个酒店管理系统
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.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料
原文:https://www.cnblogs.com/ruigege0000/p/12164447.html