relationships 2
Wed May 29 2024 09:34:54 GMT+0000 (Coordinated Universal Time)
Saved by @exam123
//Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CountryBO countryBO = new CountryBO();
AirportBO airportBO = new AirportBO();
// Input for Country
System.out.println("Enter the country count:");
int countryCount = scanner.nextInt();
scanner.nextLine(); // Consume newline character
Country[] countries = new Country[countryCount];
for (int i = 0; i < countryCount; i++) {
System.out.println("Enter country " + (i + 1) + " details");
String countryData = scanner.nextLine();
countries[i] = countryBO.createCountry(countryData);
}
// Input for Airport
System.out.println("Enter the airport count:");
int airportCount = scanner.nextInt();
scanner.nextLine(); // Consume newline character
Airport[] airports = new Airport[airportCount];
for (int i = 0; i < airportCount; i++) {
System.out.println("Enter airport " + (i + 1) + " details:");
String airportData = scanner.nextLine();
airports[i] = airportBO.createAirport(airportData, countries);
}
// Find country for airport
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);
// Compare airports
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 different Country");
}
scanner.close();
}
}
public class AirportBO {
public Airport createAirport(String data, Country[] countryList) {
String[] parts = data.split(",");
for (Country country : countryList) {
if (country.getCountryName().equals(parts[1])) {
return new Airport(parts[0], country);
}
}
return null;
}
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, 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);
}
}
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;
}
}
public class CountryBO {
public Country createCountry(String data) {
String[] parts = data.split(",");
return new Country(parts[0], parts[1]);
}
}
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