本帖最后由 不喝茶的陆羽 于 2013-6-7 23:46 编辑
class Ticket{
private static int ticketNum;
public int getTicketNum() {
return ticketNum;
}
public Ticket(int ticketNum){
this.ticketNum=ticketNum;
}
public static boolean available(){
if(ticketNum>0){
return true;
}
else{
return false;
}
}
public static void sealTicket(String winName){
System.out.println(winName+"-----恭喜您,您已完成票务购买-----");
System.out.println("还剩余"+(--ticketNum)+"张票");
}
}
class SealWindow implements Runnable {
private String winName;
private Ticket ticket;
SealWindow(String winName,Ticket ticket){
this.winName=winName;
this.ticket=ticket;
}
public void run(){
System.out.println("欢迎来到"+winName+"售票窗口,很高兴为您服务");
synchronized(ticket){
while(ticket.available()){
Ticket.sealTicket(winName);
}
}
System.out.println("-----票已卖完,下次发售时间:明早8:00,欢迎届时购买-----");
}
}
public class Test7 { //此处的Test7应该为TicketSealCenter,但是因为考试要求中的类名规范,特此说明
public static void main(String[] args) {
//售票中心 分配售票窗口 和总票数
// 生产票 送到售票中心
Ticket ticket = new Ticket(1000);
//建立 3个售票窗口
SealWindow sw1 = new SealWindow("售票窗口一",ticket);
SealWindow sw2 = new SealWindow("售票窗口二",ticket);
SealWindow sw3 = new SealWindow("售票窗口三",ticket);
new Thread(sw2).start();
new Thread(sw1).start();
new Thread(sw3).start();
}
}
急等各位大神回复啊~
|
|