In: Computer Science
Suppose there is only one customer service available in SAMBA Bank in Thursday morning, in every 4 minutes a new customer arrives at the end of waiting line, each customer will need 7 minutes for the service
Write a program to print out the information after the first 60 minutes
(((( data structure course
Programming Language: java
Hint: use the queue concept )))
Summary:
In order to fulfill this requirement we are using inbuild queue in java by importing from
java.util.Queue library.
Queue works in the FIFO manner that is the customer who comes first will be the first to go out of the queue.
I have mentioned comments for you better understanding. Please go through each comments its very easy implementaion.
----------------------------------------------------------------------Customer.java-------------------------------------------------------------------
import java.util.LinkedList;
import java.util.Queue;
public class Customer {
int custId;
int arrivingTime;
int leavingTime;
public Customer(int custId, int arrivingTime, int leavingTime) {
this.custId = custId;
this.arrivingTime = arrivingTime;
this.leavingTime = leavingTime;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", arrivingTime=" + arrivingTime +
", leavingTime=" + leavingTime +
'}';
}
public static void main(String args[]) {
Queue<Customer> q = new LinkedList<>();
int custId = 0;
//Assuming first customer arrives at 0.
q.add(new Customer(custId, 0, 7));
for (int i = 1; i < 60; i++) {
//add new customer to queue in every 4 mins.
if (i % 4 == 0) {
custId++;
q.add(new Customer(custId, i, (i + 7)));
}
//remove customer from the queue end at every 7 min. Note queue works FIFO. i.e first in first out.
if (i % 7 == 0) {
q.remove();
}
}
// Printing all the customers in the queue
System.out.println("Total " + q.size() + " Customers are waiting in queue.");
System.out.println("***************************************************");
for (Customer customer : q) {
System.out.println(customer);
System.out.println();
}
// The peek() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container.
System.out.println("Current serving customer: " + q.peek());
System.out.println("***************************************************");
}
}
Output:
I enjoyed creating this solution, hope you'll enjoy it too after understanding.