As you arrive at the airport, your excitement grows. You're not just going on a trip, but embarking on an adventure into the world of Object-Oriented Programming. Your destination? Creating classes and unit tests for airports, persons, planes, and bags.
In this activity, you'll be practicing object-oriented programming (OOP) by creating classes with attributes and methods that model real-world entities. Additionally, you'll also be writing unit tests to ensure that they're functioning as expected. By the end of this activity, you'll have not only a solid understanding of OOP principles but also a set of classes that you can use to build more complex programs in the future. So let's get ready to take off and start coding!
GOAL: You should:
- Write tests for each class in the corresponding test file in the
__test__folder (e.g. Bag tests should be written in Bag.test.js) - Create code that meets the design specifications outlined in the class diagram above and the more detailed specs outlined below.
- Feel free to try your code out by importing classes in
main.jsand creating some instances in themain()function.
- In
Bag.test.js, import theBagclass fromBag.js(it has already been exported for you). - In
Bag.test.js, construct the tests that will verify that you have included all of the required components outlined in the class diagram above. A few to consider using:- Test that you can create an instance of the
Bagclass - Test that the
weightandidhave been assigned correctly. owner: The person assigned to aBag. Initialized with a value ofnull.- Verify you can get the initial
ownerusinggetOwner. - Create a test to update
ownerwith aPersonassigned to aBagusingassignOwner(). You won't be able to assign a person until you completing the next section.
- Test that you can create an instance of the
- In
Bag.js, create code that meets the following specifications:- Properties
weight: The weight of the bag.id: An id for the bag.
- Methods
getOwner(): Returns thePersonassigned to the bag.assignOwner(person)Updatesownerwith apersonassigned to aBag.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Bag.test.js.
- In
Person.test.js, import thePersonclass fromPerson.jsthat has been created for you. - In
Person.test.js, construct the tests that will verify that you have included all of the required components outlined in the class diagram above. A few to consider using:- Test that you can create an instance of the
Personclass - Test that the
nameanddestinationhave been assigned correctly. - Test that
bagsinitializes as an empty array. - Test that
addBags()adds a bag to thebagsarray.
- Test that you can create an instance of the
- In
Person.js, create code that meets the following specifications:- Properties
name: The name of the person.destination: The destination the person is traveling to.bags: An array ofBaginstances assigned to this person. This should initialize as an empty array.
- Methods
addBag(bag): Updates thebagsarray with a bag.getBags(): Returns the array of `bags.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Person.test.js.
- In
Plane.test.js, import thePlaneclass fromPlane.jsthat has been created for you. - In
Plane.test.js, construct the tests that will verify that you have included all of the required components outlined in the class diagram above. A few to consider using:- Test that
company,origin, anddestinationare assigned to the correct value. - Check that the
passengersarray initializes as an empty array. - Verify that you can add a
Personto thepassengersarray usingaddPassenger.
- Test that
- In
Plane.js, create code that meets the following specifications:- Properties
company: The company that operates the Plane.origin: The origin set to the static property ofAirport.airportCode(created in the next section).destination: The destination the plane is traveling to.passengers: An array ofPassengerinstances assigned to this plane. This should initialize as an empty array.
- Methods
getPassengers(): Returns the array ofpassengers.addPassenger(passenger): Adds thepassengerto thepassengersarray.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Plane.test.js.
- In
Airport.test.js, import theAirportclass fromAirport.jsthat has been created for you. - In
Airport.test.js, construct the tests that will verify that you have included all of the required components outlined in the class diagram above. A few to consider using:- Test that
nameandplanesare assigned to the correct value. - Test that
airportCodeis the correct value - Check that the
planesarray initializes as an empty array. - Verify that you can add
Planeobjects to the planes array usingaddPlane.
- Test that
- In
Plane.js, create code that meets the following specifications:- Properties
name: The name of the Airport.airportCode: A static property with the three letter code for the airport (e.g. JFK or LHR).planes: An array ofPlaneobjects currently at the airport. Initializes as an empty array.
- Methods
getPlanes(): Returns the array ofplanes.addPlane(plane): Adds theplaneto theplanesarray.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Airport.test.js.

