In: Computer Science
You have to get a new driver's license and you show up at the office at the same time as 4 other people. The office says that they will see everyone in alphabetical order and it takes 20 minutes for them to process each new license. All of the agents are available now, and they can each see one customer at a time. How long will it take for you to walk out of the office with your new license? Task Given everyone's name that showed up at the same time, determine how long it will take to get your new license. Input Format Your input will be a string of your name, then an integer of the number of available agents, and lastly a string of the other four names separated by spaces. Output Format You will output an integer of the number of minutes that it will take to get your license. Implement the code in java
import java.util.*;
public class Main
{
public static int findIndex(String arr[], String t) //function to
get index of user in row
{
// if array is Null
if (arr == null)
{
return -1;
}
// find length of array
int len = arr.length;
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (t.equalsIgnoreCase(arr[i])) {
// System.out.println("null");
return i;
}
else {
i = i + 1;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println("Enter your name"); //getting user name
Scanner sc=new Scanner(System.in);
String s=sc.next();
String s1=s;
//System.out.println(s1);
//n[0]=s;
System.out.println("Enter the number of available Agents");
//getting Free agent number
int d=sc.nextInt();
System.out.println("Enter your Enter 4 people names"); //getting
details of other 4 member
sc.nextLine();
s=sc.nextLine();
s=s+" " +s1;
//System.out.println(s);
String[] n2=new String[5];
n2=s.split("\\s+"); //splitting names into name array for
alpbhatical arrangement
//n1[n1.length-1]=s1;
Arrays.sort(n2); //sorting new array
int result=0;
int j=findIndex(n2,s1); //getting index of user in row
//System.out.println(j);
if(j>d){ //if row number is gretaer than free agent then user
have to wait for 20 + number of members ahead him in row(j-d)
result=(j-d+2)*20;
System.out.println(s1+" will get its new Licence in "+result+"
minute");
}
else{//else user get its details as he is assinged to free
agent
System.out.println(s1+" will get its new Licence in 20
minute");
}
}
}
If you found this answer helpful please give a thumbs up.