Created
February 6, 2020 11:18
-
-
Save jasongorman/31433f515cdcb4cec467e5b16918f9ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
function Warehouse(catalogue) { | |
this.catalogue = catalogue; | |
this.getCd = function (artist, title) { | |
return this.catalogue.find((cd) => cd.artist == artist && cd.title == title); | |
} | |
this.updatedCatalogue = function (cd, boughtCd) { | |
return Object.assign([], this.catalogue, { | |
[this.catalogue.indexOf(cd)]: boughtCd | |
}); | |
} | |
this.buyCd = function (artist, title, catalogue) { | |
const cd = this.getCd(artist, title, catalogue); | |
let boughtCd = { | |
...cd, | |
stock: cd.stock - 1 | |
}; | |
return {...this, catalogue: this.updatedCatalogue(cd, boughtCd)}; | |
}; | |
} | |
function CD(artist, title, stock) { | |
this.artist = artist; | |
this.title = title; | |
this.stock = stock; | |
this.getStock = function () { | |
return this.stock; | |
} | |
this.buy = function () { | |
return {...this, stock: this.stock - 1}; | |
}; | |
this.matches = function (artist, title) { | |
return this.artist == artist && this.title == title; | |
}; | |
}; | |
describe('CD Warehouse', () => { | |
it('reduces stock count by 1 of bought CD', () => { | |
let catalogue = [ | |
new CD('Peter Gabriel', 'So', 1) | |
]; | |
let warehouse = new Warehouse(catalogue); | |
warehouse = warehouse.buyCd('Peter Gabriel', 'So'); | |
assert.equal(warehouse.getCd('Peter Gabriel', 'So').getStock(), 0); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment