用数组来做个酒店管理系统

酒店房间管理系统需求

根据以前所学习的知识实现一-个酒店房间管理系统,可以提供预定房间、退房的功能。酒店一共有5层,每一层有10个房间,其中:

1, 2层是标准间

3,4层是双人间

5层是豪华间

在编写该系统之前先来学习一个新知识,如何获取用户的输入?

public class dd08 {

/**

*大如何获取用户输入的数据

*

*/

public static void main (String[] args) {

Scanner s=new Scanner (System. in) ;

String userInput = s.next() ; //程序执行到这里时会等待用户的输入

System. out.println ("您输入了:"+ userInput);

}

}


需求分析

首先需要定义一个Room类,里面存储房间编号,房间类型,是否空房

其次需要定义一个Hotel类,里面存储所有房间的基本信息。里面提供预定、退房、查看房间状态的方法。

最后需要定义一-个Test类,用来接收用户的输入,从而提供房间服务。


编码实现

定义一个Room类

package demo04;

public class Room {

//房间编号

private String id;

//房间类型

private String type;

//是否空房

private boolean isUse;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

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 Room(String id, String type, boolean isUse) {

super();

this.id = id;

this.type = type;

this.isUse = isUse;

}

public Room(){

}

@Override

public String toString() {

return "[" + id + "," + type + "," + (isUse?"占用":"空闲") + "]";

}

}


编码实现

定义一个Hotel类

package demo04;

public class Hotel {

Room[][] room;

//初始化房间编号和类型

public Hotel() {

room=new Room[5][10];

//1,2标准间101,102......110

//3,4双人间

//5豪华间

for(int i=0;i<room.length;i++){

for(int j=0;j<room[i].length;j++){

if(i==0||i==1){

room[i][j]=new Room((i+1)*100+j+1+"","双人间",false);

}

if(i==2||i==3){

room[i][j]=new Room((i+1)*100+j+1+"","双人间",false);

}

if(i==4){

room[i][j]=new Room((i+1)*100+j+1+"","双人间",false);

}

}

}

}

//查看房间状态

public void print() {

for(int i=0;i<room.length;i++){

for(int j=0;j<room[i].length;j++){

System.out.print(room[i][j]+" ");

}

System.out.println();

}

}

//预订房间

public void order (String id) {

for(int i=0; i<room.length;i++){

for(int j=0; j<room[i].length;j++){

if(room[i][j].getId().equals(id)){

//将房间状态改成true已被预订

room[i][j].setUse(true);

}

}

}

}

//退房

public void checkout (String id) {

for(int i=0; i<room.length;i++){

for(int j=0; j<room[i].length;j++){

if(room[i][j].getId().equals(id)){

//将房间状态改成false空房

room[i][j].setUse(false);

}

}

}

}

}

编码实现

定义一个Test类

package demo04;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner s=new Scanner(System.in);

System.out.println("欢迎使用酒店管理系统");

Hotel h=new Hotel();

h.print();

while(true){

System.out.println("请输入退房还是预订");

String order=s.next();//接受用户的输入指令

if("预订".equalsIgnoreCase(order)){

System.out.println("请输入房间编号");

String id=s.next();

h.order(id);

}else if("退房".equalsIgnoreCase(order)){

System.out.println("请输入房间编号");

String id=s.next();

h.checkout(id);

}else{

System.out.println("请输入退房或预订");

}

h.print();

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,724评论 0 3
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,909评论 0 2
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 747评论 0 0
  • 第四天 数组【悟空教程】 第04天 Java基础 第1章数组 1.1数组概念 软件的基本功能是处理数据,而在处理数...
    Java帮帮阅读 1,614评论 0 9
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,857评论 0 6