为什么我的优先级队列没有排序

时间:2014-11-09 18:16:28

标签: java priority-queue

//在我的优先级队列上打印出来,并且在添加事件时不排序     ----------

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.*;
    public class Shell_Project_4 {

            /**
             * this program simulates a restaurant drive thru
             *`enter code here`
             * @param args the command line arguments
             */
            public static void main(String[] args) throws FileNotFoundException {
                SimClock clock = new SimClock();
                double Miat = 0.90, Ttoi = 0.10, Mttp = 0.45, Ttpi = 0.20, endTime = 240.0;

                // mean order size, Distance between order and payment station, Distance between payment and food pick up station
               final int Mos = 4, Dop = 3, Dpf = 2;
                File output = new File("simulationResults.txt");
                PrintWriter results = new PrintWriter(output);

                // the main queue that holds the events which holds the customers
                **PriorityQueue<event> masterQueue = new PriorityQueue();**
    // the order line
                Queue<Customer> OrderStation = new LinkedList<>();
    // the payment window line
                Queue<Customer> PaymentStation = new LinkedList<>();
    // the food collection window line
                Queue<Customer> FoodCollection = new LinkedList<>();
        //information used to work the project
                int cNum = 0; // customer number
                EventType ea = EventType.ARRIVAL; // arrival event type
                EventType efo = EventType.FINISHORDER;  // event finish order           
                EventType efp = EventType.FINISHPAYMENT; //event finish payment            
                EventType effc = EventType.FINISHFOODCOLLECTION; // event food collection

                //arrival time and sTime is shopping time
                double aTime, sTime;

                // the loop that creates 5 customers and puts them into an arrive event and the arrive event goes into the Queue, ignore the for loop, i will add different loop
                for (int i = 0; i < 5; i++) {
// this method gets a random number around the passed number
                    aTime = getExponential(Miat);
//aTime is arrival time, endTime is close time
                    if (aTime <= endTime) {
                        // creates the customer
                        Customer person = new Customer(Mos, Mttp, cNum);
                        // creates the arrive event
                        event arrive = new event(aTime, person, ea);
                        // adds to the master Queue
                        masterQueue.add(arrive);
                        cNum++;
                    }
                }
 // i added the array.sort here and it still does not sort by the time of the event which was added
              Arrays.sort(masterQueue.toArray());
// prints the elements of the Queue
                for (event e : masterQueue) {
                    System.out.println(e);
                }
}


        ----------
// this class is the event class that has the customers, and their information including the time the event type is over
        public class event implements Comparable<event> {

            private double time; // time the event is over
            private Customer cus; // customer
            private EventType et; // event type

        // no arg contructor
            public event() {
            }


            public event(Double time, Customer person, EventType type) {
                this.time = time;
                cus = person;
                et = type;
            }
// this is supposed to the compareTo method that supposed to sort the priority Queue "masterQueue"  so when the event is added it gets the time and compares it, it is supposed to sort it from shortest time to the longest
           @Override
           public int compareTo(event o) {

                if (time > o.getTime()) {
                    return 1;
                } else if (time < o.getTime()) {
                    return -1;
                } else {
                    return 0;
                }
            }


            public double getTime() {
                return time;
            }**strong text**
}

0 个答案:

没有答案