Supermarket simulator using queue

enter image description here

I posted this issue earlier and I asked for how to handle it. I ended up creating a customer class that generates a random first and last name and also assigns a random number of grocery items to the 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 am I creating next? I'm just looking for a guide or a step-by-step approach as it will help me learn. Don't look for code. I thought that now that I have a client class, I need to create a queue that will contain each client. I know how to write code for a simple int or char queue, but I didn't understand what the requirements of this particular queue would be, since it holds every client object.

Please correct me if I am wrong anywhere in my client class code and also I would appreciate if someone can provide me with the steps for the next step and what should I do.

I apologize if the question is poorly formulated as my English is not very good and I am sure I can make this little program on my own, I will have a much better understanding of Java and object oriented programming.

+3


source to share


1 answer


Now you have to add different groups of people (simulate different validation lines). So what I would do is create a queue for each check line you want. Then when the problem is that a new client is created when a key is pressed. The client needs to figure out which line to go to, so maybe assign it to the one with the least common items (or total people if you want to make it easier) and prefers the closest, say in the case of more than one short queue. Then you need to get the customer to buy their products and then leave. If you decide you want to do this on a button click, then as the problem shows, you can set it up so that every time you click that button, you call a method on each of the clients (only those that were at the beginning of the line,with array index 0), which decrements their elements by one and removes the client object from the queue when it reaches zero to simulate holding them and another client takes its place. Now you need to show these things to the user in some way. Maybe just a simple "[Customer Name] joined this line. Current line up: Line 1: 2 people. Line 2: 5 people" etc., A "[Customer Name] left this line after purchasing their products" ...5 people "etc., A" [Customer Name] left this line after purchasing their products. "5 people "etc., A" [Customer Name] left this line after purchasing their products. "



I hope this is what you wanted and I hope this helps.

+2


source







All Articles