In: Computer Science
Java please. No "hard coding" please. Need to ask for the file and load the sample file information into the class.
There is a complete theory about how queues work. In this problem create a limited model to study the order in which a bunch of customers will be attended by their cashiers on a supermarket. The conditions for the experiment are:
• Each cashier spends the same amount of time with each customer (this is just an exercise, not real life).
• There will be a defined number of queues but never less than 2 and never more than 5.
• There is a variable number of customers, never less than 1 and never more than 20 and they are identified by a letter (A, B, C, …)
• The customers are distributed randomly among the different queues. • If two customers are served at the same time, we would consider that they will be ordered following the queue number they are at (first will be customer in queue 1, second customer in queue 2)
Write a program to give the order in which the customers are attended, for example:
• There are 4 queues:
• Cashier number 1 spends 3 minutes on each customer
• Cashier number 2 spends 2 minutes on each customer
• Cashier number 3 spends 4 minutes on each customer
• Cashier number 4 spends 1 minutes on each customer
• Customers are distributed as follows:
• Queue 1 (Cashier 1): Customer A, customer E, customer I
• Queue 2 (Cashier 2): B, F, J, N
• Queue 3 (Cashier 3): C, G, L
• Queue 4 (Cashier 4): D, H, M, O, P, Q
With this input the customers will have been served in the following order and timing:
D (after 1 minute)
B (after 2 minutes on queue 2)
H (after 2 minutes on queue 4)
A (after 3 minutes on queue 1)
M (after 3 minutes on queue 4)
F (after 4 minutes on queue 2)
C (after 4 minutes on queue 3)
O (after 4 minutes on queue 4)
P (after 5 minutes)
E (after 6 minutes on queue 1)
J (after 6 minutes on queue 2)
Q (after 6 minutes on queue 4)
N (after 8 minutes on queue 2)
G (after 8 minutes on queue 3)
I (after 9 minutes)
L (after 12 minutes)
The input from a data file will have the number of queues on the first line, followed by the information for each queue, first the time spent by the cashier on a customer, the number of customers on a queue and then the order of the customers separated by a space.
Output to the screen the list of served customers ordered by the time spent in the queue separated by spaces.
Must use a queue data structure.
Refer to the sample output below.
Sample File: the following information is on the queues.txt file
4
3 3 A E I
2 4 B F J N
4 3 C G L
1 6 D H M O P Q
Sample Run:
Enter file name: queues.txt
The list ordered by time spent: D B H A M F C O P E J Q N G I L
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String [] args){
Scanner sc=new
Scanner(System.in);
// read file name from user
System.out.print("Enter file name :
");
String filename=sc.next();
sc.close();
// 2d array queue and
customer
// each row is each queue
// each queue contain corresponding
customers
String [][] lst = null;
// corresponding time spends
int [] time = null;
// n for number of queue
// total use for total
customers
int n=0,total=0;
BufferedReader br = null;
try {
br = new
BufferedReader(new FileReader(filename));
int i=0;
String
line="";
// read file
line by line
while((line=br.readLine())!=null){
// first line
if(i==0){
// create array
n=Integer.parseInt(line);
lst=new String[n][];
time=new int[n];
}
else{
String [] splts=line.split("
");
time[i-1]=Integer.parseInt(splts[0]);
int
size=Integer.parseInt(splts[1]);
total+=size;
lst[i-1]=new
String[size];
// add customers
for(int
j=0;j<size;j++){
lst[i-1][j]=splts[2+j];
}
}
i++;
}
br.close();
} catch (IOException e) {
System.out.print("Error: "+e.getMessage());
}
int customers=0;
int t=1;
// number of customer , who finished service in a
queue
int [] finshed=new int[n];
// initialize each to 0
for(int i=0;i<n;i++){
finshed[i]=0;
}
System.out.print("The list ordered by time
spent: ");
// loop until all customer finished his service
while(customers<total){
// each time go through each
queue
for(int i=0;i<n;i++){
// if all
customers are not finished
if(finshed[i]<lst[i].length){
// current time
if(t%time[i]==0){
System.out.print(lst[i][finshed[i]]+" ");
// increment the number of
customer
// who finished his service
from the corresponding queue
finshed[i]++;
// increment total number of
customers
customers++;
}
}
}
t++;
}
}
}