/*
Design a simple object-oriented system to manage basic information about airports. Your system should support the following functionalities:
Add a new airport to the system, including airport code, city, and country.
Display information about a specific airport based on its code.
Allow users to search for airports by --city or country-- and display a list of matching airports.
Allow airports to update their information, such as city or country, if needed.
Remove an airport from the system.
Your implementation should use appropriate classes and methods to represent airports and the airport system
*/
class AirportDomain(){
constructor(){
this.country={};
this.city={};
this.code={};
}
addAirport(airPort){
if(this.country[airPort.country]!==undefined){
this.country[airPort.country]=[airPort]
}else{// if its exist
this.country[airPort.country].push(airPort)
}
if(this.city[airPort.city]!==undefined){
this.city[airPort.city]=[airPort]
}else{// if its exist
this.city[airPort.city].push(airPort)
}
if(this.code[airPort.code]!==undefined){
this.city[airPort.code]=[airPort]
}else{// if its exist
this.code[airPort.code].push(airPort)
}
return this
}
searchByCity(city){
return this.city[city]
}
update(code,city,country){
const obj = this.code[code];
obj[city]=city;
obj[country]=country
return this
}
}
class Airport(){
constructor(code,city,country)
this.code=code;
this.city=city;
this.country=country;
}
get gerInformation(){
console.log(` code: ${this.code}, city: ${this.city}, country: ${this.country}`)
return this
}
}
//-----------------------------------------
class Airport {
constructor(code, city, country) {
this.code = code;
this.city = city;
this.country = country;
}
}
class AirportSystem {
constructor() {
this.airports = {};
}
addAirport(code, city, country) {
if (!this.airports[code]) {
this.airports[code] = new Airport(code, city, country);
console.log(`Airport ${code} added successfully.`);
} else {
console.log("Airport with this code already exists.");
}
}
displayAirportInfo(code) {
const airport = this.airports[code];
if (airport) {
console.log(`Airport Code: ${airport.code}`);
console.log(`City: ${airport.city}`);
console.log(`Country: ${airport.country}`);
} else {
console.log("Airport not found.");
}
}
searchAirportsByCity(city) {
const foundAirports = Object.values(this.airports).filter(airport => airport.city === city);
if (foundAirports.length > 0) {
foundAirports.forEach(airport => {
console.log(`Airport Code: ${airport.code}, Country: ${airport.country}`);
});
} else {
console.log("No airports found in this city.");
}
}
updateAirportInfo(code, newCity, newCountry) {
if (this.airports[code]) {
this.airports[code].city = newCity;
this.airports[code].country = newCountry;
console.log("Airport information updated successfully.");
} else {
console.log("Airport not found.");
}
}
removeAirport(code) {
if (this.airports[code]) {
delete this.airports[code];
console.log("Airport removed successfully.");
} else {
console.log("Airport not found.");
}
}
}
// Example usage:
const airportSystem = new AirportSystem();
airportSystem.addAirport("JFK", "New York", "USA");
airportSystem.addAirport("LAX", "Los Angeles", "USA");
airportSystem.addAirport("LHR", "London", "UK");
airportSystem.displayAirportInfo("JFK");
airportSystem.searchAirportsByCity("New York");
airportSystem.updateAirportInfo("JFK", "New York City", "USA");
airportSystem.displayAirportInfo("JFK");
airportSystem.removeAirport("LAX");
airportSystem.searchAirportsByCity("Los Angeles");