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(或者其他也行),至於移動,刪除等其實就是改變整個坐標系的值了,然後通過圖形方式顯示出來,移動呀,刪除呀什麼的操作可以通過拖動等來實現,考慮到電影院中實際的前排與後排座位是有一定的錯開的,所以在繪制的時候,要考慮這個錯開距離,這樣就美觀了。
熱點內容