Question

In Java The fancy new French restaurant La Food is very popular for its authentic cuisine...

In Java

The fancy new French restaurant La Food is very popular for its authentic cuisine and high prices. This restaurant does not take reservations. To help improve the efficiency of their operations, the Maitre De has hired you to write a program that simulates people waiting for tables. The goal is to determine the average amount of time people spend waiting for tables.

Your program will read in a list of event descriptions from a text file, one description per line.

  • Arrival: A party has arrived to eat. Add them to the end of the list of waiting parties (a Queue) and tell them to wait at the bar (where strong drinks are served) until called. This event is described in the following format:

    A t n name

    Here t is the time of arrival (in minutes past opening time), n is the number of people in the party, andname is the name to call when the table is ready.
  • Table: A table has become available; remove the party that has been waiting the longest from your list, and seat them. This event is described in the following form:

    T t

    Here t is the time the table became available (again, in minutes past opening time),
  • Quit: This is a sentinel event indicating the end of the input file. It has the following form:

    Q

When the events in the file have been processed, compute and print the average waiting time per customer. If there are still people waiting for tables, print a summary of who is still waiting.

Sample Data File

Here is a sample data file. You may use this if you like, or you can make up your own.

A 3 3 Merlin
A 8 2 Arthur Pendragon
T 10
A 12 2 Sir Lancelot
T 15
A 17 3 The Green Knight
T 20
Q

Here is the corresponding output from the simulator program. The user's input appears in italics.

*** Welcome to the La Food Restaurant Simulator ***

Enter data file name: data.txt

Please wait at the bar,
  party Merlin of 3 people. (time=3)
Please wait at the bar,
  party Arthur Pendragon of 2 people. (time=8)
Table for Merlin! (time=10)
Please wait at the bar,
  party Sir Lancelot of 2 people. (time=12)
Table for Arthur Pendragon! (time=15)
Please wait at the bar,
  party The Green Knight of 3 people. (time=17)
Table for Sir Lancelot! (time=20)
** Simulation Terminated **

The average waiting time was: 7.28
The following parties were never seated:
  party The Green Knight of 3 people

Have a nice meal!

Modify the A command to include a tip/bribe for the Maitre De. The new format would then be

A t n b name

where b is the amount of the bribe, in Euros, of course. A party is then placed in line ahead of everyone who gave a lesser bribe. Two parties that bribe the same amount are placed in order of arrival.

Modify the input file to include bribes for a sample run, and print the total bribe money collected as part of the final summary.

Note: This is not trivial. Do not start this until the rest is working correctly. You will have to change some aspects of the Queue implementation to get this to work.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE : Please create the data file in same folder as the .java files.

CODE :

import java.util.LinkedList;

import java.util.Queue;

import java.io.File;

import java.util.Scanner;

// Customer class to represent a customer arriving at restaurant

class Customer{

// members to store arrival time, party size and name of customer

int arrival, partySize;

String name;

// Constructor

public Customer(int arrival, int partySize, String name)

{

this.arrival = arrival;

this.partySize = partySize;

this.name = name;

}

// getters for all members

public int getArrivalTime()

{

return arrival;

}

public String getName()

{

return name;

}

public int getSize(){

return partySize;

}

}

// driver code

public class laFood{

public static void main(String[] args){

// introductory output

System.out.println("*** Welcome to La Food Restaurant Simulator ***");

System.out.print("Enter data file name : ");

System.out.println();

try{

String filename = "";

// scanner for IO

Scanner sc = new Scanner(System.in);

// read file name

if(sc.hasNextLine())

{

filename = sc.nextLine();

}

sc.close();

// create file object for current file name

File file = new File(filename);

// scanner object to read from file

sc = new Scanner(file);

// Queue to simulate customer queue

Queue<Customer> q = new LinkedList<>();

// variables to track total waiting time of all customers and

// no of customers served

int totalTime = 0, customersServed = 0;

// read from file, line by line

while(sc.hasNextLine())

{

String line = sc.nextLine();

// split at space to get various attributes from each line

String[] segments = line.split(" ");

// quit when we encounter a Q in file

if(segments[0].equals("Q"))

break;

// if we encounter an A (arrival occurs)

if(segments[0].equals("A"))

{

// extract arrival time, party size and name

int arrival = Integer.parseInt(segments[1]);

int size = Integer.parseInt(segments[2]);

String name = segments[3];

// this would be case, when name contains more than one word

// say a first name and a last name

if(segments.length > 4)

{

for(int i = 4; i < segments.length; i++)

name += " " + segments[i];

}

// print customer arrival to console.

System.out.println("Please wait at the bar, party " + name + " of " + size + " people. (time = " + arrival + " )");

// add customer to queue

q.add(new Customer(arrival, size, name));

}else{

// when we encounter a T, a table is empty

// get the time when customer was served

int serving = Integer.parseInt(segments[1]);

// remove customer from queue

Customer currentCustomer = q.poll();

// add customers waiting time to total waiting time

totalTime += serving - currentCustomer.getArrivalTime();

// increase the count of customers served

customersServed++;

// call the customer's name

System.out.println("Table for " + currentCustomer.getName() + "! (time = " + serving + " )");

}

}

// close scanner to avoid resource leak

sc.close();

// simulation ends

System.out.println("** Simulation Terminated **");

// output the average waiting time

System.out.println("The average waiting time was " + (double)totalTime /customersServed);

// if there are still some customers in queue

if(q.size() > 0)

{

// output all customers that were never served

System.out.println("The following parties were never served :");

// if queue is non empty

while(!q.isEmpty())

{

// pull out a customer and display him/her.

Customer current = q.poll();

System.out.println("party " + current.getName() + " of " + current.getSize() + " people");

}

}

// output good bye remarks

System.out.println("Have a nice meal!");

}catch(Exception e){

e.printStackTrace();

}

}

}

