Question

In: Computer Science

You will design a program to keep track of a restaurants waitlist using a queue implemented...

You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251

1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables.

2. Add the following operations to your program: a. Return the first person in the queue b. Return the last person in the queue c. Add a person to the queue d. Delete a person from the queue

3. Create a main program to test your class. Your main program should contain the following options.

a. Add a guest (adds the reservation name and number of guests)

b. Delete a guest (the user must give you the name to delete, the list may be empty or the guest may not be in the list)

c. Show last guest waiting (return NONE if the queue is empty) d. Show first guest waiting (return NONE if the queue is empty) e. Exit

She is requiring Stacks and Queues. There are various ways to do this code, but I am really unfamiliar with this one.  

Solutions

Expert Solution

Hi, please find the solution. Explanations embedded inline. Thanks.

Main program to test.

package com.company;

public class Main {

    public static void main(String[] args) {
   Person p1 = new Person("Name 1",5);
   Person p2 = new Person("Name 2",4);
   Person p3 = new Person("Name 3",6);
   Person p4 = new Person("Name 4",3);
   Person p5 = new Person("Name 5",7);
   Person p6 = new Person("Name 6",5);
   Person p7 = new Person("Name 7",34);
   Person p8 = new Person("Name 8",2);
   Person p9 = new Person("Name 9",1);

   WaitList w = new WaitList(p1);
   w.addPerson(p2);
   w.addPerson(p3);
   w.addPerson(p4);
   w.addPerson(p5);
   w.addPerson(p6);
        System.out.println("The first person waiting is "+w.showLastGuestWaiting());
        System.out.println("The persn who is getting a serve now is "+w.returnFirstPerson());
        System.out.println("The just arrived person is "+w.returnLastPerson());

    }
}
package com.company;

import java.util.Objects;

public class Person {
    String reservationName;
    int noOfGuests;

    @Override
    public String toString() {
        return reservationName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return getReservationName().equals(person.getReservationName());
    }

    public Person next;

    public Person(String reservationName, int noOfGuests) {
        this.reservationName = reservationName;
        this.noOfGuests = noOfGuests;
    }

    public String getReservationName() {
        return reservationName;
    }

    public void setReservationName(String reservationName) {
        this.reservationName = reservationName;
    }

    public int getNoOfGuests() {
        return noOfGuests;
    }

    public void setNoOfGuests(int noOfGuests) {
        this.noOfGuests = noOfGuests;
    }
}
package com.company;

public class WaitList {
    public static Person front=null;

    WaitList(Person p) {
        if(p==null)return;
        if (front == null) {
            front = p;
        } else if (front != null) {
            Person temp=front;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = p;
            p.next=null;
        }
    }

    public void addPerson(String name, int noOfGuests) {
        Person p = new Person(name, noOfGuests);
        functionalAddPerson(p);
    }

    public void addPerson(Person p){
        if(p==null)return;
        functionalAddPerson(p);
    }

    private void functionalAddPerson(Person p) {
        if (front == null) {
            front = p;
        } else {
            p.next = front;
            front = p;
        }
    }

    public Person returnFirstPerson() {
        //here the first person means who came first to the restaurant, meaning last node in the linked list.
        return getFirstWaitingPerson(true);//true means delete the person who came first
    }

    private Person getFirstWaitingPerson(boolean toBeDeleted) {
        Person toReturn = null;
        if (front == null) {
            return null;
        } else if (front.next == null) {
            toReturn = front;
            if (toBeDeleted) {
                front = null;
            }
            return toReturn;
        } else {
            Person temp = front;
            while (temp.next != null) {
                if (temp.next.next == null) {
                    toReturn = temp.next;
                    if (toBeDeleted) {
                        temp.next = null;
                    }
                    return toReturn;
                }
                temp = temp.next;
            }
        }

        return toReturn;
    }

    public Person returnLastPerson() {//this means who has just registered his entry
        Person toReturn;
        if (front == null) {
            return null;
        } else if (front.next == null) {
            toReturn = front;
            front = null;
            return toReturn;

        } else {
            toReturn = front;//returning the just inserted person
            front = front.next;//making the front point to the second object so that the first can be removed.
            toReturn.next = null;//removing assignments from the linked list.
            return toReturn;
        }
    }

    public Person showLastGuestWaiting() {//here we have to get to the end of the queue and not delete the person from the queue
        return getFirstWaitingPerson(false);//we return the person without deleting him.
    }

}

Following images (output)show that the queue was implemented using Linked list.

Sample output:

You can also see in the below screenshot that the name1 is missing since he is got a serve, after the call of returnFirstPerson method.

In the above screen shot "Name 1" is missing since he got a serve and removed from the queue. He came first hence got serve. Thanks


Related Solutions

Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
C++ Modify this to use a separate Boolean member to keep track of whether the queue...
C++ Modify this to use a separate Boolean member to keep track of whether the queue is empty rather than require that one array position remain empty. #include <stdio.h> #include <stdlib.h> #include <limits.h> // A structure to represent a queue struct Queue { int front, rear, size; unsigned capacity; int* array; }; // function to create a queue of given capacity. // It initializes size of queue as 0 struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*)...
You are tasked to design an application to keep track of sales for your company. Sales...
You are tasked to design an application to keep track of sales for your company. Sales should be tracked for two types of accounts: supplies and services. Complete the following:     Create a UML class diagram for the Account inheritance hierarchy. Your subclasses should be Supplies and Services.     All sales accounts will have an account ID (accountId).     You will need attributes to keep track of the number of hours (numberOfHours) and rate per hour of services provided (ratePerHour)....
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
You are going to create a console based program to keep track of a small business...
You are going to create a console based program to keep track of a small business that sells Doodads. First you will need to create a class Doodad that keeps track of two integers and two Strings. Next, create a constructor for the Doodad. Next, add getters and setters for each of the fields (two integers and two Strings). You need to use Doodad.java (see the starter code) Inside your main method ask the user to read in the two...
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
In this assignment, the program will keep track of the amount of rainfall for a 12-month...
In this assignment, the program will keep track of the amount of rainfall for a 12-month period. The data must be stored in an array of 12 doubles, each element of the array corresponds to one of the months. The program should make use of a second array of 12 strings, which will have the names of the months. These two arrays will be working in parallel. The array holding the month names will be initialized when the array is...
Design a database through the EER diagram to keep track of the teams and games of...
Design a database through the EER diagram to keep track of the teams and games of a sport league. Assume that the following requirements are collected (the English description of cardinal ration and partial/complete participate is NOT required, but you still need to provide the total/partial and cardino ration in your EER diagram) : The database has a collection of TEAM. Each Team has a unique name, players, and owner. The database also keeps the records of PLAYERS. Each player...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT