1

I have an ArrayList called queue where I store the Customers in a Supermarket as they arrive to the checkout. When they have paid, I remove the first element of the Arraylist (the Customer that was first on the queue), but I don't know how to tell the second one, that now is the first, to proceed to pay. In addition, this Customer should be waiting for an "event".

To sum up, I'm looking for something like a "signal" and "wait".

Here is an outline of what I have:

public class Customer{
    public void checkItems(){
    //Here goes the code for waiting his turn
    //Here goes the code for paying items (already done)
    //The next line is how I remove the client of the queue
    Globals.checkoutList.get(currentCheckout).queue.remove(0);
    Checkout.nextCustomer();
    }
}

public class Checkout{
    public static void nextCustomer(){
    //Here I should tell the next customer that it is his turn
    }
}

Regards and thanks.

EDIT: maybe using Exceptions? Help with exceptions will be appreciated too.

Tito Mc
  • 45
  • 5
  • What you're conceptually talking about is a "queue", a first-in/first-out list. Unfortunately, the available queue implementations in Java is a weird set, so it's hard to pick, but using a LinkedList using `addFirst` and `peekLast`/`removeLast` will probably come close to what you want. There are other queue implementations that have various multi-threading features, if you feel you need that. – Hot Licks May 18 '14 at 20:06
  • (But I'm guessing you don't want to get into threads quite yet.) – Hot Licks May 18 '14 at 20:08
  • By threads are you referring to something like processes in Unix? I'm really new into Java – Tito Mc May 18 '14 at 20:10
  • Yes, "threads" are kinda like processes, only they run inside the current JVM and hence can share the same set of classes and objects. So you could have (if you wished) a separate thread for the cashier and each person in line, and they would be able to "simultaneously" do things. Powerful, but a lot of added complexity. – Hot Licks May 18 '14 at 20:14
  • Buff, too complicated for me at the moment, but appreciated. – Tito Mc May 18 '14 at 20:16

3 Answers3

2

The Customer should not be removing himself from the queue, it is a job of the Checkout or the controlling entity of the Checkout. Also, when you remove the first customer in the list, all others would automatically 'shift up' and you will only ever have to deal with the first element in the checkout queue.

Islay
  • 478
  • 4
  • 17
  • Why shouldn't a Customer remove himself from the queue? I have it stored in another ArrayList that I use to keep statistics – Tito Mc May 18 '14 at 20:09
  • Well, he can in a case where he decides to exit the queue without being processed, but because the removal of a customer depends on him being processed at the checkout, logically speaking the 'checkout lady' decides when the customer is no longer in the queue. – Islay May 18 '14 at 20:11
  • In this case, I'm representing an automatic checkout ;P (It's true) – Tito Mc May 18 '14 at 20:14
1

Why don't you just tell the first index of the list to just checkout now? As far as I'm aware the one that you want to checkout will now be at index 0 as they will be shifted as one is removed.

Jake Arnold
  • 143
  • 11
  • Oh, it was so obvious but I didn't see it. A loop to check if the customer is at the index 0. Thank you. – Tito Mc May 18 '14 at 19:56
  • yep, something like `this==Globals.checkoutList.get(currentCheckout).queue.get(0)` inside an infinite loop, with a break if the condition occurs – Tito Mc May 18 '14 at 20:04
0

Observer Pattern 1.1. Definition

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

The object which is being watched is called the subject. The objects which are watching the state changes are called observers or listeners.

just google JAVA OBSERVER examples

Hector
  • 4,016
  • 21
  • 112
  • 211
  • This is too complicated a solution to implement for a simple task. All Customers do not have to 'observe' anything if they are still far away in the line. On the other hand, adding the second customer in the line as an observer and then removing the status every single time a customer checks out is unnecessary. – Islay May 18 '14 at 20:02
  • this is more complicated than the thing that I'm expected to do, but I will save it for the future, as it looks interesting (I'm just starting with java). Thanks. – Tito Mc May 18 '14 at 20:06