Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to calculate the total \'wait time\' and the average wait time for m

ID: 3780278 • Letter: I

Question

I'm trying to calculate the total 'wait time' and the average wait time for my 'restaurant waitlist' application. I can't figure that part out and I've tried to display the total, but it keeps displaying the total for one. I'm using a linkedlist and want it to keep adding as I add people to the waitlist. I also do not know how to display the average as the name is a string and if I do the total/name, it gives me an error. If I parse it to an Int, it gives me an error. Any help would be appreciated. When I click the following button, it should display the running total.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {   
  
Random rand = new Random();
  
String name = txtName.getText();
  
int time = rand.nextInt(30) + 1;
  
int totaltime = time;
  
  
int partySize = Integer.parseInt(txtSize.getText());
  
  
  
txtTotal.setText(String.valueOf(totaltime) + " minutes until your table is ready.");
Highest = partySize;
  
  
String result = " Name: " + txtName.getText() + " Party Size: " + partySize + " Wait Time: " + time + " minutes.";

stack.enqueue(result);
Wait++;
  
}
  
private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {   
String result;
int s;
int size = stack.size();
lstOutput.removeAll();
for (s = 0; s < size; s++) {
result = (String)stack.dequeue();
lstOutput.add(result);
  
}
}


stack is my LinkedList.
LinkedListStack stack = new LinkedListStack();

This is the LinkedListStack

public class LinkedListStack<E> implements QueueInterface<E> {
  
private LinkedList<E> list = new LinkedList<>(); // an empty list
  
public LinkedListStack() {
} // new queue relies on the initially empty list
  
public int size() {
return list.getSize();
}
  
public boolean isEmpty() {
return list.isEmpty();
}
  
public void enqueue(E element) {
list.addLast(element);
}
  
public E first() {
return list.first();
}
  
public E dequeue() {
return list.removeFirst();
}
}

This is my QueueInterface

public interface QueueInterface<E> {
  
int size();
  
boolean isEmpty();
  
// adds an item to the stack
void enqueue(E e);
  
// return but not remove the top item on the stack
E first();
  
// remove item at the top of the stack
E dequeue();
}

And this is the LinkedList I'm using.

public class LinkedList<E> {
private int size;
private Node<E> head;
private Node<E> tail;
  
// default constructor
public LinkedList() {
size = 0;
head = null;
tail = null;
}
  
// read-only property
public int getSize() {
return size;
}
  
public boolean isEmpty() {
// replaces an if/else
return (size == 0);
}
  
// return but not remove head of the list
public E first() {
if ( isEmpty() ) {
return null;
}
else {
return head.getElement();
}
}
  
public E last() {
if ( isEmpty() ) {
return null;
}
else {
return tail.getElement();
}
}
  
public void addFirst(E e) {
// create a new node and make it the new head of the list
head = new Node<>(e, head);
  
if (size == 0) {
tail = head; // special case first item in the list
}
  
size++;
}
  
public void addLast(E e) {
// create a new node and add to the tail of the list
Node<E> newest = new Node<>(e, null);
if (size == 0) { // special case for the first item
head = newest; // now head points to the new node
}
else {
tail.setNext(newest);
}
  
tail = newest;
size++;
}
  
public E removeFirst() {
if (isEmpty() ) {
return null;
}
else {
E tets = head.getElement();
head = head.getNext();
size--;
if (size == 0) {
tail = null; // list is now empty
}
return tets;
}
}
  
  
// nested class
public class Node<E> {
private E element;
private Node<E> next;
  
// custom constructor
public Node(E e, Node<E> n) {
element = e;
next = n;
}
  
// get element
public E getElement() {
return element;
}
  
public Node<E> getNext() {
return next;
}
  
public void setNext(Node<E> n) {
next = n;
}
} // end nested node class

} // end of LinkedList

Explanation / Answer

Now, as per the code given, below are my finding and the updated part in the bold and their explanation in the comments:

private static int totalWaitTime=0;

// There has to be an static total wait counter. You were actually getting the random time and printing the same //time due to which the time for single was occurring.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {   
  
Random rand = new Random();
          
String name = txtName.getText();
  
int time = rand.nextInt(30) + 1;
totalWaitTime+=time; // For every party we generate the time and add the time to get the total time
  
  
int partySize = Integer.parseInt(txtSize.getText());
  
  
  
txtTotal.setText(String.valueOf(totalWaitTime) + " minutes until your table is ready."); // Updated //the variable name for the totalWaitTime to print the total
Highest = partySize;
               int avgWaitTime = totalWaitTime/stack.size(); // avgWaitTime is the variable to hold the average, //it would be the totalwaittime divide by the stack size. Here the stack size is actually the total party waiting.
  
String result = " Name: " + txtName.getText() + " Party Size: " + partySize + " Wait Time: " + time + " minutes.";

stack.enqueue(result);
Wait++;
  
}
  
private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {   
String result;
int s;
int size = stack.size();
lstOutput.removeAll();
for (s = 0; s < size; s++) {
result = (String)stack.dequeue();

// Also you would have to reduce the time when an customer is given its table,so I have split the string and got the //time from it
               txtTotal.setText(result.split(" Wait Time: ").split(" ")[0] + " minutes until your table is ready.");
lstOutput.add(result);
  
}
}

I have tried to not touch your Stack and LinkedList part, just the handler button event code.