编写一个程序模式超市的收款队列。可以用上机作业4.1的display()方法,显示出顾客的几条队列。可以通过敲击一个键插入一个新的顾客。
为顾客选在哪一个队列上。收银员为每个顾客服务的时间是随机的(可假定为按照顾客买了多少东西而定)。一旦结完账,就从队列中删除该顾客。为了简单起见,通过敲击键模拟时间的流逝。可能每点击一下键表示时间过去了1分钟。下面是我写的程序,有A,B,C,D四个队列,当输入A,B,C,D其中一个时,就等于为该队列添加一名顾客,然后会打印出当前4个队列的排队情况。用一个数字0代表一个人。但是    /*收银员为每个顾客服务的时间是随机的(可假定为按照顾客买了多少东西而定)。一旦结完账,就从队列中删除该顾客。为了简单起见,通过敲击键模拟时间的流逝。可能每点击一下键表示时间过去了1分钟。*/上面的要求怎么才能做到呢,请高手请教,想不出思路啊。麻烦了,谢谢。import java.io.*;
class Queue
   {
   private int maxSize;
   private long[] queArray;
   private int front;
   private int rear;
   private int nItems;
//--------------------------------------------------------------
   public Queue(int s)          // constructor
      {
      maxSize = s;
      queArray = new long[maxSize];
      front = 0;
      rear = -1;
      nItems = 0;
      }
//--------------------------------------------------------------
   public void insert(long j)   // put item at rear of queue
   {
      if(nItems==maxSize) {
       System.out.println("数族已满,不能在插入数据项");
      }
      else if(rear == maxSize-1) {        // deal with wraparound
        rear = -1;
        queArray[++rear] = j;         // increment rear and insert
    nItems++;                     // one more item   
      }
  else {
   queArray[++rear] = j;          // increment rear and insert
    nItems++;                       // one more item  
  }                     
   }
//--------------------------------------------------------------
   public long remove()         // take item from front of queue
      {
      long temp = queArray[front++]; // get value and incr front
      if(front == maxSize)           // deal with wraparound
         front = 0;
      nItems--;                      // one less item
      return temp;
      }
//--------------------------------------------------------------
 public void display() {
   int count = nItems;
   int i = front;
   while(count>0) {
     System.out.print(queArray[i++] + " ");
     count--;
     if(i==maxSize) {
       i = 0;
     }
   }
   System.out.println();
 }
//--------------------------------------------------------------
   public long peekFront()      // peek at front of queue
      {
      return queArray[front];
      }
//--------------------------------------------------------------
   public boolean isEmpty()    // true if queue is empty
      {
      return (nItems==0);
      }
//--------------------------------------------------------------
   public boolean isFull()     // true if queue is full
      {
      return (nItems==maxSize);
      }
//--------------------------------------------------------------
   public int size()           // number of items in queue
      {
      return nItems;
      }
//--------------------------------------------------------------
   }  // end class Queue
////////////////////////////////////////////////////////////////
class QueueApp {
  public static void main(String[] args) throws IOException {
   Queue A = new Queue(10);
   Queue B = new Queue(10);
   Queue C = new Queue(10);
   Queue D = new Queue(10);
  
    String input;
    while(true) {
     System.out.println("Add a shopper at: ");
     System.out.println("please enter A or B or C or D");
     System.out.flush();
     input = getString();
     if(input.equals("A")) {
     A.insert((long)Math.random());
     }
     if(input.equals("B")) {
     B.insert((long)Math.random());
     }
     if(input.equals("C")) {
     C.insert((long)Math.random());
     }
     if(input.equals("D")) {
     D.insert((long)Math.random());
     }
     A.display();
     B.display();
     C.display();
     D.display();
    }
  }
   
  public static String getString() throws IOException {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    return s;
  }   

解决方案 »

  1.   

    使用Timer组件记时可以不呢?
    再用一个线程监视各个队列,到了设定时间就操作
      

  2.   

    通过敲击键盘来模拟时间流程只是一种方法,当然可以通过随机数来模拟,个人理解啊//伪代码
    int keyDownCount;//通过敲击键模拟时间的流逝。可能每点击一下键表示时间过去了1分钟。 int timeSpend;while(input!=end){ keyDownCount++;
    }timeSpend = keyDownCount*1min;//当然可以更简单,我认为两个变量直观一点;那么timeSpend就是用户接受服务的时间了,敲击次数不同,时间也就不同可以假设顾客一进商场就确定了接受服务的时间(这与临时确定时间的效果一样,当然意义不同,我只是为了简便),因为各个收银台之间是独立的,所以可以使用线程来解决同时有顾客付账的问题。如下是我的代码:(时间紧迫代码拙劣请见谅)import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Date;
    import java.util.LinkedList;public class PayingBills { private LinkedList<Customer> a ;
    private LinkedList<Customer> b ;
    private LinkedList<Customer> c ;
    private LinkedList<Customer> d ;

    private final long timeToSleep = 50;

    public PayingBills(){

    a = new LinkedList<Customer>();
    b = new LinkedList<Customer>();
    c = new LinkedList<Customer>();
    d = new LinkedList<Customer>();
    }


    public class EnterMall {

    String readFromScreen( String s){
                //if failed return ""

    System.out.print(s);

    BufferedReader stdIo = new BufferedReader( new InputStreamReader( System.in ) );

    try {



    return stdIo.readLine();

    } catch (IOException e) {

    //e.printStackTrace();
    System.err.println("Wrong input");

    return  "";
    }

    }

    void addCustomer( LinkedList<Customer> t){
    //add customer to the special list

    Customer temp = new Customer();

    temp.setName( readFromScreen( "Please Enter name of the Customer: " ) );

    t.addLast(temp);

    //在这里确定接受服务的时间比较方便
    }

    void enterMall(){
    //sim customer enter mall
    String s = "inilization";

    while(!"end".equals(s)){//stop until meet enter key 

    s = readFromScreen( "Please enter a/b/c/d to choose which queue: " );

    if("a".equals(s)) addCustomer(a);
    if("b".equals(s)) addCustomer(b);
    if("c".equals(s)) addCustomer(c);
    if("d".equals(s)) addCustomer(d);
    }

    //-------------------------------------------------------------------------
    System.out.println("out of enter mall in entermall Entermall  PayingBills");
    //-------------------------------------------------------------------------
    }

    public void run(){
    enterMall();
    }

    }// end of EnterMall

    public class OutMall extends Thread{

    private LinkedList<Customer> temp;

    public OutMall( LinkedList<Customer> temp ){

    this.temp = temp;
    }

    void outMall(){

    if(temp.isEmpty()){

    try {
    this.currentThread().sleep( timeToSleep );

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }else{

    System.out.println( temp.pop().getName() );
                                    //在这里确定因为是线程,有点麻烦 }

    }

    public void run(){

    while(true){

    outMall();
     
    }//while
    }
    }//end of OutMall

       public static void main(String[] args){
       
       PayingBills pb = new PayingBills();
       
       EnterMall em = pb.new EnterMall();
       
       em.enterMall();
       
       OutMall am = pb.new OutMall( pb.getA() );
       OutMall bm = pb.new OutMall( pb.getB() );
       OutMall cm = pb.new OutMall( pb.getC() );
       OutMall dm = pb.new OutMall( pb.getD() );
       
       am.start();
       bm.start();
       cm.start();
       dm.start();
       
       
       }
        
       public LinkedList<Customer> getA() {

       return a;
       }
       public LinkedList<Customer> getB() {

       return b;
       }   public LinkedList<Customer> getC() {

       return c;
       }
       public LinkedList<Customer> getD() {

       return d;
       }
    }