过年了,模拟一个卖火车票

GWISOFT 发布于 2011/01/29 10:37
阅读 358
收藏 2

过年了,模拟一下卖火车票吧,不对的地方请各位多多指点。

package com.saletickets;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 系统主类
 * @author 
 *
 * @version 2011-1-29
 */

public class Main {

	private HashMap soldTicket = new HashMap(); //已售出的票
	private List tickets = new ArrayList();  //未售出的票
	

	/**
	 * 查询票查询参数,查询剩余火车票
	 * @param para
	 * @return
	 */
	public List getUnSaleTicket(TicketSearchPara para){
		return tickets;
	}
	
	/**
	 * 设置火车票
	 * @param tickets
	 */
	public void setTicket(List tickets){
		this.tickets = tickets;
	}

	/**
	 * 根据客户销售火车票
	 * @param para
	 * @param consumer
	 * @return
	 */
	public synchronized Ticket soldTicket(TicketSearchPara para, Consumer consumer){
		List unSaleTickets = getUnSaleTicket(para);
		if (unSaleTickets.size() <= 0){
			return null;
		}
		
		Ticket ticket = unSaleTickets.get(0);
		
		this.tickets.remove(ticket);
		ticket.setIsSale(true);
		this.soldTicket.put(consumer.getIdentityCard(), ticket);
		return ticket;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Main main = new Main();
		
		//生成一天的车票
		List genTickets = new ArrayList();
		for(int i = 0; i < 5; i++){
			Ticket ticket = new Ticket();
			ticket.setIsSale(false);
			ticket.setTicketCode(String.valueOf(i));
			genTickets.add(ticket);
		}		
		main.setTicket(genTickets);
		
		//生成售票窗口
		List saleWindowsList = new ArrayList();
		for(int j = 0; j < 10; j++){
			SaleWindows saleWindows = new SaleWindows(main);
			saleWindows.setWindowsId(String.valueOf(j));
			new Thread(saleWindows).start();
			saleWindowsList.add(saleWindows);
		}
		
		//客流线程
		ConsumerQueue consumerQueue = new ConsumerQueue();
		consumerQueue.setSaleWindowsList(saleWindowsList);
		new Thread(consumerQueue).start();

	}

}


package com.saletickets;

import java.util.ArrayList;
import java.util.List;

/**
 * 售票窗口线程
 * @author 
 *
 * @version 2011-1-29
 */
public class SaleWindows implements Runnable {
	private List consumers = new ArrayList(); //排队的顾客
	private String windowsId; //售票窗口ID
	private Main parent; //线程父类

	public SaleWindows(Main parent){
		super();
		this.parent = parent;
	}
	
	public List getConsumers() {
		return consumers;
	}

	/**
	 * 进出窗口队列方法
	 * @param consumer
	 * @param isIn
	 */
	public synchronized void queue(Consumer consumer, boolean isIn){
		if (isIn){
			consumers.add(consumer);
		}else{
			consumers.remove(consumer);
		}
		
	}
	
	/**
	 * 窗口售票方法
	 * @param consumer
	 * @param para
	 * @return
	 */
	public Ticket soldTicket(Consumer consumer, TicketSearchPara para){				
		Ticket ticket = parent.soldTicket(para, consumer);
		if (ticket == null){
			return null;
		}else{
			this.queue(consumer, false);
			return ticket;		
		}
		
	}

	public String getWindowsId() {
		return windowsId;
	}

	public void setWindowsId(String windowsId) {
		this.windowsId = windowsId;
	}

	public void run() {
		while(true){
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
			if (consumers.size() > 0){
				Consumer consumer = consumers.get(0);
				TicketSearchPara para = new TicketSearchPara(); //车票查询参数类
				List tickets = parent.getUnSaleTicket(para);
				if (tickets.size() > 0){
					Ticket ticket = this.soldTicket(consumer, para);
					if (ticket == null){
						System.out.println("对不起,今天的票已售完,请明天早点来!");
					}else{
						System.out.println("窗口[" + getWindowsId() + "]卖给"
				                + consumer.getIdentityCard() + "一张票,票号为:" + ticket.getTicketCode());
					}					 					
				}else{
					System.out.println("对不起,今天的票已售完,请明天早点来!");
				}
			}
		}
	}

}



package com.saletickets;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 模拟客流量线程
 * @author 
 *
 * @version 2011-1-29
 */
public class ConsumerQueue implements Runnable {

	private List saleWindowsList = null;
	
	
	public List getSaleWindowsList() {
		return saleWindowsList;
	}


	public void setSaleWindowsList(List saleWindowsList) {
		this.saleWindowsList = saleWindowsList;
	}


	public void run() {
		if (saleWindowsList == null){
			System.out.println("当前不存在售票窗口!");
			return;
		}
		
		while(true){
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			Random random = new Random();			
			int b = random.nextInt(2);
			if (b == 1){
				int queueCount = random.nextInt(saleWindowsList.size());
				SaleWindows saleWindows = saleWindowsList.get(queueCount);
				Consumer consumer = new Consumer();
				consumer.setIdentityCard(UUID.randomUUID().toString());
				saleWindows.queue(consumer, true);
				System.out.println("客户[" + consumer.getIdentityCard() 
						+ "]在窗口[" + saleWindows.getWindowsId() + "]进行排队,当前排队人数为:" + saleWindows.getConsumers().size() + "人!");
			}
		}

	}

}



package com.saletickets;

/**
 * 顾客类
 * @author 
 *
 * @version 2011-1-29
 */
public class Consumer {
	private String identityCard;

	public String getIdentityCard() {
		return identityCard;
	}

	public void setIdentityCard(String identityCard) {
		this.identityCard = identityCard;
	}	
}

package com.saletickets;

/**
 * 火车票
 * @author 
 *
 * @version 2011-1-29
 */
public class Ticket {
	private String ticketCode;
	private boolean isSale = false;

	public String getTicketCode() {
		return ticketCode;
	}

	public void setTicketCode(String ticketCode) {
		this.ticketCode = ticketCode;
	}

	public boolean getIsSale() {
		return isSale;
	}

	public void setIsSale(boolean isSale) {
		this.isSale = isSale;
	}
	
	
}

package com.saletickets;

/**
 * 票查询参数类
 * @author 
 *
 * @version 2011-1-29
 */
public class TicketSearchPara {
	
}
加载中
0
szmneo
szmneo

	private HashMap<String, Ticket> soldTicket = new HashMap<String, Ticket>(); // 已售出的票
	private List<Ticket> tickets = new ArrayList<Ticket>(); // 未售出的票

还是这样好用些,呵呵

0
GWISOFT
GWISOFT

引用来自#2楼“szmneo”的帖子

	private HashMap soldTicket = new HashMap(); // 已售出的票
	private List tickets = new ArrayList(); // 未售出的票

还是这样好用些,呵呵

嗯,是的

返回顶部
顶部