【Java学习笔记】二维数组模拟酒店管理系统
来源:互联网 发布:知满天孙景民 编辑:程序博客网 时间:2024/04/25 12:25
首先创建一个房间类Room
package com.longya.HOTEL;public class Room{ private int no; private String type; private boolean occupancy; public Room(int no, String type, boolean occupancy) { this.no = no; this.type = type; this.occupancy = occupancy; } //重写toString()——按照规定格式输出房间信息 public String toString() { String str = occupancy ? "占用" : "空闲"; return "[no=" + no + ",房型=" + type + ",状态=" + str + "]"; } //setter/getter public int getNo(){ return no;} public void setNo(int no){ 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;}}
然后,创建一个旅馆类Hotel
package com.longya.HOTEL;public class Hotel{ Room[][] rooms; public Hotel(){ rooms = new Room[5][9]; //默认创建5层楼,每层9个房间 //给所有房间编号 for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { /* 房间分布设置:1楼、2楼为标准间,3楼为单人间,4楼为情侣间,5楼为豪华间 */ if (i == 0 || i == 1) { rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准间", false); }else if (i == 2) { rooms[i][j] = new Room((i + 1) * 100 + j + 1, "单人间", false); }else if (i == 3) { rooms[i][j] = new Room((i + 1) * 100 + j + 1, "情侣间", false); }else if (i == 4) { 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) { System.out.println(); //每行显示三个房间的信息 } System.out.print(rooms[i][j] + "\t"); } System.out.println(); } } //订房 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("订房成功!您预定的房间编号为:" + 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 + "]房间!"); return; } } } } //根据房间编号,获取房间的占用情况occupancy 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(); } } } return false; }}
最后,创建一个测试类HotelTest
package com.longya.HOTEL;import java.util.*;public class HotelTest{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("-------------------------------------------------- 酒店管理系统 -------------------------------------------------"); Hotel hotel = new Hotel(); hotel.display(); //显示所有房间信息 while(true) { try { //订房 System.out.println(); System.out.print("请输入您想要入住的房间编号:"); int no = sc.nextInt(); if (hotel.getRoomOccupancy(no)) { System.out.println("该房间已经有人入住!请重新预定!"); }else{ hotel.reserve(no); hotel.display(); } //退房 System.out.println(); System.out.print("请输入您想要退房的房间编号:"); no = sc.nextInt(); if (hotel.getRoomOccupancy(no)) { hotel.checkout(no); hotel.display(); }else{ System.out.println("该房间原本就无人入住!请重新输入!"); } } catch (Exception e) { System.out.println("输入数据有误!请重新进入系统!"); return; } } }}
运行结果如下:
至此程序结束!
阅读全文
0 0
- 【Java学习笔记】二维数组模拟酒店管理系统
- 纯数组,实现简单模拟酒店管理系统
- 酒店管理系统(java)
- 酒店管理系统web版/Java酒店管理系统
- Java二维数组【Java学习笔记】
- 酒店管理系统java代码下载
- 基于java web的酒店管理系统
- Java JDK 5.0学习笔记(二维数组)
- java学习笔记(十三)二维数组
- 黑马程序员之项目开发学习笔记:酒店管理系统(HotelManager)
- Java实现酒店管理系统之Java swing酒店管理系统的实现及源码下载
- 酒店管理系统
- 酒店管理系统
- 酒店管理系统心得
- 酒店管理系统数据库
- 酒店管理系统
- 酒店管理系统
- php酒店管理系统
- hdu3635 P
- HDU4635 Strongly connected【强连通】
- 小ho的01串 [字符串]
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
- 色彩天气APP-MVP初体验
- 【Java学习笔记】二维数组模拟酒店管理系统
- 2017.08.15【NOIP提高组】模拟赛B组总结
- python学习入门
- ycb的ACM进阶之路(背包优化)
- 树状数组 (从下标0计数)
- manacher回文串算法
- js练习题(2)
- 寄存器在内存单元寻址组合和div/mul指令笔记
- html的转义字符