'Supermarket Simulator using Queue

enter image description here

I posted this problem earlier and i asked for help on how to approach it. I ended up creating a customer class that generates a random first and last name and also assigns random number of grocery items to a customer every time a new customer object is created.

Here is the code

import java.util.Random;

public class Customer {

    private String lastName;
    private String firstName;
    private int groceryItems;

    private String[] last = { "Jordan", "James", "Bryant", "Bird", "Wade",
            "Bosh", "Griffin", "Durant", "WestBrook", "Anthony" };
    private String[] first = { "Michael", "Lebron", "Kobe", "Larry", "Dwayne",
            "Chris", "Blake", "Kevin", "Russell", "Carmelo" };

    public Customer() {
        Random pickLast = new Random();
        lastName = last[pickLast.nextInt(10)];

        Random pickFirst = new Random();
        firstName = first[pickFirst.nextInt(10)];

        Random pickItems = new Random();
        groceryItems = pickItems.nextInt(25);

    }

    public String getlast() {
        return lastName;
    }

    public String getFirst() {
        return firstName;
    }

    public int getItems() {
        return groceryItems;
    }

    public void display() {
        System.out.println("First Name: " + firstName + " Last Name: "
                + lastName + " Items Purchased: " + groceryItems);
    }

}

CODE FOR QUEUE

public class Queue {

    private int maxSize;
    private int[] queArray;
    private int front;
    private int rear;
    private int nItems;

    public Queue(int s) {
        maxSize = s;
        queArray = new int[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }

    public void insert(int j) {
        if (rear == maxSize - 1)
            rear = -1;

        queArray[++rear] = j;
        nItems++;

    }

    public int remove() {
        int removed = queArray[front++];
        if (front == maxSize)
            front = 0;

        nItems--;

        return removed;
    }

    public int peek() {
        return queArray[front];
    }

    public boolean isEmpty() {
        return (nItems == 0);
    }

    public boolean isFull() {
        return (nItems == maxSize);
    }

    public int size() {
        return nItems;
    }

    public void display() {
        System.out.println("First Inserted Item to Last Inserted Item");

        if (rear < front && (!isEmpty())) {
            for (int i = front; i < maxSize; i++) {
                System.out.println(queArray[i]);
            }

            for (int i = 0; i <= rear; i++) {
                System.out.println(queArray[i]);
            }

        }

        else if (front >= rear && (!isEmpty())) {
            for (int i = front; i <= rear; i++) {
                System.out.println(queArray[i]);
            }
        }

        else {
            System.out.println("Queue is Empty!");
        }

    }

}

Now i was wondering what do i create next? I am just looking for guidance or step by step approach as it will help me learn. Not looking for code. I was thinking that now that i have a customer class i would need to create a Queue that would hold each customer. I know how to write code for a simple int or char queue but i was unclear what the requirements of this particular queue would be , since its holding each customer objects.

Please correct me if i am wrong anywhere in my customer class code and also i would appreciate if someone can provide me steps for the next stage and what i should do .

I am sorry if question is poorly worded as my English is not good and i am certain if i can make this small program working on my own i will have much better understanding of java and Object Oriented programing.



Solution 1:[1]

Now you should add different lineups of people(To simulate different checkout lines). So what I'd do is create an Queue for each checkout line you want. Then, as the problem states, when a key is pressed, it creates a new customer. The customer needs to figure out which line to go to, so maybe assign it to the one with the least amount of total items (or total people if you want to make it easier), and prefers the on closest to, say the left, in the case of more than one shortest queue. Then you need to make the customer buy their items and then leave. If you decide you want to do it at a button press, as the problem suggests, you can set it up so that every time that button is pressed, you call a method in each of the customers (only the ones at the start of the line, with array index 0) that decreases their items by one, and deletes the customer object from the Queue when it reaches zero, to simulate them leaving and another customer taking its place. Now you need to show these things to the user somehow. Perhaps just a simple "[Customer Name] Joined this line. Current line ups are: Line 1: 2 people Line 2: 5 people" etc, and a "[Customer Name] left this line after buying their groceries."

I hope that this is what you wanted, and I hope it helps.

Solution 2:[2]

Try this approach:

import java.util.Scanner;
import java.util.Random;

class CirQueue {
    private
        int max,rear,front;
        int[] a;
        int size;
    
    public
        CirQueue(int m) {
          max = m;
          size = 0;
          a = new int[m];
          rear = front = -1;
        }
    
        void insert(int val) {
            if(isFull()) {
                System.out.println("CUSTOMER CANNOT BE ADDED AS QUEUE IS FULL");
            } else {
                if(isEmpty()) {
                    front = rear = 0;
                    a[rear] = val;
                } else {
                    rear = (rear+1)%max;
                    a[rear] = val;
                }
                size++;
            }
        }

        int size() {
            return size;
        }

        void decFront(int rmv) {
            a[front]-=rmv;
            if (a[front] <= 0) {
                delete();
            }
        }
        
        void display() {
            if(!isEmpty()) {
                int i;
                for(i=front; i!=rear; i=(i+1)%max) {
                    System.out.print(a[i] + " ");
                }
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }

        boolean isFull() {
            if(front == (rear+1)%max) return true;
            else return false;
        }

        boolean isEmpty() {
            if(size == 0)  return true;
            else return false;
        }

        int delete() {
            if(isEmpty()) {
                System.out.println("Underflow");
                return -1;
            } else {
                int temp = a[front];
                if(rear==front) {
                    rear = -1;
                    front = -1;
                } else {
                    front = (front+1)%max;
                }
                size--;
                return temp;            }
        }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 drdavehere
Solution 2 schlenger