A class CustomerService is defined to resolve customer service requests in the order in which they are received.
The details of the members of the class are given below:
Class name: CustomerService
Data members/instance variables:
services[]: array to store customer service request
size: to store the maximum capacity of the array
first: to store the index of the first customer service request
last: to store the index of the last customer service request
Methods/Member functions:
CustomerService(int s): constructor to assign size = s, first = 0 and last = 0
void add(int reqst): to insert a request at index last, if space is available, otherwise display “Request cannot be accepted at the moment”
int del(): to remove and return the request at index first, if any, else return -9999.
(i) Specify the class CustomerService giving details of the functions void add(int) and int del(). Assume that the other functions have been defined.
import java.util.Scanner;
class CustomerService{
int services[];
int size;
int first;
int last;
public CustomerService(int s){
size = s;
services = new int[size];
first = 0;
last = 0;
}
public void add(int reqst){
if(last < size)
services[last++] = reqst;
else
System.out.println("Request cannot be accepted at the moment.");
}
public int del(){
if(first < last){
int reqst = services[first];
first++;
return reqst;
}
return -9999;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Array capacity: ");
int n = in.nextInt();
CustomerService obj = new CustomerService(n);
while(true){
System.out.println("1. Add request");
System.out.println("2. Delete request");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch(choice){
case 1:
System.out.print("Enter request: ");
int r = in.nextInt();
obj.add(r);
break;
case 2:
int d = obj.del();
if(d == -9999)
System.out.println("Queue Empty");
else
System.out.println("Deleted request: " + d);
break;
default:
System.out.println("Bye...");
return;
}
}
}
}
(ii) Name the entity described above and state its principle.
The entity described above is the Queue Data Structure. It works on the FIFO (First In First Out) principle.