7 Replies - 4262 Views - Last Post: 04 April 2012 - 08:42 AM Rate Topic: -----

#1 gliddon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 06-October 11

Supermarket checkout simulation- Scope problem

Posted 03 April 2012 - 08:09 PM

Ok, I'm just a little confused about this program. I'm using a Queue to simulate a grocery store checkout line. Here's some psuedocode.


------------------------------
a) choose a random integer between 1 and 4 to determine the minute at which the first customer arrives

B) At the first customers arrival time, do the following:
determine customers service time(random integer from 1 to 4)
begin servicing the customer
Sched arrival time of next customer (random 1 to 4 dded to currentTime)

c) for each simulated minute of the day, consider the following:
If next customer arrives, proceed as follows:
Say so
Enqueue the customer
schedule arrival time of next customer
If service was completed for the last customer, do the following:
Say so
DeQ next customer to be serviced
Determine customers service completion time(random 1 to 4 added to current time)
--------------------------

Basically I'm thinking that I'll need two customer objects (I have a class which has set/get methods for customer number, arrival time, service time, departuretime and a toString method as well)

I'm confused because when I was testing the theory of how to make sure my thinking was correct, I simply created a small for loop (<10) and created 10 customer and added them into the queue. When I removed the Customer myCustomer = new Customer(); line (initializing a new customer), and put it in the main, I ran into problems with objects existing.

My problem is that I have a for loop, there are different conditions for when customers get arrival times, service length times (and departure times based on that), and these different conditions mess it up somehow if I follow the pseudocode above.

My thinking is that I create a newCustomer object when they arrive, give them a service time, add them to the q.
When service time is complete, dequeue the customer by lastCustomer = q.poll(); i.e creating a separate customer so I can use his data.

am I better off creating a customer with all attributes when I create him and just accessing him later?
Do I need a new customer object to take a customer out of the queue? or can I access the object that is in q.first()?
Where do I need to initialize each customer object in the psuedocode above?? Really confused at this point. Thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: Supermarket checkout simulation- Scope problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Supermarket checkout simulation- Scope problem

Posted 03 April 2012 - 08:18 PM

Lots of questions here:
- you will have to create your Customer object befor insertin them in the Queue
- your Customer class may hold a variable "serviceTime" that is randomly initialized
- you can (and should) extract a Customer object from a Queue without creating a new one
Was This Post Helpful? 0
  • +
  • -

#3 gliddon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 06-October 11

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 02:45 AM

pbl
Posted Yesterday, 08:18 PM
Lots of questions here:
- you will have to create your Customer object befor insertin them in the Queue
- your Customer class may hold a variable "serviceTime" that is randomly initialized
- you can (and should) extract a Customer object from a Queue without creating a new one

So are you saying to use variables through the loop and to only add values to customer object right before adding to Q?
I need it to store different instances of customer object in q, ie cust 1,2,3,4 with different values

Customer class has all the get/set methods it needs

Not positive how to implement extraction, that's y I was thinking Customer extractCustomer = new Customer(); extractCustomer = q.poll();
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 04:09 AM

queue.add(new Customer("John"));
queue.add(new Customer("Paul"));
...

while(true) {
   Customer theOneToProcess = queue.remove();
   if(theOneToProcess == null)
      break;
   ...
}


Was This Post Helpful? 0
  • +
  • -

#5 gliddon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 06-October 11

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 05:57 AM

Yes, no doubt, but the sim runs for 720 iterations and must create customers on the fly, if I had a short list of cutomers that would work, customer gets number, arrival time, service time, maybe departure time to keep things neat, so I can't add a list, it must automate customer creation, which I am still a bit shaky with when attributes are being added in different places at different times (please see psuedocode in first post to clear up what I mean there


View Postpbl, on 04 April 2012 - 04:09 AM, said:

queue.add(new Customer("John"));
queue.add(new Customer("Paul"));
...

while(true) {
   Customer theOneToProcess = queue.remove();
   if(theOneToProcess == null)
      break;
   ...
}


Was This Post Helpful? 0
  • +
  • -

#6 gliddon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 06-October 11

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 06:14 AM

I don't need to add a predefined list of costoners, but that would be too easy ;).

When I was gaming it out I created a loop to initialize a new customer and add it to the queue and do that for ten times, just to test creating objects and storing them in q, that was no problem either, my problem is working with one customer while adding another etc,,, like described in the psuedocode in first post.

When I try that I wind up with a q full of the last customer created, 8 versions of the same thing, rather than the eight different customers I would need to wind up with...
Was This Post Helpful? 0
  • +
  • -

#7 gliddon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 06-October 11

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 07:00 AM

OIC!!!! Sorry I'm at work, I missed the new... That is very possibly what I've been missing, sorry for my stupidity there!
Now I can't wait to get home to try that! Awesome, I'll update later.. Thanks!

View Postpbl, on 04 April 2012 - 04:09 AM, said:

queue.add(new Customer("John"));
queue.add(new Customer("Paul"));
...

while(true) {
   Customer theOneToProcess = queue.remove();
   if(theOneToProcess == null)
      break;
   ...
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 08:42 AM

View Postgliddon, on 04 April 2012 - 09:14 AM, said:

When I try that I wind up with a q full of the last customer created, 8 versions of the same thing, rather than the eight different customers I would need to wind up with...


You will have to show your code so we will better understand your problem
And to create different Customer:

import java.util.Random;

/*
 * Returns a Random firstName lastName by calling:
 * String name = RandomName.get();
 */
public class RandomName {
	private static final Random ran = new Random();
	private static final String[] first = {"John", "Paul", "Andrea", "Peter",
		                                   "Melanie", "Abdul", "Tim", "Kim",
		                                   "William", "Steven", "Carl", "Lynn"};
	private static final String[] last = {"Smith", "Taylor", "Nguyen", "Gordon",
		                                  "St-James", "Connery", "O'Brien", "McLean",
		                                  "Holmes", "Hudson", "Obama"};
	
	static public String get() {
		return first[ran.nextInt(first.length)] + " " + last[ran.nextInt(last.length)];
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1