QuestionQuestion

Problem Statement:
This assignment involves modeling a group of customers and a service-counter on a typical day from 9am to 5pm, using queues (implemented as LinkedList using Generic Types, you must have attended the recitations before this assignment was assigned)
Different customers come to the service counter at different times to get some service (get their gifts wrapped, say), and assume for simplicity that the service time per customer is some constant T.

The service is first-come first-serve.

Given as input a group of customers along with an arrival time for each customer, the kinds of queries that your program should be able to answer are:

• How long will a given Person wait?
• How many customers in that group got served on that typical day?
• What is the total idle time of the employee (the person behind the service counter)?
• How long is the longest break that the employee has?
• What is the longest that the waiting line got, measured in the number of people waiting, not including the person being served? (Note that if a new person arrives right at the exact time when the person at the front of the line is called for service, both the new person and the person being called for service are included.)

Definition: A break is the period of time between the end of serving one person and the beginning of serving the next person. If the next person is right there, waiting in the queue, the break length is 0. Note that if the last person in the group is done before 5pm, then the employee will have a last break extending from the time the last person is done until 5pm. Over the span of the day, the employee may end up having several breaks, or no break at all. The employee does not take any break while serving a customer of if there is a person waiting to be served.

Definition: The total idle time of the employee is the sum of the lengths of the breaks that the employee ends up having.

The precise formatting of the queries and answers for your program is detailed below.

The Input:
Your program will take two input files from the command prompt.
The first file, called customersfile.txt, has the customers’ information, such that each customer has one "paragraph" of two lines, with at least one blank line between one paragraph and the next. The format of the paragraph of a customer is:

ID-NUMBER: a unique integer customer id ARRIVAL-TIME: hh:mm:ss

For example, for a person arriving 15 minutes and 27 seconds after 9am, the arrival time is 9:15:27; for a person arriving 17 minutes 35 seconds after 10am, the arrival time is 10:17:35; and for a person arriving 3:30sharp, the arrival time is 3:30:0.

The first line of customersfile.txt is an unsigned positive integer, representing the constant service time per customer, in seconds. After that line there is at least one blank line. After that, the 2-line paragraphs follow.

Note that some customers may arrive before 9am or after 5pm. Those who are not served before 5pm are dismissed without service. If the last person served begins to get service before 5pm and the service time will take the clock beyond 5pm, s/he is served completely before the employee goes home. The waiting time of a person that arrives after 5pm is 0. The waiting time of a person that arrives before 5pm but does not get served is the time from his/her arrival until 5pm.

You are guaranteed that the arrival times of the customers in customersfile.txt are in chronological order. That is, the arrival time of a customer A is greater than the arrival times of all the customers that occur before A in the input file. Also, you are guaranteed that all customers ids are positive, and no customer id occurs multiple times in the input file.

The second input file, called queriesfile.txt, will have a sequence of queries, one query per line, where the queries can be:

WAITING-TIME-OF customer-id // measured in seconds NUMBER-OF-CUSTOMERS-SERVED LONGEST-BREAK-LENGTH // measured in seconds TOTAL- IDLE-TIME // measured in seconds MAXIMUM-NUMBER-OF-PEOPLE-IN-QUEUE-AT-ANY-TIME
Note that there could be many queries beginning with WAITING-TIME-OF but ending with different id numbers.

The output:
Your program must take as input the two input files above, in that order. That is, we should be able to compile your program into an executable file (called Program2, say), then call it from the command-line like this:

>> Program2 customersfile.txt queriesfile.txt
(Research how to accept text files as arguments in Eclipse)

Your program must then output as many lines as there are query lines: the kth output line should be the answer to the kth query line in file queriesfile.txt. Your output line answer to a query should begin by repeating the exact same query, followed by a colon (:), followed
by the query-answer.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {
       if (args.length != 2) {
            System.out.println("Program2 customersfile.txt queriesfile.txt");
            return;
       }
       Queue<CustomerInfo> customers = new QueueLinkedList<>();
       // read customersfile.txt
       int serviceTime = readCustomerFile(args[0], customers);

       if (serviceTime < 0) {
            System.out.println("Invalid service time");
            return;
       }

       int currentTime = CustomerInfo.from12AM("9:0:0");
       int nextTime = currentTime;
       int closeTime = CustomerInfo.from12AM("17:0:0");

       int breakTime = 0;
       int longestBreakTime = 0;
       int idleTime = 0;
       int max_queue_length = 0;
       // process the list;

       Queue<CustomerInfo> waitingQueue = new QueueLinkedList<>();
       Queue<CustomerInfo> servedQueue = new QueueLinkedList<>();

       // work until closing time
       while (customers.empty() == false
                && currentTime <= closeTime) {
            while (customers.empty() == false
                   && currentTime >= customers.peek().getTime()) {
                // get break time
                breakTime = customers.peek().getTime() - nextTime;
                if (breakTime > 0) {
                   idleTime += breakTime;
                   // get longest break time
                   if (breakTime > longestBreakTime) {
                        longestBreakTime = breakTime;
                   }
                }
                // push customter to waiting queue
                waitingQueue.enqueue(customers.dequeue());
            }

            // get max number in the waiting queue
            if (max_queue_length < waitingQueue.size()) {
                max_queue_length = waitingQueue.size();
            }

            // serve a customer
            if (waitingQueue.empty() == false
                   && nextTime <= currentTime) {
                CustomerInfo info = waitingQueue.dequeue();

                // calculate the waiting time
                int waiting_time = currentTime - info.getTime();
                info.setWaitingTime(waiting_time);
                // sete the time to serve the next customer
                nextTime = currentTime + serviceTime;
                // try to finish all tasks at closeing time
                if (nextTime > closeTime) {
                   nextTime = closeTime;
                }
                servedQueue.enqueue(info);
            }
            if (currentTime < closeTime) {
                currentTime++;
            }
       }
      
       // do the rest of work when there isn't new customer
       while (currentTime < closeTime
                && waitingQueue.empty() == false) {
            while (waitingQueue.empty() == false
                   && nextTime <= currentTime) {
                CustomerInfo info = waitingQueue.dequeue();
                int waiting_time = currentTime - info.getTime();
                info.setWaitingTime(waiting_time);
                nextTime = currentTime + serviceTime;
                if (nextTime > closeTime) {
                   nextTime = closeTime;
                }
                servedQueue.enqueue(info);
            }
            currentTime++;
       }...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$38.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats