Supermarket checkout simulation- Scope problem

Page 1 of 1

7 Replies - 11568 Views - Last Post: 04 April 2012 - 08:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273756&amp;s=7421ff1b7cc8e99db26dd9fabfb981e3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 gliddon

Reputation: 2
• 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

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

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

Reputation: 8378
• Posts: 31,956
• 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

#3 gliddon

Reputation: 2
• 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();

#4 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 04:09 AM

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

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

```

#5 gliddon

Reputation: 2
• 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

pbl, on 04 April 2012 - 04:09 AM, said:

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

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

```

#6 gliddon

Reputation: 2
• 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...

#7 gliddon

Reputation: 2
• 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!

pbl, on 04 April 2012 - 04:09 AM, said:

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

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

```

#8 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Supermarket checkout simulation- Scope problem

Posted 04 April 2012 - 08:42 AM

gliddon, 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)];
}
}

```