Results 1 to 2 of 2
  1. #1
    herooftime is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default a problem with queues

    Hello everyone

    here's my problem, I have 3 waiting lines ( 3 queues ) to finishing the registration of the courses at lab in an university
    first queue will be outside of the lab.. when a student arrives at lab
    the second queue will be when that student enters to the lab to take a laptop ( from stack ) to register his courses ( the student will enter to the lab in the same time of arrival ), the student needs a minute to talk to the manager to take the laptop. if 3 students arrive in the same time 12:45 PM, the first will receive his laptop at 12:46 PM, next at 12:47 PM, the last one at 12:48 PM. if there's no available laptop at the moment.. the student is going to wait till a laptop replaced ( someone who finished his registration ) .. and the registration process takes 5 minutes.
    the third queue will be the waiting line for returning the laptop, after finishing the registration .. the student will enter to this line and he's going to wait a minute to return that laptop ( if he finished his registration at 12:51, he will enter to the last waiting line and return that laptop at 12:52)

    my code for this assignment is working perfect with the waiting condition ( no available laptop at the moment ), but with the normal condition.. the output is messed up

    the code:
    Java Code:
     int time = outsideLine.peek().getEnterTime(); // the time that student will take the laptop ( plus a minute to talk to the manager )
                int maintime = outsideLine.peek().getEnterTime(); // timing for the loop
    
                int size = students; // number of students
    
     while (size > 0) {
    
    
    // timeEnter: the time that student will enter to the lab
    // timeRemaining: time to finish the  registration ( if a student takes a laptop at 12:45 .. the timeReaming will be 2:50 PM (5 minutes )
    // timeRegistred: to return the laptop
    // laptop: stack of 5 laptops
    
    // when the return line is not empty
     if (returnLine.peek() != null) {
                        if (returnLine.peek().getTimeRegistered() == maintime) {
                            FCITstudent ob = returnLine.dequeue();
    
                            System.out.println(hour(ob.getTimeRemaining()) + ":" + minute(ob.getTimeRemaining()) + " PM:  " + ob.getFirstName() + " " + ob.getLastName() + " finished registering and entered the Laptop Return Line.");
                            System.out.println(hour(ob.getTimeRegistered()) + ":" + minute(ob.getTimeRegistered()) + " PM:  " + ob.getFirstName() + " " + ob.getLastName() + " successfully registered and returned laptop # " + ob.getLaptopSerialNumber() + ".");
    
    
                            laptop.push(ob.getLaptopSerialNumber());
                            linkedList.insert(ob); // never mind
                            size--;
                        }
                    }
    
                    // when the waiting line for taking the laptop isn't empty
                    if (waitingLine.peek() != null) {
    
                        // if the first student receives his laptop at 12:45 PM, he will be dequeued from this queue.. then check the next one if his timeEnter bigger than the time of the previous student .. for example, the time is 12:45 PM, the next student will enter to the lab at 12:50 PM, the time must be modified here .... ( I think my fault is here )
                        if (waitingLine.peek().getEnterTime() > time) {
                            time = waitingLine.peek().getEnterTime();
                        }
    
                        // if laptops are not available
                        if (laptop.getTop() == -1) {
                            time += 2;
                        } else {
                            if (waitingLine.peek() != null) {
                                if (time + 1 == maintime) { // time plus a mintue needed to talk to the manager
    
    
                                    FCITstudent ob = waitingLine.dequeue();
                                    ob.setTimeRemaining(++time + 5);
                                    ob.setTimeRegistered(ob.getTimeRemaining() + 1);
                                    ob.setLaptopSerialNumber(laptop.pop());
                                    System.out.println(hour(time) + ":" + minute(time) + " PM:  " + ob.getFirstName() + " " + ob.getLastName() + " has checked-out laptop # " + ob.getLaptopSerialNumber() + ".");
                                    returnLine.enqueue(ob);
    
    
                                }
                            }
                        }
                    }
    
    
    
                    if (outsideLine.peek() != null && outsideLine.peek().getEnterTime() == maintime) {
                        FCITstudent ob = outsideLine.dequeue();
    
                        System.out.println(hour(ob.getEnterTime()) + ":" + minute(ob.getEnterTime()) + " PM:  " + ob.getFirstName() + " " + ob.getLastName() + " has arrived at the Registration Lab and entered the Laptop Check-out Line.");
    
                        waitingLine.enqueue(ob);
                    }
    
    
                    // if students arrive at same time 
                    if (outsideLine.peek() != null) {
                        if (outsideLine.peek().getEnterTime() == maintime) {
                            continue;
                        }
                    }
    
    
    
                    
                    maintime++;
    
    
                }
    as i mentioned above.. the code works perfectly for the waiting condition, for example .. these enter times ( the time is between 12 PM to 5 PM only:
    256, 256, 256, 257, 257, 257, 258, 258, 258, 258
    255, 255, 255, 256, 257, 257, 258, 258, 258, 259

    and not working with this:
    45, 49, 68, 73, 81, 98, 164, 208, 263, 297

    for some reason i don't know.. the output is correct up to ( 164 ) , after that it gets back to ( 98 )
    i think my fault is that condition:
    Java Code:
     if (waitingLine.peek().getEnterTime() > time) {
                            time = waitingLine.peek().getEnterTime();
                        }
    Last edited by herooftime; 11-24-2013 at 11:17 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default Re: a problem with queues

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help converting from recursion to queues
    By Samir4021 in forum New To Java
    Replies: 0
    Last Post: 05-03-2012, 06:56 AM
  2. Implementing a Stack Using two Queues
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 11:28 AM
  3. Queues
    By A.M.S in forum New To Java
    Replies: 3
    Last Post: 12-23-2009, 11:00 PM
  4. Help with Queues in Java
    By Java01 in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 04:56 PM
  5. Replies: 1
    Last Post: 08-07-2007, 08:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •