This repository is an assignment from the OOP track of the Manchester Codes Syllabus. The assignment is an opportunity to explore and practice:
- The Git/GitHub workflow
- Implementing Code in reference to User Stories
- "Mocks and Spies" in the Jest TDD framework
- Object Oriented Programming using class syntax & Prototypical Inheritance
- Higher Order Functions & Built in Methods
- ES6 Modules
- Dependency Injection & Inversion
- Conditional logic
- Implementing Guard Clauses & Throwing exceptions
- Code Refactoring
Over the next couple of weeks, we are going to cover further aspects of Object-oriented Programming - namely how we pass objects into one another and test for this, how we decide what methods and properties our objects should have, how we isolate objects in tests, and how we test behaviour that's outside of our control.
This week you are going to be building and working with many objects that allow a cruise ship to operate. Hopefully we don't hit any icebergs on the way ๐.
-
Clone the repository:
- via http:
git clone https://github.com/mike-shields-dev/cruise-ships.git
- via ssh:
git clone [email protected]:mike-shields-dev/cruise-ships.git
-
Install dependencies:
cd cruise-ships && npm install
-
Testing:
This repository utilises the Jest JavaScript testing framework.
Test file is located in the
__tests__
directory.To run the test suite once:
npm test
To automatically run the tests as changes are made to the code:
npm run test:watch
When creating Ship instances, an Itinerary instance must be provided. The Itinerary instance in turn must be provided with an array of Port instances.
Throughout the life cycle of the ship, it will proceed to "visit" each port in the provided itinerary, in turn, until all ports have been visited.
const port = new Port("Amsterdam");
port.name // "Amsterdam"
name
: The name of the portships
: An array of ships currently docked at this port
Note: It is solely the responsibility of a ship
to invoke the port
methods below
addShip
: Adds the givenship
toships
array:
port.addShip(ship)
ports.ships // [ship]
removeShip
: Removes the givenship
fromships
array:
port.ships // [ship]
port.removeShip(ship)
port.ships // []
const ports = [port1, port2, port3]
const itinerary = new Itinerary(ports);
itinerary.ports // [port1, port2, port3]
Creating a Ship instance
const ship = new Ship(itinerary)
itinerary
: The provideditinerary
of portscurrentPort
: Theport
at which theship
is currently dockedpreviousPort
: Theport
at which theship
was previously docked
-
setSail
:- Removes
ship
from theships
array of the currentport
- Sets
previousPort
to be the value ofcurrentPort
- Sets
currentPort
to equalnull
const ship = new Ship(itinerary) ship.previousPort // null ship.currentPort // itinerary[0] ship.currentPort.ports // [ship] ship.setSail() ship.previousPort // itinerary[0] ship.previousPort.ports // [] ship.currentPort // null
- Removes
-
dock
:- Adds
ship
to theports
array of the nextport
in theitinerary
- Sets
currentPort
to be the nextport
in theitinerary
// ...continues from setSail code example above ship.dock() ship.currentPort // ship.itinerary[1] ship.currentPort.ports // [ship]
- Adds