A.酒店客房管理系统(命令行版本)
* 1.search是查询酒店房间状态的命令。
* 2.in是有客人入住的命令,此时需要提供入住的客户名字和房间号。
* 3.out是客户退房的命令,此时需要提供退房的房间号。
* 4.exit是退出命令。
* 提示:房间状态用一个String类型的二维数组表示即可,里面的元素初始值为" EMPTY".

这两个是老师写的,老师就是老师,比我写有思路多了。。佩服。 :wink:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package day05.hotel_manager;
import java.util.Scanner;
public class HotelRoomManager {
	public static void main(String[] args) {
		String command;
		String[][] rooms=new String[12][10];
		for(int i=0;i<rooms .length;i++){
			for(int j=0;j<rooms[i].length;j++){
				rooms[i][j]="EMPTY";
			}
		}
		Scanner sca=new Scanner(System.in);
		while(true){
			System.out.println("欢迎来到达内酒店,请输入命令:");
			command=sca.next();
			if(command.equals("search")){
				search(rooms);
			}else if(command.equals("in")){
				int roomNo=sca.nextInt();
				String guest=sca.next();
				in(guest,roomNo,rooms);
			}else if(command.equals("out")){
				int roomNo=sca.nextInt();
				out(roomNo,rooms);
			}else if(command.equals("quit")){
				break;
			}else{
				System.out.println("命令错误,请重新输入!");
			}
		}
	}//main() end!
 
	public static void search(String[][] rooms){
		for(int i=0;i<rooms.length;i++){
			for(int j=0;j<rooms[i].length;j++){
				System.out.print((i+1)*100+(j+1)+"\t");
			}
			System.out.println();
			for(int j=0;j<rooms[i].length;j++){
				System.out.print((rooms[i][j].length()>7?rooms[i][j].substring(0,7):rooms[i][j])+"\t");
			}
			System.out.println();
			System.out.println();
		}
	}
 
	public static void in(String guest,int roomNo,String[][] rooms){
		int i=roomNo/100-1;
		int j=roomNo%100-1;
		if(rooms[i][j].equals("EMPTY")){
			rooms[i][j]=guest;
			System.out.println(guest+" 成功入住 "+roomNo+"房间!");
		}else{
			System.out.println(roomNo+"房间已经有客人,请重新输入!");
		}
	}
 
	public static void out(int roomNo,String[][] rooms){
		int i=roomNo/100-1;
		int j=roomNo%100-1;
 
		if(rooms[i][j].equals("EMPTY")){
			System.out.println(roomNo+"没有客人,请重新输入!");
		}else{
			rooms[i][j]="EMPTY";
			System.out.println(roomNo+"退房成功!");
		}
	}
 
	public static boolean testRoomNo(int roomNo){
		return false;
	}
 
}
 
</rooms>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package day06.hotel_management;
 
import java.util.Scanner;
 
public class Hotel {
	private String name;
	private String address;
	private Room[][] rooms;
 
	public Hotel(String name,String address){
		this.name=name;
		this.address=address;
		rooms=new Room[12][10];
		for(int i=0;i<rooms .length;i++){
			for(int j=0;j<rooms[i].length;j++){
				rooms[i][j]=new Room((i+1)*100+(j+1));
			}
		}
	}
 
	public void start(){
		System.out.println(name+"欢迎您!"+"["+address+"]");
		Scanner sca=new Scanner(System.in);
		while(true){
			System.out.print("请输入命令:");
			String comm=sca.next();
			if(comm.equalsIgnoreCase("search")){
				search();
			}else if(comm.equalsIgnoreCase("in")){
				String name=sca.next();
				int id=sca.nextInt();
				in(id,name);
			}else if(comm.equalsIgnoreCase("out")){
				int id=sca.nextInt();
				out(id);
			}else if(comm.equalsIgnoreCase("quit")){
				System.out.println("GOODBYE!");
				break;
			}else{
				System.out.println("错误的命令,请重新输入!");
			}
 
		}
	}
 
	private void search(){
		for(int i=0;i<rooms.length;i++){
			for(int j=0;j<rooms[i].length;j++){
				System.out.print((i+1)*100+(j+1)+"\t");
			}
			System.out.println();
			for(int j=0;j<rooms[i].length;j++){
				System.out.print((rooms[i][j].isEmpty()?"EMPTY":rooms[i][j].getGuestName())+"\t");
			}
			System.out.println();
			System.out.println();
		}
	}
 
	private void in(int id,String name){
		int i=id/100-1;
		int j=id%100-1;
 
		if(rooms[i][j].in(name)){
			System.out.println(name+"入住"+id+"房间!");
		}else{
			System.out.println(name+"入住失败!");
		}
	}
 
	private void out(int id){
		int i=id/100-1;
		int j=id%100-1;
 
		if(rooms[i][j].out()){
			System.out.println(id+"退房成功!");
		}else{
			System.out.println(id+"退房失败!");
		}
	}
 
	public static void main(String[] args){
		Hotel h=new Hotel("达内酒店","杭州下沙经济开发区6号大街23号路口");
		h.start();
	}
 
}
//这里还有一个Room类
 
package day06.hotel_management;
 
public class Room {
	private int id;
	private boolean isEmpty;
	private String guestName;
 
	public Room(int id){
		this.id=id;
		isEmpty=true;
		guestName=null;
	}
 
	public boolean in(String guestName){
		if(isEmpty){
			isEmpty=false;
			this.guestName=guestName;
			return true;
		}else{
			return false;
		}
	}
 
	public boolean out(){
		if(isEmpty){
			return false;
		}else{
			isEmpty=true;
			guestName=null;
			return true;
		}
	}
 
	public String getGuestName() {
		return guestName;
	}
 
	public int getId() {
		return id;
	}
 
	public boolean isEmpty() {
		return isEmpty;
	}
 
 
 
}

>> 若为原创,转载请注明: 转载自Laycher's Blog

>> 本文链接地址: 酒店客房管理系统(命令行版本)(老师写的两个)

>> 订阅本站: http://feed.feedsky.com/laycher



无觅相关文章插件,快速提升流量