java模拟电影院售票机
发布时间: 2023-07-08 09:25:07
㈠ 模拟售票 java 售票窗接口Wicket方法:sellTicket()
interfaceWicket{
Objectobj=newObject();
publicvoidsellTicket();
publicvoidwicketName();
}
,Runnable{
intMovieTicket=30;
intEachticket=10;
publicvoidrun(){
for(;Eachticket>0;Eachticket--)
sellTicket();
}
(){
if(MovieTicket>0&&Eachticket>0)
System.out.println("MovieTicket剩余"+--MovieTicket+""
+Thread.currentThread().getName());
}
publicvoidwicketName(){
}
}
,Runnable{
intBusTicket=30;
intEachticket=10;
publicvoidrun(){
for(;Eachticket>0;Eachticket--)
sellTicket();
}
(){
if(BusTicket>0&&Eachticket>0)
System.out.println("BusWicket剩余"+--BusTicket+""
+Thread.currentThread().getName());
}
publicvoidwicketName(){
}
}
,Runnable{
intTrainTicket=30;
intEachticket=10;
publicvoidrun(){
for(;Eachticket>0;Eachticket--)
sellTicket();
}
(){
if(TrainTicket>0&&Eachticket>0)
System.out.println("TrainTicket剩余"+--TrainTicket+""
+Thread.currentThread().getName());
}
publicvoidwicketName(){
}
}
publicclassWicketDemo{
staticvoidDoubleThread(Runnableoj){
Threadt1=newThread(oj);
Threadt2=newThread(oj);
t1.setName("线程1");
t2.setName("线程2");
t1.start();
t2.start();
}
publicstaticvoidmain(String[]args){
MovieWicketmw=newMovieWicket();
BusWicketbw=newBusWicket();
TrainWickettw=newTrainWicket();
while(mw.MovieTicket!=0||bw.BusTicket!=0||tw.TrainTicket!=0){
longt1=System.currentTimeMillis();
intrand=(int)(Math.random()*3);
if(rand==0&&mw.MovieTicket>0){
System.out.println("正在卖10张电影票!");
WicketDemo.DoubleThread(mw);
mw.Eachticket=10;
}elseif(rand==1&&bw.BusTicket>0){
System.out.println("正在卖10张汽车票!");
WicketDemo.DoubleThread(bw);
bw.Eachticket=10;
}elseif(rand==2&&tw.TrainTicket>0){
System.out.println("正在卖10张火车票!");
WicketDemo.DoubleThread(tw);
tw.Eachticket=10;
}
longt2=System.currentTimeMillis();
if(t2-t1<3000){
try{
Thread.sleep(3000-t2+t1);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
}
刚好我也在学多线程,就拿你的问题练习一下,写出来一坨翔给你参考一下。你的要求应该是已经完全满足了,期待高手优化。
㈡ 初学Java, 在写在线电影购票,想问下 怎么实现买票选座位的功能
这个应该不难,定义一个行列的坐标系,用来标识每个座位,如果有座位的就标位1,没有座位的就标位0,如果此座位票已售出,就设为99(或者其他也行),至于移动,删除等其实就是改变整个坐标系的值了,然后通过图形方式显示出来,移动呀,删除呀什么的操作可以通过拖动等来实现,考虑到电影院中实际的前排与后排座位是有一定的错开的,所以在绘制的时候,要考虑这个错开距离,这样就美观了。
热点内容