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
__tests__
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.js
and creating some instances in themain()
function.
- In
Bag.test.js
, import theBag
class 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
Bag
class - Test that the
weight
andid
have been assigned correctly. owner
: The person assigned to aBag
. Initialized with a value ofnull
.- Verify you can get the initial
owner
usinggetOwner
. - Create a test to update
owner
with aPerson
assigned to aBag
usingassignOwner()
. 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 thePerson
assigned to the bag.assignOwner(person)
Updatesowner
with aperson
assigned to aBag
.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Bag.test.js
.
- In
Person.test.js
, import thePerson
class fromPerson.js
that 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
Person
class - Test that the
name
anddestination
have been assigned correctly. - Test that
bags
initializes as an empty array. - Test that
addBag()
adds a bag to thebags
array.
- 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 ofBag
instances assigned to this person. This should initialize as an empty array.
- Methods
addBag(bag)
: Updates thebags
array 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 thePlane
class fromPlane.js
that 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
, anddestination
are assigned to the correct value. - Check that the
passengers
array initializes as an empty array. - Verify that you can add a
Person
to thepassengers
array 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 ofPassenger
instances assigned to this plane. This should initialize as an empty array.
- Methods
getPassengers()
: Returns the array ofpassengers
.addPassenger(passenger)
: Adds thepassenger
to thepassengers
array.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Plane.test.js
.
- In
Airport.test.js
, import theAirport
class fromAirport.js
that 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
name
andplanes
are assigned to the correct value. - Test that
airportCode
is the correct value - Check that the
planes
array initializes as an empty array. - Verify that you can add
Plane
objects 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 ofPlane
objects currently at the airport. Initializes as an empty array.
- Methods
getPlanes()
: Returns the array ofplanes
.addPlane(plane)
: Adds theplane
to theplanes
array.
- Properties
- Verify your tests work by running
npm test ./__tests__/classes/Airport.test.js
.