Relationship i assess
Sat May 04 2024 06:07:52 GMT+0000 (Coordinated Universal Time)
Saved by @cms
class Client { private Integer clientId; private String clientName; private String phoneNumber; private String email; private String passport; private Country country; public Client() { } public Client(Integer clientId, String clientName, String phoneNumber, String email, String passport, Country country) { this.clientId = clientId; this.clientName = clientName; this.phoneNumber = phoneNumber; this.email = email; this.passport = passport; this.country = country; } // Getters and setters public Integer getClientId() { return clientId; } public void setClientId(Integer clientId) { this.clientId = clientId; } public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName = clientName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassport() { return passport; } public void setPassport(String passport) { this.passport = passport; } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; } @Override public String toString() { return String.format("%-25s %-25s %-25s %-25s %-25s %s", clientId, clientName, phoneNumber, email, passport, country); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter number of clients"); int numClients = scanner.nextInt(); scanner.nextLine(); // Consume newline Client[] clients = new Client[numClients]; for (int i = 0; i < numClients; i++) { System.out.println("Enter client " + (i + 1) + " details:"); System.out.println("Enter the client id"); int clientId = scanner.nextInt(); scanner.nextLine(); // Consume newline System.out.println("Enter the client name"); String clientName = scanner.nextLine(); System.out.println("Enter the phone number"); String phoneNumber = scanner.nextLine(); System.out.println("Enter the email id"); String email = scanner.nextLine(); System.out.println("Enter the passport number"); String passport = scanner.nextLine(); System.out.println("Enter the iata country code"); String iataCountryCode = scanner.nextLine(); System.out.println("Enter the country name"); String countryName = scanner.nextLine(); Country country = new Country(iataCountryCode, countryName); clients[i] = new Client(clientId, clientName, phoneNumber, email, passport, country); } ClientBO clientBO = new ClientBO(); int choice; do { System.out.println("Menu:"); System.out.println("1.View client details"); System.out.println("2.Filter client with country"); System.out.println("3.Exit"); choice = scanner.nextInt(); switch (choice) { case 1: clientBO.viewDetails(clients); break; case 2: System.out.println("Enter country name"); scanner.nextLine(); String countryName = scanner.nextLine(); clientBO.printClientDetailsWithCountry(clients, countryName); break; case 3: break; default: System.out.println("Invalid choice. Please enter again."); } } while (choice != 3); } } class Country { private String iataCountryCode; private String countryName; public Country() { } public Country(String iataCountryCode, String countryName) { this.iataCountryCode = iataCountryCode; this.countryName = countryName; } // Getters and setters public String getIataCountryCode() { return iataCountryCode; } public void setIataCountryCode(String iataCountryCode) { this.iataCountryCode = iataCountryCode; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } @Override public String toString() { return String.format("%-25s %s\n", iataCountryCode, countryName); } } class ClientBO { public void viewDetails(Client[] clientList) { System.out.println("ClientId ClientName PhoneNumber Email Passport IATACountryCode CountryName"); for (Client client : clientList) { System.out.println(client); } } public void printClientDetailsWithCountry(Client[] clientList, String countryName) { System.out.println("ClientId ClientName PhoneNumber Email Passport IATACountryCode CountryName"); for (Client client : clientList) { if (client.getCountry().getCountryName().equalsIgnoreCase(countryName)) { System.out.println(client); } } } } //2----------------------------------------------------------------------------------- // CountryBO.java public class CountryBO { public Country createCountry(String data) { String[] parts = data.split(","); String iataCountryCode = parts[0]; String countryName = parts[1]; return new Country(iataCountryCode, countryName); } } // AirportBO.java public class AirportBO { public Airport createAirport(String data, Country[] countryList) { String[] parts = data.split(","); String airportName = parts[0]; String countryName = parts[1]; Country country = null; for (Country c : countryList) { if (c.getCountryName().equals(countryName)) { country = c; break; } } return new Airport(airportName, country); } public String findCountryName(Airport[] airportList, String airportName) { for (Airport airport : airportList) { if (airport.getAirportName().equals(airportName)) { return airport.getCountry().getCountryName(); } } return null; } public boolean findWhetherAirportsAreInSameCountry(Airport[] airportList, String airportName1, String airportName2) { String country1 = null; String country2 = null; for (Airport airport : airportList) { if (airport.getAirportName().equals(airportName1)) { country1 = airport.getCountry().getCountryName(); } if (airport.getAirportName().equals(airportName2)) { country2 = airport.getCountry().getCountryName(); } } return country1 != null && country2 != null && country1.equals(country2); } } // Airport.java public class Airport { private String airportName; private Country country; public Airport() { } public Airport(String airportName, Country country) { this.airportName = airportName; this.country = country; } public String getAirportName() { return airportName; } public void setAirportName(String airportName) { this.airportName = airportName; } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; } } // Main.java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the country count"); int countryCount = scanner.nextInt(); scanner.nextLine(); // Consume newline Country[] countries = new Country[countryCount]; CountryBO countryBO = new CountryBO(); for (int i = 0; i < countryCount; i++) { System.out.println("Enter country " + (i + 1) + " details"); String data = scanner.nextLine(); countries[i] = countryBO.createCountry(data); } System.out.println("Enter the airport count"); int airportCount = scanner.nextInt(); scanner.nextLine(); // Consume newline Airport[] airports = new Airport[airportCount]; AirportBO airportBO = new AirportBO(); for (int i = 0; i < airportCount; i++) { System.out.println("Enter airport " + (i + 1) + " details"); String data = scanner.nextLine(); airports[i] = airportBO.createAirport(data, countries); } System.out.println("Enter the airport name for which you need to find the country name"); String airportName = scanner.nextLine(); String countryName = airportBO.findCountryName(airports, airportName); System.out.println(airportName + " belongs to " + countryName); System.out.println("Enter 2 airport names"); String airportName1 = scanner.nextLine(); String airportName2 = scanner.nextLine(); boolean sameCountry = airportBO.findWhetherAirportsAreInSameCountry(airports, airportName1, airportName2); if (sameCountry) { System.out.println("The 2 airports are in the same Country"); } else { System.out.println("The 2 airports are in the different Country"); } } } // Country.java public class Country { private String iataCountryCode; private String countryName; public Country() { } public Country(String iataCountryCode, String countryName) { this.iataCountryCode = iataCountryCode; this.countryName = countryName; } public String getIataCountryCode() { return iataCountryCode; } public void setIataCountryCode(String iataCountryCode) { this.iataCountryCode = iataCountryCode; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } }
Comments