-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
196 lines (182 loc) · 5.2 KB
/
Client.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package flight;
import java.util.*;
import java.io.*;
//用户操作界面
public class Client {
private String flightName = null;//航班名
private int row = 0;//座位排数
private int rowLength = 0;//每排座位数
private Flight flight= null;//本次航班对象
private String cmdString = null;//命令串
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//获取控制台命令
private void commandShell() {
System.out.println("\n\n=========================");
System.out.println("type 'exit' command to exit");
while(true) {
readCommand();
processCommand();
}
}
private void processCommand() {
// TODO 自动生成的方法存根
String[] cmds;
String cmd;
if(cmdString != null) {
cmds = command(cmdString);
if(cmdString != null) {
cmd = cmds[0].toLowerCase();
if(cmd.equals("create")) {
if(flightName == null)
createCommand(cmds);
else {
System.out.println("Create Error:can't handle more flights");
}
}else if(cmd.equals("reserve")) {
if(flightName != null)
reserveCommand(cmds);
}else if(cmd.equals("cancel")) {
if(flightName != null)
cancelCommand(cmds);
}else if(cmd.equals("lsit")) {
System.out.println("Thanks.See you later.");
System.exit(0);
}else {
System.out.println("Bad command");
cmdString = null;
}
}
}
}
private void cancelCommand(String[] cmds) {
// TODO 自动生成的方法存根
if(cmds.length != 2) {
System.out.println("\ncancel command format error");
return;
}
int bookingNumber = readInt(cmds[1]);
if(bookingNumber <= 0) {
System.out.println("\ncancel command parameter error");
return;
}
boolean state = flight.cancel(bookingNumber);
if(state)
System.out.println("Your seat has been cancelled");
else
System.out.println("The seat has not been reserved");
}
private int readInt(String valstr) {
// TODO 自动生成的方法存根
int val = 0;
try {
val = Integer.parseInt(valstr);
}catch(Exception e) {
val = Integer.MIN_VALUE;
}
return val;
}
private void reserveCommand(String[] cmds) {
// TODO 自动生成的方法存根
if(cmds.length <= 1) {
System.out.println("reserve command error!");
return;
}
String[] names = new String[cmds.length - 1];
for(int i = 0;i < names.length;i++)
names[i] = new String(cmds[i + 1]);
int[] bn = flight.reserve(names);
if(bn[0] != -1) {
for(int i = 0; i < bn.length;i++)
System.out.println(names[i] + "'s Booking Number is:" + bn[i]);
}else
System.out.println("No Such Sequential Seat Now");
}
private void createCommand(String[] cmds) {
// TODO 自动生成的方法存根
if(cmds.length != 4) {
System.out.println("create command error");
}
else {
flightName = cmds[1];
row = readInt(cmds[2]);
rowLength = readInt(cmds[3]);
if(row <= 0||rowLength <= 0) {
System.out.println("create command parameters error");
flightName = null;
row = 0;
rowLength = 0;
}else {
try {
flight = new Flight(flightName,row,rowLength);
System.out.println("create Flight OK");
}catch(Exception e) {
System.out.println(e);
flight = null;
flightName = null;
row = 0;
rowLength = 0;
}
}
}
}
private String[] command(String cmdStr) {
// TODO 自动生成的方法存根
int cc = 0;
String[] cmd;
StringTokenizer st =new StringTokenizer(cmdStr);
if((cc = st.countTokens()) == 0)
return null;
cmd = new String[cc];
for(int i = 0;i < cc;i++)
cmd[i] = st.nextToken();
return cmd;
}
private void readCommand() {
// TODO 自动生成的方法存根
if(flightName == null) {
System.out.println("*************************");
System.out.println("Please Create The Fligth Data First");
System.out.println("Use command:create fligth_name rows rowLenght<CR>");
System.out.print("\nCOMMAND>");
try {
cmdString = br.readLine().trim();
}catch(IOException e) {
System.out.println("command error");
cmdString = null;
}
}
}
private void listCommand(String[] cmds) {
if(cmds.length != 1) {
System.out.println("\nlist command format error");
return;
}
Passenger[] passengerlist = flight.getPassengerList();
int flag = 0;
System.out.println("Name Booking Number Row Seat Position");
System.out.println("---------------------------------------");
if(passengerlist == null||passengerlist.length <= 0)
System.out.println("Now no seat is occupied");
else {
flag = 0;
for(int b = 0;b < passengerlist.length;b++) {
if(passengerlist[b] != null) {
flag = 1;
System.out.println(formatStr(passengerlist[b].getName()) + formatStr(""+passengerlist[b].getBookingNumber()) + formatStr(""+passengerlist[b].getRow())+formatStr(""+passengerlist[b].getSeatPosition()));
}
}
if(flag == 0)
System.out.println("Now no seat is occupied");
}
}
private String formatStr(String s) {
// TODO 自动生成的方法存根
for(int i = 0;i < 16 - s.trim().length();i++)
s +="";
return s;
}
//主方法,程序入口
public static void main(String[] args) {
// TODO 自动生成的方法存根
new Client().commandShell();
}
}