OUTPUT :

NOTE : Since the total waiting time was 22 and 3 customers were served, so the avg. waiting time was 7.33 and not 7.28 as given in question.

For second part, there would be some small chamges and most of code would be same. Since in second part, a customer who gives higher bribe has higher priority, so instead of a queue, we would use a priorioty queue for customers. We would also add a member for bribe to customer class and since we would be giving priority based on bribe we would have to implement Comparable interface for Customer class (to create a priority queue).

Below i have pasted the entire code with changes in bold and any comments underlined and bold.

CODE :

import java.util.PriorityQueue;

import java.io.File;

import java.util.Scanner;

// Customer class to represent a customer arriving at restaurant

class Customer implements Comparable<Customer>{

// members to store arrival time, party size, name, bribe of customer

int arrival, partySize, bribe;

String name;

// Constructor

public Customer(int arrival, int partySize, String name, int bribe)

{

this.arrival = arrival;

this.partySize = partySize;

this.name = name;

this.bribe = bribe;

}

// getters for all members

public int getArrivalTime()

{

return arrival;

}

public String getName()

{

return name;

}

public int getSize(){

return partySize;

}

// This function is used to decide which customer comes first in queue, We compare the current customer

// object (pointed to by this) with another customer object C. We should return negative, zero, or positive depending upon whether this should be placed before, same as or after the Customer C.

public int compareTo(Customer c)

{

// if bribe given by this is greater than given by C, then we return c.bribe - this.bribe which would be negative and hence this would be placed before customer C in queue. If same the order depends on arrival time. We return this.arrival - c.arrival : this ensures that after if c comes after current customer then returned value would be negative and current customer would be placed before c in queue.

// NOTE again : for bribe we return c.bribe - this.bribe and for arrival time : this.arrival - c.arrival

return (c.bribe - this.bribe) == 0 ? this.arrival - c.arrival: c.bribe - this.bribe;

}

}

// driver code

public class laFood{

public static void main(String[] args){

// introductory output

System.out.println("*** Welcome to La Food Restaurant Simulator ***");

System.out.print("Enter data file name : ");

System.out.println();

try{

String filename = "";

// scanner for IO

Scanner sc = new Scanner(System.in);

// read file name

if(sc.hasNextLine())

{

filename = sc.nextLine();

}

sc.close();

// create file object for current file name

File file = new File(filename);

// scanner object to read from file

sc = new Scanner(file);

// Queue to simulate customer queue

PriorityQueue<Customer> q = new PriorityQueue<>();

// variables to track total waiting time of all customers and

// no of customers served

int totalTime = 0, customersServed = 0;

// read from file, line by line

while(sc.hasNextLine())

{

String line = sc.nextLine();

// split at space to get various attributes from each line

String[] segments = line.split(" ");

// quit when we encounter a Q in file

if(segments[0].equals("Q"))

break;

// if we encounter an A (arrival occurs)

if(segments[0].equals("A"))

{

// extract arrival time, party size and name

int arrival = Integer.parseInt(segments[1]);

int size = Integer.parseInt(segments[2]);

int bribe = Integer.parseInt(segments[3]);

String name = segments[4];

// this would be case, when name contains more than one word

// say a first name and a last name

if(segments.length > 5)

{

for(int i = 5; i < segments.length; i++)

name += " " + segments[i];

}

// print customer arrival to console.

System.out.println("Please wait at the bar, party " + name + " of " + size + " people. (time = " + arrival + " )");

// add customer to queue

q.add(new Customer(arrival, size, name, bribe));

}else{

// when we encounter a T, a table is empty

// get the time when customer was served

int serving = Integer.parseInt(segments[1]);

// remove customer from queue

Customer currentCustomer = q.poll();

// add customers waiting time to total waiting time

totalTime += serving - currentCustomer.getArrivalTime();

// increase the count of customers served

customersServed++;

// call the customer name

System.out.println("Table for " + currentCustomer.getName() + "! (time = " + serving + " )");

}

}

// close scanner to avoid resource leak

sc.close();

// simulating ends

System.out.println("** Simulation Terminated **");

// output the total waiting time

System.out.println("The average waiting time was " + (double)totalTime /customersServed);

// if there are still some customers in queue

if(q.size() > 0)

{

// output all customers that were never served

System.out.println("The following parties were never served :");

// if queue is non empty

while(!q.isEmpty())

{

// pull out a customer ans display him/her.

Customer current = q.poll();

System.out.println("party " + current.getName() + " of " + current.getSize() + " people");

}

}

// output good bye remarks

System.out.println("Have a nice meal!");

}catch(Exception e){

e.printStackTrace();

}

}

}

OUTPUT :

data.txt file :

A 3 3 5 Merlin
A 8 2 2 Arthur Pendragon
T 10
A 12 2 7 Sir Lancelot
T 15
A 17 3 4 The Green Knight
T 20
Q

NOTE that even though Arthur Pendragon arrived the earlier than Sir Lancelot, but he is never served because his bribe was least, so he has lowest priority.

Add a comment
Know the answer?
Add Answer to:
In Java The fancy new French restaurant La Food is very popular for its authentic cuisine...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT