Excelente Reto, hay que estar bien atento a las indicaciones de la guia y en especial tener un poco de logica para interpretar, ya que implicitamente debemos descontar el capacity una vez que se venda algun ticket del Flight.
Les dejo mi solucion. Saludos
Para Reservation.js
export class Reservation {
constructor(flight, passenger) {
this.flight = flight;
this.passenger = passenger;
}
reservationDetails() {
const { origin, destination, date } = this.flight;
const { name, lastName } = this.passenger;
return {
origin,
destination,
date,
reservedBy: `${name} ${lastName}`,
};
}
}
Para Passenger.js
export class Passenger {
constructor(name, lastName, age) {
this.name = name;
this.lastName = lastName;
this.age = age;
this.flights = [];
}
addFlight(flight) {
const { origin, destination, date, price } = flight;
this.flights.push({
origin,
destination,
date,
price,
});
}
}
Para Flight.js
import { Reservation } from './Reservation';
export class Flight {
constructor(origin, destination, date, capacity, price) {
this.origin = origin;
this.destination = destination;
this.date = date;
this.capacity = capacity;
this.price = price;
this.passengers = [];
}
addFlightToPassenger(passenger) {
passenger.addFlight(this);
}
sellTicket(passenger) {
if (this.capacity > 0) {
this.passengers.push({
fullName: `${passenger.name} ${passenger.lastName}`,
age: passenger.age,
});
this.capacity -= 1;
this.addFlightToPassenger(passenger);
return new Reservation(this, passenger);
}
}
}
Para PremiumFlight.js
import { Flight } from './Flight';
import { Reservation } from './Reservation';
export class PremiumFlight extends Flight {
constructor(origin, destination, date, capacity, price, specialService) {
super(origin, destination, date, capacity, price);
this.specialService = specialService;
}
// Override to apply logic
addFlightToPassenger(passenger) {
this.price += this.specialService;
passenger.addFlight(this);
}
}
Y para EconomicFlight.js
import { Flight } from './Flight';
import { Reservation } from './Reservation';
export class EconomicFlight extends Flight {
// Override to apply logic
addFlightToPassenger(passenger) {
if (passenger.age < 18 || passenger.age > 65) {
this.price *= 0.8;
}
passenger.addFlight(this);
}
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?