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 10 of 10

Thread: Please Help (Queues)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help (Queues)

    Hi!
    so i'm trying to make a program with a customer class that keeps how many minutes each customer needs to be served. The driver class CustomerQueue is a queue of all customers that haven't been served yet. Each iteration of the program is one minute, and there is a 25% chance of a new customer entering the queue each minute. Each customer takes 1-5 minutes to be completely served ad removed from queue.

    My Code:

    import java.util.Queue;
    import java.util.LinkedList;
    import java.util.Random;
     
    public class CustomerQueue
    {
    	public static void main(String[] args) 
    	{
    		int chance;
    		Queue<Customer> customerQueue = new  LinkedList<Customer>();
     
    		//Simulating 60 minutes
    		for (int i = 0; i<60; i++)
    		{
    			//25% chance of a new customer, to do this:
    			//get a random number from 1-4, if you get 4, you add a customer
    			Random rand = new Random();
    			chance = rand.nextInt(4)+1;
     
    			if(chance == 4)
    			(
    				Customer newCustomer = new Customer();
    				customerQueue.add(newCustomer);
    				system.out.println("A new customer just entered the queue.\nTotal number of customers in the Queue is: "+customerQueue.size());
    			)
     
    			//decrease a minute from the firs customer and check if he's fully servieced
    			if(customerQueue(0).timeLeft() == 0)
    			(
    				customerQueue.remove();
    				System.out.println("Customer serviced and removed from queue.\nQueue lenght is now: "+customerQueue.size());
    			)
     
    			System.out.println("-------------------one minute elapsed---------------------------");
     
    		}
    	}
    }

    import java.util.Random;
     
    public class Customer
    {
    	private int serviceTime;
     
    	public void customer()
    	{
    		Random rand = new Random();
    		serviceTime = rand.nextInt(5)+1;
    	}
     
    	private int timeLeft()
    	{
    		serviceTime =-1;
    		return serviceTime;
    	}
     
     
    }




    I'm having a problem adding and removing Customer objects from the queue with these errors:
    CustomerQueue.java:22: error: ')' expected
    Customer newCustomer = new Customer();
    ^
    CustomerQueue.java:25: error: illegal start of expression
    )
    ^
    CustomerQueue.java:30: error: ')' expected
    customerQueue.remove();
    ^
    CustomerQueue.java:29: error: not a statement
    (
    ^
    CustomerQueue.java:32: error: illegal start of expression
    )
    ^
    5 errors


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Please Help (Queues)

    if(chance == 4)
    			( << WRONG
    				Customer newCustomer = new Customer();
    				customerQueue.add(newCustomer);
    				system.out.println("A new customer just entered the queue.\nTotal number of customers in the Queue is: "+customerQueue.size());
    			) << WRONG

    You're using parenthesis where there should be curly brackets. All conditional blocks use curly brackets over parenthesis.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (Queues)

    oops!
    thanks for that. It compiles now but i get an exception when running it. could you take a look at the new code?

    import java.util.Queue;
    import java.util.LinkedList;
    import java.util.Random;
     
    public class CustomerQueue
    {
    	public static void main(String[] args) 
    	{
    		int chance;
    		Queue<Customer> customerQueue = new  LinkedList<Customer>();
     
    		//Simulating 60 minutes
    		for (int i = 0; i<60; i++)
    		{
    			//25% chance of a new customer, to do this:
    			//get a random number from 1-4, if you get 4, you add a customer
    			Random rand = new Random();
    			chance = rand.nextInt(4)+1;
     
    			if(chance == 4)
    			{
    				Customer newCustomer = new Customer();
    				customerQueue.add(newCustomer);
    				System.out.println("A new customer just entered the queue.\nTotal number of customers in the Queue is: "+customerQueue.size());
    			}
     
    			//decrease a minute from the firs customer and check if he's fully servieced
    			customerQueue.peek().minutePassed();
     
    			//check if customer fully served
    			if(customerQueue.peek().timeLeft() == 0)
    			{
    				customerQueue.remove();
    				System.out.println("Customer serviced and removed from queue.\nQueue lenght is now: "+customerQueue.size());
    			}
     
    			System.out.println("time left for first customer: "+customerQueue.peek().timeLeft());
    			System.out.println("-------------------one minute elapsed---------------------------");
     
    		}
    	}
    }
    import java.util.Random;
     
    public class Customer
    {
    	private int serviceTime;
     
    	public Customer()
    	{
    		Random rand = new Random();
    		serviceTime = rand.nextInt(5)+1;
    	}
     
    	public void minutePassed()
    	{
    		serviceTime =-1;
    	}
     
    	public int timeLeft()
    	{
    		return serviceTime;
    	}
     
     
    }

    am I using the peek method of the class correctly? I'm not sure how to update the first customer object in the queue or see the timeLeft in it. I also tried to create another Customer variable and assigning the first Customer object to it and then getting the timeLeft and using minutePassed but I get the same error.
    Please help?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please Help (Queues)

    Quote Originally Posted by husain2213 View Post
    ...It compiles now but i get an exception when running it. ....
    You should *always* also post the full text of the exception and indicate by an obvious comment on the code which line(s) throw the exception. Please do this as this will greatly help us to figure out exactly what is wrong. Also, part of what you have to learn is how to interpret these exception stack traces. It's not hard to do once you've had a little practice.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (Queues)

    Exception in thread "main" java.lang.NullPointerException
    at CustomerQueue.main(CustomerQueue.java:28)

    that's the exact exception. it points to this line:
    customerQueue.peek().minutePassed();

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please Help (Queues)

    Quote Originally Posted by husain2213 View Post
    Exception in thread "main" java.lang.NullPointerException
    at CustomerQueue.main(CustomerQueue.java:28)

    that's the exact exception. it points to this line:
    customerQueue.peek().minutePassed();
    So what is null? consider changing this:
    customerQueue.peek().minutePassed();

    to this:
    // add before checking the minutes passed:
    System.out.println("customerQueue is null: " + (customerQueue == null));
    System.out.println("customerQueue.peek() is null: " + (customerQueue.peek() == null));
     
    // then try to get the minutes passed:
    customerQueue.peek().minutePassed();

    and letting us know what you see.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (Queues)

    customerQueue is null: false
    customerQueue.peek() is null: true
    Exception in thread "main" java.lang.NullPointerException
    at CustomerQueue.main(CustomerQueue.java:30)

    is this happening because there might be no Customers in the queue? if that's the case, how can I keep the program running even if there are no customers?

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please Help (Queues)

    Quote Originally Posted by husain2213 View Post
    customerQueue is null: false
    customerQueue.peek() is null: true
    Exception in thread "main" java.lang.NullPointerException
    at CustomerQueue.main(CustomerQueue.java:30)

    is this happening because there might be no Customers in the queue? if that's the case, how can I keep the program running even if there are no customers?
    Yep, that's what the Queue API states:
    Returns the head of this queue, or null if this queue is empty
    You could call peek and check that it's not null before proceeding:
    Customer cust = customerQueue.peek();
    if (cust != null) {
       // do check the minutes passed here
       cust.minutesPassed();
       if (cust.timeLeft == 0) {
         //...
       }
    }

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help (Queues)

    Thanks a lot,, its working now just needs some tweaking
    really appreciate the help

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please Help (Queues)

    You're quite welcome! Glad you've got it working.

Similar Threads

  1. Help with queues
    By araujo3rd in forum Collections and Generics
    Replies: 2
    Last Post: March 10th, 2010, 11:20 AM
  2. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM