Write a Java program to filter the client details based on the country using one to one relationship.
Strictly adhere to the object-oriented specifications given as a part of the problem statement. Use the same class names and attributes names and method names should be the same as specified in the problem statement.
Create a class named Client with the following private member variables.
- Integer clientId
- String clientName
- String phoneNumber
- String email
- String passport
- Country country
Include appropriate getters and setters, default constructor and parameterized constructor.
Override toString() method to display the client details as the specified format. Use (“%-25s %-25s %-25s %-25s %-25s %s”) this format.
Create a class named Country with the following private member variables.
- String iataCountryCode
- String countryName
Include appropriate getters and setters, default constructor and parameterized constructor.
Override toString() method to display the country details. Use (“%-25s %s\n”) this format.
Create a class named ClientBO with the following public methods.
No | Method | Description |
1 | void viewDetails(Client[] clientList) | This method is used to print all the details |
2 | void printClientDetailsWithCountry(Client[] clientList, String countryName) | In this method, display all client details of a specific country(from Country) from the client list. |
Create another class Main and write the main method to test the above classes.
Note: Use an array of objects to read Client details
Input and Output Format:
Refer to sample input and output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter number of clients
2
Enter client 1 details:
Enter the client id
11245
Enter the client name
Rahul
Enter the phone number
9845632175
Enter the email id
rahul@gmail.com
Enter the passport number
54623
Enter the iata country code
IN91
Enter the country name
India
Enter client 2 details:
Enter the client id
11236
Enter the client name
Aadhi
Enter the phone number
8956412348
Enter the email id
aadhi@gmail.com
Enter the passport number
55642
Enter the iata country code
IN91
Enter the country name
India
Menu:
1.View client details
2.Filter client with country
3.Exit
1
ClientId ClientName PhoneNumber Email Passport IATACountryCode CountryName
11245 Rahul 9845632175 rahul@gmail.com 54623 IN91 India
11236 Aadhi 8956412348 aadhi@gmail.com 55642 IN91 India
Menu:
1.View client details
2.Filter client with country
3.Exit
2
Enter country name
India
ClientId ClientName PhoneNumber Email Passport IATACountryCode CountryName
11245 Rahul 9845632175 rahul@gmail.com 54623 IN91 India
11236 Aadhi 8956412348 aadhi@gmail.com 55642 IN91 India
Menu:
1.View client details
2.Filter client with country
3.Exit
3
Solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of clients");
String n=sc.nextLine();
Integer num=Integer.parseInt(n);
ArrayList<String> al=new ArrayList<>();
for(int i=0;i<num;i++){
System.out.println("Enter client "+(i+1)+" details:\nEnter the client id");
String cid=sc.nextLine();
System.out.println("Enter the client name");
String cname=sc.nextLine();
System.out.println("Enter the phone number");
String phone=sc.nextLine();
System.out.println("Enter the email id");
String email=sc.nextLine();
System.out.println("Enter the passport number");
String pass=sc.nextLine();
System.out.println("Enter the iata country code");
String iata=sc.nextLine();
System.out.println("Enter the country name");
String country=sc.nextLine();
al.add(cid+","+cname+","+phone+","+email+","+pass+","+iata+","+country);
}
for(int z=0;z<2000;z++){
System.out.println("Menu:\n1.View client details\n2.Filter client with country\n3.Exit");
String op=sc.nextLine();
if(op.equals("1")){
System.out.printf("%-25s %-25s %-25s %-25s %-25s %-25s %s","ClientId","ClientName","PhoneNumber","Email","Passport","IATACountryCode","CountryName\n");
for(int l=0;l<al.size();l++){
String[] fr=al.get(l).split(",");
System.out.printf("%-25s %-25s %-25s %-25s %-25s %-25s %s",fr[0],fr[1],fr[2],fr[3],fr[4],fr[5],fr[6]+"\n");
}
}
else if(op.equals("2")){
System.out.println("Enter country name");
String tempc=sc.nextLine();
System.out.printf("%-25s %-25s %-25s %-25s %-25s %-25s %s","ClientId","ClientName","PhoneNumber","Email","Passport","IATACountryCode","CountryName\n");
for(int l=0;l<al.size();l++){
String[] fr=al.get(l).split(",");
if(fr[6].equals(tempc)){
System.out.printf("%-25s %-25s %-25s %-25s %-25s %-25s %s",fr[0],fr[1],fr[2],fr[3],fr[4],fr[5],fr[6]+"\n");
}
}
}
else if(op.equals("3")){
break;
}
}
}
}
Happy Learning – If you require any further information, feel free to contact me.