飛機訂票小系統

package org.cctv1;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test8 {
 
 public static void main(String[] args) {
  Scanner s = new Scanner(System .in);
  Air air = new Air(5);
  while(air.number>1){
   System.out.println("請訂票:");
   String str = s.nextLine();
   D1 d = new D1(str,air,-1);
   d.start();
  }
 }

}
class Air{
 int number;
 
 public Air(int number){
//  this.name = name;
  this.number = number;
//  this.place = place;
 }
 public synchronized void f(int a ) throws InterruptedException{
  Thread.sleep(1000);
  number+=a;
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日   hh:mm:ss");
  String str = sdf.format(new Date());
  System.out.println(Thread.currentThread().getName()+"訂了一張飛往美國的機票"+"剩餘"+number+"張票        "+str);
  System.out.println(".....................................");
 }
}
class D1 extends Thread{
 Air air = null;
 int b;
 public D1(String name, Air air, int b){
  super(name);
  this.air =air;
  this.b = b;
 }
 @Override
 public void run() {
  try {
   air.f(b);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
}

 

 

 

 

 

 

 

 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章