HELP IN JAVA: For this assignment you are to write a program that simulates cust
ID: 3591308 • Letter: H
Question
HELP IN JAVA:
For this assignment you are to write a program that simulates customers arriving at the drive through of a bank. Your program will use the arrival times and service times for the 4 customers listed in the handout. It will calculate the total wait time and the average wait time. You will need to create a customer class. Each customer will have attributes arrivalTime, serviceTime, finishTime (all int), and the constructor will have as parameter arrivalTime and serviceTime. Here is your input file (first int is arrival time, second is service time).
input.txt:
5 15
6 12
8 4
40 2
40 3
41 8
public class Customer {
private int arrivalTime;
private int finishTime;
private int serviceTime;
public Customer(int A, int B){
arrivalTime=A;
finishTime=0;
serviceTime=B;
}
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getFinishTime() {
return finishTime;
}
public void setFinishTime(int finishTime) {
this.finishTime = finishTime;
}
public int getServiceTime() {
return serviceTime;
}
public void setServiceTime(int serviceTime) {
this.serviceTime = serviceTime;
}
public String toString() {
return "arrivalTime: "+ " "+ arrivalTime + "finishTime: " + " "+ finishTime + "serviceTime: "+ " "+ serviceTime;
}
}
public static void main(String[] args) {
Queue <Customer> myQ = new Queue();
//file input
File inFile = new File("H10.txt");
// try/catch
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
}
//while loop
while (fileInput.hasNext()) {
int arrival = fileInput.nextInt();
int service = fileInput.nextInt();
Customer A = new Customer(arrival,service);
myQ.enqueue(A);
}
myQ.print();
} }
Explanation / Answer
/*For a better Object Oriented implementation, I’ve included the main function in another class called Tester.java which also includes the function to calculate the total wait time*/
//Customer.java class
import java.io.File;
import java.io.FileNotFoundException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Customer {
private int arrivalTime;
private int finishTime;
private int serviceTime;
private int waitTime;// for storing waittime
public Customer(int A, int B){
arrivalTime=A;
finishTime=0;
serviceTime=B;
waitTime=0;
}
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getFinishTime() {
return finishTime;
}
public void setFinishTime(int finishTime) {
this.finishTime = finishTime;
}
public int getServiceTime() {
return serviceTime;
}
public void setServiceTime(int serviceTime) {
this.serviceTime = serviceTime;
}
public void setWaitTime(int waitTime) {
this.waitTime=waitTime;
}
public int getWaitTime() {
return waitTime;
}
public String toString() {
return "arrivalTime: "+ " "+ arrivalTime + ", finishTime: " + " "+
finishTime + ", serviceTime: "+ " "+ serviceTime+ ", waitTime: "+waitTime;
}
}
//end of Customer.java
//Tester.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
ArrayList<Customer> customers=new ArrayList<Customer>(); //It's better to use a List than Queue; and you cannot instantiate a Queue (in your question it was wrongly put)
File inFile = new File("input.txt");
// try/catch
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
System.out.println("file not found");
}
// while loop
int i = 0;
while (fileInput.hasNextLine()) {
int arrival = fileInput.nextInt();
int service = fileInput.nextInt();
Customer tmp = new Customer(arrival,service);
customers.add(tmp); //adding to the list
//System.out.println(arrival + " " + service);
//i++;
}
calculateWaitTime(customers);
}
public static void calculateWaitTime(ArrayList<Customer> list){
int total_wait_time=0;
for (int i=0;i<list.size();i++){
if(i==0)//first customer, no need to wait
{
Customer tmp=list.get(i);
tmp.setFinishTime(tmp.getArrivalTime()+tmp.getServiceTime());
}else{
Customer tmp1=list.get(i);
Customer tmp2=list.get(i-1);
if(tmp1.getArrivalTime()>tmp2.getFinishTime()){ //customer who came after the finish time of previous customer; no need to wait
tmp1.setFinishTime(tmp1.getArrivalTime()+tmp1.getServiceTime());
}else{
tmp1.setFinishTime(tmp2.getFinishTime()+tmp1.getServiceTime()); //need to wait till the previous customer finishes.
}
}
}
for (Customer customer : list) {
customer.setWaitTime(customer.getFinishTime()-(customer.getArrivalTime()+customer.getServiceTime())); //setting the wait time of each customers
System.out.println(customer.toString()); //printing all the details
total_wait_time+=customer.getWaitTime(); //counting the total wait time
}
System.out.println("Total wait time: "+total_wait_time);
System.out.println("Average wait time: "+(total_wait_time/list.size()));
}
}
//end of Tester.java
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.