Playground - Agenda de vuelos
Clase 65 de 99 • 30 días de JavaScript
Contenido del curso
Clase 65 de 99 • 30 días de JavaScript
Contenido del curso
Luis Gomero
David Ochoa
Luis Gomero
David Ochoa
Luis Gomero
Jose Carlos Machado
Daniel Sebastián Rodríguez
Luis Gomero
Joan Alexander Valerio Rodríguez
Juan Rossano
JAIME EDUARDO DIAZ TOBON
Andrés Soret Chacin
Andres Felipe Torres
Harrison Steve Pinzón Neira
Rubén Hernández Hernández
Alexis Corrales
Angel Javier Sanchez Tenjo
Carina Payleman
Alejandro Anaya
Elias Rayas Gonzalez
Gabriel Luna
Ivan Pérez Arencibia
Frandel Corporan Rodríguez
Maday Choque
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); } }
Muy bien, pero deberias realizar una copia del precio en el vuelo Economico, ya que si compras mas de una vez un ticket, el precio quedara con el descuento anterior.
Buen detalle David, es cierto menos mal el test no validaba una compra mas. Voy a tomarlo en cuenta para optimizar esa parte. En un momento tb le doy una revisada al tuyo. exitos
Escudo anti-spoilers
!Imagen anti-spoilers
Muy entretenido el reto, y se vuelve muy fácil cuando se tiene los conocimientos de los cursos recomendados en el post del reto #PlatziChallenge: 30 días de JavaScript
A continuación comparto mi solución 😎
EconomicFlight.js
En este módulo es importante considerar que si hacemos algún cambio directo el precio como lo hizo @luisgv94, las próximas iteraciones, el precio del vuelo no será el correcto, es por ello que agregué una variable auxiliar que me permite recuperar el precio anterior.
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); } sellTicket(passenger) { // Tu código aquí 👇 if (this.lastPrice != undefined) { this.price = this.lastPrice } if (passenger.age < 18 || passenger.age > 65) { this.lastPrice = this.price this.price = this.price * 0.8; } return super.sellTicket(passenger); } }
Flight.js
🤔 Para este módulo es importante tomar en cuenta que cada vez que se venda un ticket, la capacidad del vuelo debe decrementar
import { Reservation } from "./Reservation"; export class Flight { constructor(origin, destination, date, capacity, price) { // Tu código aquí 👈 this.origin = origin; this.destination = destination; this.date = date; this.capacity = capacity; this.price = price; this.passengers = []; } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age, }); passenger.addFlight(this); let reservation = new Reservation(this, passenger); this.capacity--; return reservation; } // Tu código aquí 👈 } }
Passenger.js
Nada que comentar en este módulo, la explicación muy clara en este inciso.
export class Passenger { // Tu código aquí 👈 constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } addFlight(flight) { this.flights.push({ origin: flight.origin, destination: flight.destination, date: flight.date, price: flight.price, }); } }
PremiumFlight.js
Aquí no sé si hice un poco de trampa, pero al no ser clara las instrucciones también es posible agregar directamente el precio de specialService a la variable this.price y así nos evitamos sobreescribir el método addFlightToPassenger
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { // Tu código aquí 👈 super(origin, destination, date, capacity, price) this.price = price + specialService } }
Reservation.js
Aquí solamente considerar bien los nombres de variables como reservedBy fullName, etc.
export class Reservation { constructor(flight, passenger) { // Tu código aquí 👈 this.flight = flight; this.passenger = passenger; } reservationDetails() { // Tu código aquí 👈 return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: `${this.passenger.name} ${this.passenger.lastName}`, }; } }
NUNCA PARES DE APRENDER!
Excelente amigo David. Me parecio un trucazo tu hack hehe para el de PremiumFlight. Ahi ajuste y subi una nueva solucion. Saludos
Oigame, Oigame, Oigame, un fuerte aplauso para todos los que hemos llegado aquí, y hemos luchado contra nosotros mismos para lograr estos lindos desafíos que nos ha dado Platzi. Quiero recalcar, que si capacidad es igual a 0 no se debe retornar nada, es decir, undefined... ahora sí, dejo mi código... espero que les sirva, como a mi me ha servido algunos de ustedes... :)
export class Flight { constructor(origin, destination, date, capacity, price) { this.origin = origin; this.date = date; this.destination = destination; this.capacity = capacity; this.price = price; this.passengers = []; } sellTicket(passenger) { // Tu código aquí 👇 if (this.capacity > 0) { let res = { origin: this.origin, destination: this.destination, date: this.date, price: this.price } this.capacity--; let reservation = new Reservation(res, passenger); this.passengers.push(reservation.full); passenger.flights.push(res) //agregar el vuelo return reservation } } }
export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); } sellTicket(passenger) { // Tu código aquí 👇 if (passenger.age < 18 || passenger.age > 65) { this.price = this.price - (this.price * 0.2); return super.sellTicket(passenger); } else { return super.sellTicket(passenger); } } }
export class Passenger { // Tu código aquí 👈 constructor(name,lastName, age) { this.name = name; this.age = age; this.full = {fullName:`${name} ${lastName}`,age:this.age} this.flights = []; } }
export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { // Tu código aquí 👈 super(origin, destination, date, capacity, price + specialService); } }
export class Reservation { constructor(flight, passenger) { // Tu código aquí 👈 this.flight = flight; this.passenger = passenger; this.full = passenger.full; this.age = passenger.age; this.origin = flight.origin; this.destination = flight.destination; this.date = flight.date; this.reservedBy = passenger.full.fullName } reservationDetails() { // Tu código aquí 👈 return { origin: this.origin, destination: this.destination, date: this.date, reservedBy: this.reservedBy } } }
Aporto mi respuesta! . . . . . . . . .
Reservation.js
export class Reservation { constructor(flight, passenger) { this.flight = flight this.passenger = passenger } reservationDetails() { return {'origin':this.flight.origin,'destination':this.flight.destination,'date':this.flight.date,'reservedBy':this.passenger.name+' '+this.passenger.lastName} } }
Passenger.js
export class Passenger { constructor(name, lastName, age) { this.name = name this.lastName = lastName this.age = age this.flights = [] } addFlight(origin, destination, date, price) { this.flights.push( { 'origin': origin, 'destination': destination, 'date': date, 'price': price }) } }
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 = [] } sellTicket(passenger,price=this.price) { if (this.capacity > 0) { passenger.addFlight(this.origin, this.destination, this.date, price) this.passengers.push( { 'age':passenger.age, 'fullName': passenger.name + " " + passenger.lastName, } ) this.capacity-- //Para que no se modifique el precio original del vuelo al aplicar descuentos let temp = this temp.price = price return new Reservation(temp,passenger) } } }
EconomicFlight.js
import { Flight } from "./Flight"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price) this.discountPrice = price*0.8 } sellTicket(passenger) { if (passenger.age > 65 || passenger.age < 18) return super.sellTicket(passenger, this.discountPrice) else return super.sellTicket(passenger,this.price) } }
PremiumFlight.js
import { Flight } from "./Flight"; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price) this.price += specialService } sellTicket(passenger) { return super.sellTicket(passenger) } }
Debido a que aun no se puede editar los comentarios hehe (ya creo que es hora) voy a subir una actualizacion de mi solucion propuesta con creditos y agradecimientos especiales a @DavidOchoa. Pues si es necesario no modificar la propiedad price del EconomicFlight cada vez que se venda algun ticket. Por ello en el metodo que propuse addFlightToPassenger() estoy haciendo una copia del objeto this que representa al Flight y asi evito modicar el precio cuando se cree la reservacion una vez vendido el ticket de avion. . . . .
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); return this; } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age, }); this.capacity -= 1; const cloneFlight = this.addFlightToPassenger(passenger); return new Reservation(cloneFlight, passenger); } } }
Para EconomicFlight.js
import { Flight } from './Flight'; import { Reservation } from './Reservation'; export class EconomicFlight extends Flight { // Override to apply logic addFlightToPassenger(passenger) { const cloneFlight = { ...this }; if (passenger.age < 18 || passenger.age > 65) { cloneFlight.price *= 0.8; } passenger.addFlight(cloneFlight); return cloneFlight; } }
Para PremiumFlight.js (Gran hack del amigo David)
import { Flight } from './Fli'; import { Reservation } from './Reservation'; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price); this.price += specialService; } }
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, }); } }
Reservation
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}`, }; } }
Buenísimo el ejercicio, la verdad excelente para practicar. Lamentablemente la redacción de estos retos son un dolor de cabeza..
Aqui mi solución:
A mi de da un error que no encuentro cual es porque comparo mi codigo con la solucion y mas alla de ciertos cambios de variables no encuentro la explicacion al siguiente mensaje de error de las pruebas solo para esto y el resto bien
Should add a flight to passenger expect(received).toEqual(expected) // deep equality - Expected - 0 + Received + 6 Array [ Object { "date": "2022-01-01", "destination": "Guadalajara", "origin": "CDMX", "price": 1000, }, + Object { + "date": "2022-01-01", + "destination": "Guadalajara", + "origin": "CDMX", + "price": 1000, + }, ]
El error dice que no estas agregando al pasajero su vuelo, la clase pasajero tiene que tener un arreglo que se llama flights y cada que vendas un tiquete o se llame al método sellTicket de cualquiera de las clases de debe agregar el vuelo a ese arreglo, y ese vuelo solo debe de tener esos argumentos origin destination date price pero es un poco difícil saber si es que no lo estas agregando o lo estas agregando mal o la variable tiene un error de que las variables no se llamen igual, asi que si nos puedes mostrar el código te podría ayudar mejor
My solution 👇
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 = []; } sellTicket(passenger) { if (this.capacity > 0) { this.capacity -= 1; this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age }); passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }); return new Reservation(this, passenger); } } } ```Passenger.js: ```js export class Passenger { constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } } ```Reservation.js: ```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}` } } } ```PremiumFlight.js: ```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; } sellTicket(passenger) { this.price += this.specialService; return super.sellTicket(passenger); } } ```EconomicFlight.js: ```js import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) { const discountValue = this.price * 0.2; this.price -= discountValue; } return super.sellTicket(passenger); } }
Muy interesante el reto, me pasó que solo hasta ver las pruebas unitarias caí en cuenta de disminuir la cantidad de asientos disponibles del vuelo. Otro detalle es evitar modificar el precio original del vuelo por aplicar un descuento o un aumento de precio por vuelo premium.
EconomicFlight.js
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); } sellTicket(passenger) { let specialPrice = ''; if (passenger.age < 18 || passenger.age > 65) { specialPrice = this.price * 0.8; } return super.sellTicket(passenger, specialPrice); } }
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 = []; } sellTicket(passenger, specialPrice) { console.log('sellTicket'); console.log('passenger: ', passenger); if (this.capacity > 0) { this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age }); this.capacity -= 1; const flightCopy = { origin: this.origin, destination: this.destination, date: this.date, price: specialPrice? specialPrice: this.price } passenger.flights.push(flightCopy); return new Reservation(flightCopy, passenger); } } }
Passenger.js
export class Passenger { constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } }
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; } sellTicket(passenger) { let specialPrice = this.price + this.specialService; return super.sellTicket(passenger, specialPrice); } }
Reservation.js
export class Reservation { constructor(flight, passenger) { this.flight = flight; this.passenger = passenger; } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: `${this.passenger.name} ${this.passenger.lastName}` } } }
Desafio completado:
Mi solución 💚 Cuidado con la lógica de disminuir la capacidad y también vean los ejemplos para ver que se espera que se agregue del pasajero en el vuelo (ya que yo cometí el error de pasar toda la referencia jaja 😅) !LadyGaga
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 = [] } sellTicket(passenger) { if (this.capacity <= 0) return this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age }) passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }) this.capacity--; return new Reservation(this, passenger) } }
Passenger.js
export class Passenger { constructor(name, lastName, age) { this.name = name this.lastName = lastName this.age = age this.flights = [] } }
Reservation.js
export class Reservation { constructor(flight, passenger) { this.flight = flight this.passenger = passenger } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: `${this.passenger.name} ${this.passenger.lastName}` } } }
PremiumFlight.js
import { Flight } from "./Flight"; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price) this.specialService = specialService } sellTicket(passenger) { this.price += this.specialService; return super.sellTicket(passenger) } }
EconomicFlight.js
import { Flight } from "./Flight"; export class EconomicFlight extends Flight { sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) this.price -= (this.price * 0.20) return super.sellTicket(passenger) } }
Flight
constructor(origin, destination, date, capacity, price) { this.origin = origin; this.destination = destination; this.date = date; this.capacity = capacity; this.price = price; this.passengers = []; } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push(passenger); passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }); this.capacity--; return new Reservation(this, passenger); } else { console.log("el vuelo esta lleno"); } }
Passenger
export class Passenger { constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } }
Reservation
export class Reservation { constructor(flight, passenger) { this.flight = flight; this.passenger = passenger; } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: `${this.passenger.name} ${this.passenger.lastName}` }; } }
PremiumFlight
export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price); this.specialService = specialService; } sellTicket(passenger) { const reservation = super.sellTicket(passenger); if (reservation) { reservation.flight.price += this.specialService; } return reservation; } }
EconomicFlight
export class EconomicFlight extends Flight { sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) { this.price *= 0.8; // Apply 20% discount } return super.sellTicket(passenger); } }
Hola
👏 👏 👏 👏 👏 👏 👏 👏 👏 👏
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 = []; } get capacity() { return this._capacity - this.passengers.length; } set capacity(capacity) { this._capacity = capacity; } sellTicket(passenger) { if (this.capacity > 0) { passenger.flights.push( { origin : this.origin, destination : this.destination, date : this.date, price : this.price } ); this.passengers.push({ fullName : passenger.name + ' ' + passenger.lastName, age: passenger.age }); return new Reservation(this, passenger); } } }
Passenger.js
export class Passenger { constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } }
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 + specialService); this.specialService = specialService; } sellTicket(passenger) { return super.sellTicket(passenger); } }
Reservation.js
export class Reservation { constructor(flight, passenger) { this.flight = flight; this.passenger = passenger; } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: this.passenger.name + ' ' + this.passenger.lastName, }; } }
EconomicFlight.js
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); } sellTicket(passenger) { let priceNew = this.price; if (passenger.age < 18 || passenger.age > 65) { priceNew -= this.price * 0.2; } if (this.capacity > 0) { passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: priceNew, }); this.passengers.push({ fullName: passenger.name + " " + passenger.lastName, age: passenger.age, }); return new Reservation({ origin: this.origin, destination: this.destination, date: this.date, price: priceNew, }, passenger); } } }
EconomicFlight.js:
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) { this.price *= 0.80 } return super.sellTicket(passenger); } }
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 = []; } sellTicket(passenger) { if (this.capacity > 0) { let reservation = new Reservation(this, passenger); this.passengers.push({ fullName: passenger.fullName, age: passenger.age }); passenger.addFlight(this); this.capacity--; return reservation; } } }
Passenger.js:
export class Passenger { constructor(name, lastName, age) { this.fullName = name + " " + lastName; this.age = age; this.flights = []; } addFlight(flight) { this.flights.push( { 'origin': flight.origin, 'destination': flight.destination, 'date': flight.date, 'price': flight.price }) } }
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.price += specialService; } sellTicket(passenger) { return super.sellTicket(passenger); } }
Reservation.js:
export class Reservation { constructor(flight, passenger) { this.flight = flight; this.passenger = passenger; } reservationDetails() { return { "origin": this.flight.origin, "destination": this.flight.destination, "date": this.flight.date, "reservedBy": this.passenger.fullName } }
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
import { Flight } from './Flight'; 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); } }
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); } } }
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, }); } }
PremiumFlight.js
import { Flight } from './Flight'; 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); } }
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}`, }; } } ``
Solucion
Reservation.js
export class Reservation { constructor(flight, passenger) { // Tu código aquí 👈 this.flight = flight; this.passenger = passenger; } reservationDetails() { // Tu código aquí 👈 return { flightDetails: this.flight, passenger: this.passenger, }; } }
Flight.js
import { Reservation } from "./Reservation.mjs"; export class Flight { constructor(origin, destination, date, capacity, price) { // Tu código aquí 👈 this.origin = origin; this.destination = destination; this.date = date; this.capacity = capacity; this.price = price; this.passengers = []; } sellTicket(passenger) { // Tu código aquí 👈 if (this.capacity > 0) { // Add passenger and reduce capacity by each indivudial this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age, }); this.capacity -= 1; // Add flight to passenger's data passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price, }); return new Reservation(this, passenger); } } }
Passenger.js
export class Passenger { // Tu código aquí 👈 constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } }
PremiumFlight.js
import { Flight } from "./Flight.mjs"; import { Reservation } from "./Reservation.mjs"; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); this.passengers = []; this.specialService = 1.2; } sellTicket(passenger) { if (this.capacity > 0) { // Add passenger and reduce capacity by each indivudial this.passengers.push(passenger); this.capacity -= 1; // Add flight to passenger's data passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: (this.price *= this.specialService), }); return new Reservation(this, passenger); } } }
EconomicFlight.js
import { Flight } from "./Flight.mjs"; import { Reservation } from "./Reservation.mjs"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); this.passengers = []; } sellTicket(passenger) { if (this.capacity > 0) { // Check passenger's age for discount const hasDiscountPrice = passenger.age < 18 || passenger.age > 65; // Add passenger and reduce capacity by each indivudial this.passengers.push(passenger); this.capacity -= 1; // Add flight to passenger's data passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: hasDiscountPrice ? (this.price *= 0.8) : this.price, }); return new Reservation(this, passenger); } } }
Aquí mi solución: . . . . . . . . . . . .
Flight.js
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 = []; } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push({ fullName: passenger.name + " " + passenger.lastName, age: passenger.age }); this.capacity--; passenger.addFlight({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }) return new Reservation(this, passenger) } } }
Passenger.js
export class Passenger { constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } addFlight(flight) { this.flights.push(flight); } }
EconomicFlight.js
export class EconomicFlight extends Flight { sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) { this.price *= 0.80 } return super.sellTicket(passenger); } }
PremiumFlight.js
export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price) this.specialService = specialService } sellTicket(passenger) { this.price += this.specialService; return super.sellTicket(passenger); } }
Reservation.js
export class Reservation { constructor(flight, passenger) { this.flight = flight this.passenger = passenger } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: this.passenger.name + " " + this.passenger.lastName } } }
Solución. Reservation.js
export class Reservation { constructor(flight, passenger) { // Tu código aquí 👈 this.flight = flight; this.passenger = passenger; } reservationDetails() { // Tu código aquí 👈 return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: this.passenger.getFullName() } } }
Passenger.js
export class Passenger { // Tu código aquí 👈 constructor(name, lastName, age) { this.name = name; this.lastName = lastName; this.age = age; this.flights = []; } getFullName() { return this.name + ' ' + this.lastName; } addFlight(flight) { this.flights.push(flight); } }
Flight.js
import { Reservation } from "./Reservation"; export class Flight { constructor(origin, destination, date, capacity, price) { // Tu código aquí 👈 this.origin = origin; this.destination = destination; this.date = date; this.capacity = capacity; this.price = price; this.passengers = []; } sellTicket(passenger) { // Tu código aquí 👈 if (this.capacity > 0) { this.passengers.push({ fullName: passenger.getFullName(), age: passenger.age }); this.capacity--; const flight = { origin: this.origin, destination: this.destination, date: this.date, price: this.price }; passenger.addFlight(flight); return new Reservation(flight, passenger); } } }
EconomicFlight.js
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price); } sellTicket(passenger) { // Tu código aquí 👇 if (passenger.age < 18 || passenger.age > 65) { this.price -= (this.price * 0.20); } return super.sellTicket(passenger); } }
PremiumFlight.js
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { // Tu código aquí 👈 super(origin, destination, date, capacity, price); this.specialService = specialService; } sellTicket(passenger) { // Tu código aquí 👈 this.price += this.specialService; return super.sellTicket(passenger); } }
my expectacular code
Flight
import { Reservation } from "./Reservation.js"; 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 = [] } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age, }); this.capacity-- passenger.addFlight(this) return new Reservation(this, passenger); } } }
Passenger
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, }); } }
Reservation
export class Reservation { constructor(flight, passenger) { this.flight = flight this.passenger = passenger } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: this.passenger.name + ' ' + this.passenger.lastName } } }
EconomicFlight
import { Flight } from “./Flight.js”; import { Reservation } from “./Reservation.js”; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price) } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push(passenger) this.capacity– passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }) } if (passenger.age < 18 || passenger.age > 65) { this.price = this.price - (this.price * 0.20) } return new Reservation(this, passenger) } }
PremiumFlight
import { Flight } from “./Flight.js”; import { Reservation } from “./Reservation.js”; export class PremiumFlight extends Flight { constructor(origin, destination, date, capacity, price, specialService) { super(origin, destination, date, capacity, price) this.specialService = specialService } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push(passenger) this.capacity– passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }) } this.price += this.specialService return new Reservation(this, passenger) } }
Mi solución... Hay mucho de lo que no entiendo aún pero pude hacerlos con las instrucciones de la prueba y la ayuda de los aportes pude 😀 Flight
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 = [] } sellTicket(passenger) { if (this.capacity > 0) { this.passengers.push({ fullName: `${passenger.name} ${passenger.lastName}`, age: passenger.age }) passenger.flights.push({ origin: this.origin, destination: this.destination, date: this.date, price: this.price }) this.capacity-- return new Reservation(this, passenger) } } }
Passenger
export class Passenger { constructor(name, lastName, age) { this.name = name this.lastName = lastName this.age = age this.flights = [] } }
Reservation
export class Reservation { constructor(flight, passenger) { this.flight = flight this.passenger = passenger } reservationDetails() { return { origin: this.flight.origin, destination: this.flight.destination, date: this.flight.date, reservedBy: `${this.passenger.name} ${this.passenger.lastName}` } } }
PremiumFlight
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 + specialService) } }
EconomicFlight
import { Flight } from "./Flight"; import { Reservation } from "./Reservation"; export class EconomicFlight extends Flight { constructor(origin, destination, date, capacity, price) { super(origin, destination, date, capacity, price) } sellTicket(passenger) { if (passenger.age < 18 || passenger.age > 65) { this.price = this.price*0.8 } return super.sellTicket(passenger) } }