Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How can I make multiple Person Queues when user defined?

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I make multiple Person Queues when user defined?

    Hello, I need help with making multiple Person Queues and then adding "Person" into a Person Queue with the least amount of "Persons". I'm not sure how I'd go about making a new person with certain parameters and then adding them to a PersonQueue which i'll be referencing to. Hopefully someone can point me in the right direction. This was given as an assignment. So any parameters or classes that are blank are just part of the structure of the project.






     
    import java.util.Scanner;
    import java.util.*;
     
    class Person {
        int arrivalTime;
        int departureTime;
        int processingTime;
     
        Person (int minArrivalTime, int maxArrivalTime, int minProcessingTime, int maxProcessingTime) {//creates a person
            generateArrivalTime(minArrivalTime, maxArrivalTime);
            generateProcessingTime(minProcessingTime, maxProcessingTime);
            System.out.print("\nCreated person with arrival time " + arrivalTime + " processing time "+ processingTime);
            this.departureTime = this.arrivalTime + this.processingTime;
     
     
     
     
        }
     
        private void generateArrivalTime (int minArrivalTime, int maxArrivalTime){
     
            this.arrivalTime = (int)Math.random() * maxArrivalTime + minArrivalTime;
        }
        private void generateProcessingTime(int minProcessingTime, int maxProcessingTime){
     
            this.processingTime = (int)Math.random() * maxProcessingTime + minProcessingTime;
     
        }
     
        public int getProcessingTime(){
     
            return this.processingTime;
        }
        public int getArrivalTime(){
            return this.arrivalTime;
        }
     
     
     
     
    }
    class Event {
     
        String type;
        int time; //this will be either the arrival or departure 
        int param; //arrival = processing time, if departure = checkout line that had the departure 
     
     
     
    }
    class PersonQueue {
     
        int totalTime;
        int numOfCustomers;
     
     
     
    }
    class EventPQueue {
     
     
     
    }
    class Store {
     
        PersonQueue[] mypq; //this is the person queue where I need to add each "Person" into the shortest Person Queue (the amount of PersonQueues (registers) is user defined) 
        EventPQueue myeq; //Priority Queue puts all events into a priority
     
        int totalNumberOfQueue;
     
     
     
        private int numOfCustomers;
     
        Store (int checkoutLines, int numOfCustomers, int minArrivalTime, int maxArrivalTime, int minProcessingTime, int
               maxProcessingTime) {
            this.numOfCustomers = numOfCustomers;
            createPersons(numOfCustomers, minArrivalTime, maxArrivalTime, minProcessingTime, maxProcessingTime); //calls the create Person method
     
        }
     
        void createPersons(int numOfCustomers, int minArrivalTime, int maxArrivalTime, int minProcessingTime, int maxProcessingTime){
     
            for (int i = 0; i < numOfCustomers; i++){
                 new Person(minArrivalTime, maxArrivalTime, minProcessingTime, maxProcessingTime); //makes a new Person passing along the parameters in order to make its arrival time and departure time
     
            }
        }
    }
     
     
    public class Driver {
        public static void main(String[] args){
     
        Scanner input = new Scanner(System.in);
     
        System.out.print("\nEnter number of checkout lines: ");
        int checkoutLines = input.nextInt(); //amount of Person Queues
     
        System.out.print("\nEnter number of customers: ");
        int numOfCustomers = input.nextInt(); //amount of persons needed to be created
     
        System.out.print("\nEnter the min time between arrivals: ");
        int minArrivalTime = input.nextInt();
     
        System.out.print("\nEnter the max time between arrivals: ");
        int maxArrivalTime = input.nextInt();
     
        System.out.print("\nEnter the min processing time for a person: ");
        int minProcessingTime = input.nextInt();
     
        System.out.print("\nEnter the max processing time for a person: ");
        int maxProcessingTime = input.nextInt();
     
        Store simulation = new Store(checkoutLines, numOfCustomers, minArrivalTime, maxArrivalTime, minProcessingTime, maxProcessingTime);
     
        }
    }


    A sample simulation would be: (The time arriving of a customer is randomly chosen after the user chooses the min arrival time and max arrival time. same with departure time).
    Time Activity
    0 Customer Arrives (processing time 7) - Customer Joins Queue 1 //this customer would depart at time = 7
    2 Customer Arrives (processing time 8) - Customer Joins Queue 2 //this customer would depart at time = 10
    7 Customer Departs Queue 1
    9 Customer Arrives (processing time 8) - Customer joins Queue 1
    10 Customer Departs Queue 2
    Last edited by kinggio; December 18th, 2017 at 06:18 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    Please fix the code tags by adding []s:
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    Done, thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    making multiple Person Queues
    and then adding "Person" into a Person Queue
    Can you use existing Java SE classes to do the work of the queue? For example the PersonQueue could define an ArrayList internally to hold the queue's contents. Then it would be up to you to define what methods the PersonQueue class would need and then design and write the code for them.
    Adding a Person object to an instance of the PersonQueue would simply be done by calling one of the class's methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    I'm really not sure how I'd go about doing this. I've thought about this idea, but still not sure what i'd do and how I'd reference to it when adding a customer.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    First decide what the PersonQueue class would do.
    Make a list of the features the class will need to have and write them down.
    Then look at what the class would need to do to implement each of those features.
    Pick one feature and work on the code for that.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    Thanks! That really helped, but now I'm encountering a different issue having to do with an OutOfBounds Exception. Can you take a look at my code? (I'll try replying to this thread with it)

    --- Update ---

    class Store {
     
        PersonQueue[] mypq;
        EventPQueue myeq;
        int totalTime;
        int totalNumberOfQueue;
        int totalDelay;
        ArrayList<PersonQueue> listOfQueues = new ArrayList<>();
     
     
        private int numOfCustomers;
     
        Store (int checkoutLines, int numOfCustomers, int minArrivalTime, int maxArrivalTime, int minProcessingTime, int
               maxProcessingTime) {
            this.numOfCustomers = numOfCustomers;
            createPersons(numOfCustomers, minArrivalTime, maxArrivalTime, minProcessingTime, maxProcessingTime);
            createRegisters(checkoutLines, numOfCustomers);
        }
     
        void createPersons(int numOfCustomers, int minArrivalTime, int maxArrivalTime, int minProcessingTime, int maxProcessingTime){
     
            for (int i = 0; i < numOfCustomers; i++){
                 add(new Person(minArrivalTime, maxArrivalTime, minProcessingTime, maxProcessingTime));
     
            }
        }
     
     
         void createRegisters(int checkoutLines, int numOfCustomers){
            for (int i = 0; i < checkoutLines; i++){
                listOfQueues.add(new PersonQueue(numOfCustomers));
            }
         }
         void add(Person x){
     
             listOfQueues.get(shortestLine(listOfQueues)).enquePerson(x);
     
         }
     
        public int shortestLine(ArrayList<PersonQueue> x){
            int min = x.get(0).customerCount;
            int shortestLine = 0;
            for(int i=1;i < x.size(); i++){
                if(x.get(i).customerCount < min){
                    min = x.get(i).customerCount;
                    shortestLine = i;
                }
            }
            return shortestLine;
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I make multiple Person Queues when user defined?

    issue having to do with an OutOfBounds Exception
    Can you copy the full text of the error message and paste it here so we can see where the exception occurs and what the values were?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. To creating user defined packages
    By vishvaD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 1st, 2014, 03:40 PM
  2. User Defined Criteria
    By SimonTemplar in forum Java Theory & Questions
    Replies: 0
    Last Post: February 20th, 2013, 07:02 PM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 10:23 PM
  4. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM
  5. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM

Tags for this Thread