Climbing的专栏
Life is limited, but art is long
hpdlzu80100
登录
注册
全站
当前博客
空间
博客
好友
相册
留言
huang Climbing
ID:hpdlzu80100
[修改头像]
共
1564
次访问,排名
2万外
好友
5
人,关注者
6
人
热爱生活,积极向上,流自己的汗,吃自己的饭!
hpdlzu80100的文章
原创 23 篇
翻译 0 篇
转载 2 篇
评论 0 篇
最近评论
软件项目交易
订阅我的博客
文章分类
IT Industry and Career
(RSS)
Java Learning
(RSS)
Temp
(RSS)
收藏
相册
存档
2008年05月(1)
2008年04月(24)
简单航班订票模拟系统(Airline Reservation Sysytem, a simple simulation)
新一篇: 掷双骰结果统计(Show statistical data of Craps (Rolling of two dice))
//
Airline Reservation System, a simple simulation
//
Java how to program, 5/e, Exercise 7.18
import
javax.swing.
*
;
import
java.awt.
*
;
import
java.awt.event.
*
;
public
class
AirlineReservation
extends
JApplet
implements
ActionListener
{
int
seat[]
=
new
int
[
10
];
boolean
seatStatus[]
=
new
boolean
[
10
];
int
seatNumber,classType;
JLabel classTypeLabel;
JTextField classTypeField;
JTextArea output;
int
FirstClassCounter
=
3
,EconomyCounter
=
7
;
public
void
init()
{
Container container
=
getContentPane();
container.setLayout(
new
FlowLayout());
classTypeLabel
=
new
JLabel(
"
Please type 1 for First Class and 2 for Economy:
"
);
container.add(classTypeLabel);
classTypeField
=
new
JTextField(
10
);
container.add(classTypeField);
classTypeField.addActionListener(
this
);
output
=
new
JTextArea();
container.add(output);
for
(
int
i
=
0
;i
<
seat.length;i
++
)
seat[i]
=
i
+
1
;
}
public
void
actionPerformed (ActionEvent event)
{
classType
=
Integer.parseInt(classTypeField.getText());
if
(classType
==
1
&&
FirstClassCounter
>
0
)
{
for
(
int
i
=
0
;i
<
3
;i
++
)
{
if
(seatStatus[i]
!=
true
)
{
seatNumber
=
seat[i];
seatStatus[i]
=
true
;
FirstClassCounter
--
;
output.append(
"
Thanks! Your have reserved a First Class seat and got a boarding pass.
"
+
"\n
Your seat number is:
"
+
seatNumber
+
"\n
"
);
break
;
}
}
}
else
if
(classType
==
1
&&
FirstClassCounter
<=
0
&&
EconomyCounter
>
0
)
{
output.append(
"
Sorry, all the First Class seats have been reserved.\n
"
+
"
You can type 2 to reserve an Economy one instead! \n
"
);
}
else
if
(classType
==
2
&&
EconomyCounter
>
0
)
{
for
(
int
i
=
3
;i
<
10
;i
++
)
{
if
(seatStatus[i]
!=
true
)
{
seatNumber
=
seat[i];
seatStatus[i]
=
true
;
EconomyCounter
--
;
output.append(
"
Thanks! Your have reserved an Economy seat and got a boarding pass.
"
+
"\n
. Your seat number is:
"
+
seatNumber
+
"
\n
"
);
break
;
}
}
}
else
if
(classType
==
2
&&
EconomyCounter
<=
0
&&
FirstClassCounter
>
0
)
{
output.append(
"
Sorry, all the Economy seats reserved.
"
+
"\n
You can type 1 to reserve an First Class one instead!\n
"
);
}
else
if
(FirstClassCounter
<=
0
&&
EconomyCounter
<=
0
)
{
output.append(
"
Sorry, all the seats have been reserved.
"
+
"\n
Next flight leaves in 3 hours.\n
"
);
}
}
}
发表于 @
2008年04月16日 17:51:00
|
评论(
loading...
)
|
编辑
旧一篇: 汉诺塔迭代算法(Towers of Hanoi, classic problem (recursive method))
评论:没有评论。
发表评论
姓 名:
主 页:
校验码:
看不清,换一张
登录