This commit is contained in:
Martin Donnelly 2020-08-11 21:12:17 +01:00
parent e6b7c987d8
commit 59e89318de
4 changed files with 61 additions and 14 deletions

View File

@ -2,8 +2,10 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="8f59754f-8b0b-4496-b8f6-b19be929e084" name="Default Changelist" comment=""> <list default="true" id="8f59754f-8b0b-4496-b8f6-b19be929e084" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/tests/buyer.test.js" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Buyer.js" beforeDir="false" afterPath="$PROJECT_DIR$/Buyer.js" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Buyer.js" beforeDir="false" afterPath="$PROJECT_DIR$/Buyer.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Seller.js" beforeDir="false" afterPath="$PROJECT_DIR$/Seller.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/buyer.test.js" beforeDir="false" afterPath="$PROJECT_DIR$/tests/buyer.test.js" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -73,7 +75,7 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1597136254764</updated> <updated>1597136254764</updated>
<workItem from="1597136255857" duration="11000" /> <workItem from="1597136255857" duration="11000" />
<workItem from="1597136289338" duration="7691000" /> <workItem from="1597136289338" duration="11990000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -81,22 +83,30 @@
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state width="2463" height="519" key="GridCell.Tab.0.bottom" timestamp="1597146833789"> <state x="1085" y="461" width="429" height="542" key="FileChooserDialogImpl" timestamp="1597154405421">
<screen x="0" y="29" width="2560" height="1411" /> <screen x="0" y="29" width="2560" height="1411" />
</state> </state>
<state width="2463" height="519" key="GridCell.Tab.0.bottom/0.29.2560.1411@0.29.2560.1411" timestamp="1597146833789" /> <state x="1085" y="461" width="429" height="542" key="FileChooserDialogImpl/0.29.2560.1411@0.29.2560.1411" timestamp="1597154405421" />
<state width="2463" height="519" key="GridCell.Tab.0.center" timestamp="1597146833789"> <state width="2183" height="383" key="GridCell.Tab.0.bottom" timestamp="1597176719490">
<screen x="0" y="29" width="2560" height="1411" /> <screen x="0" y="29" width="2560" height="1411" />
</state> </state>
<state width="2463" height="519" key="GridCell.Tab.0.center/0.29.2560.1411@0.29.2560.1411" timestamp="1597146833789" /> <state width="2183" height="383" key="GridCell.Tab.0.bottom/0.29.2560.1411@0.29.2560.1411" timestamp="1597176719490" />
<state width="2463" height="519" key="GridCell.Tab.0.left" timestamp="1597146833788"> <state width="2183" height="383" key="GridCell.Tab.0.center" timestamp="1597176719490">
<screen x="0" y="29" width="2560" height="1411" /> <screen x="0" y="29" width="2560" height="1411" />
</state> </state>
<state width="2463" height="519" key="GridCell.Tab.0.left/0.29.2560.1411@0.29.2560.1411" timestamp="1597146833788" /> <state width="2183" height="383" key="GridCell.Tab.0.center/0.29.2560.1411@0.29.2560.1411" timestamp="1597176719490" />
<state width="2463" height="519" key="GridCell.Tab.0.right" timestamp="1597146833789"> <state width="2183" height="383" key="GridCell.Tab.0.left" timestamp="1597176719489">
<screen x="0" y="29" width="2560" height="1411" /> <screen x="0" y="29" width="2560" height="1411" />
</state> </state>
<state width="2463" height="519" key="GridCell.Tab.0.right/0.29.2560.1411@0.29.2560.1411" timestamp="1597146833789" /> <state width="2183" height="383" key="GridCell.Tab.0.left/0.29.2560.1411@0.29.2560.1411" timestamp="1597176719489" />
<state width="2183" height="383" key="GridCell.Tab.0.right" timestamp="1597176719490">
<screen x="0" y="29" width="2560" height="1411" />
</state>
<state width="2183" height="383" key="GridCell.Tab.0.right/0.29.2560.1411@0.29.2560.1411" timestamp="1597176719490" />
<state x="799" y="369" key="SettingsEditor" timestamp="1597155550499">
<screen x="0" y="29" width="2560" height="1411" />
</state>
<state x="799" y="369" key="SettingsEditor/0.29.2560.1411@0.29.2560.1411" timestamp="1597155550499" />
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/coding_challenge$buyer_test_js.info" NAME="buyer.test.js Coverage Results" MODIFIED="1597143325899" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="JestJavaScriptTestRunnerCoverage" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" /> <SUITE FILE_PATH="coverage/coding_challenge$buyer_test_js.info" NAME="buyer.test.js Coverage Results" MODIFIED="1597143325899" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="JestJavaScriptTestRunnerCoverage" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" />

View File

@ -11,10 +11,10 @@ class Buyer {
let sellers = []; let sellers = [];
// build up the initial list // build up the initial list
this.market.sellers.forEach(seller => { this.market.sellers.forEach((seller, i) => {
if (seller.inventory.hasOwnProperty(product)) { if (seller.inventory.hasOwnProperty(product)) {
const price = seller.quote(product); const price = seller.quote(product);
sellers.push({id:seller.id, price:price}) sellers.push({id: seller.id, price: price, index: i, quantity:seller.inventory[product].quantity})
} }
}); });
@ -26,6 +26,7 @@ class Buyer {
return sellers; return sellers;
} }
/** /**
* This method should get the best price for a given product * This method should get the best price for a given product
* across all sellers * across all sellers
@ -51,18 +52,33 @@ class Buyer {
*/ */
fillWithBestPrices(product, quantity) { fillWithBestPrices(product, quantity) {
let wantedQuantity = quantity;
let sellerPreference = this.buildOrderPreference(product); let sellerPreference = this.buildOrderPreference(product);
console.log(sellerPreference); console.log(sellerPreference);
while(sellerPreference.length > 0) { let reciept = [];
while (sellerPreference.length > 0 && wantedQuantity > 0) {
let seller = sellerPreference.shift(); let seller = sellerPreference.shift();
let r = this.market.sellers[seller.index].sell(product, wantedQuantity);
wantedQuantity = (wantedQuantity - r.boughtQuantity) < 0 ? 0 : (wantedQuantity - r.boughtQuantity);
reciept.push(r);
} }
console.log(reciept);
console.log(reciept.reduce((a, cv) => {
return a.cost + cv.cost;
})
);
throw Error("Not Implemented"); throw Error("Not Implemented");
} }

View File

@ -53,6 +53,7 @@ class Seller {
tick() { tick() {
console.log('tick', this);
for (let [product, value] of Object.entries(this.inventory)) { for (let [product, value] of Object.entries(this.inventory)) {
let inventory = value; let inventory = value;
const isReadyForDelivery = (inventory.priceHistory.length % this.deliveryWait) == 0; const isReadyForDelivery = (inventory.priceHistory.length % this.deliveryWait) == 0;

View File

@ -37,11 +37,31 @@ describe("Buyer", function () {
}); });
it("fill", () => { it("fill 10 apples", () => {
let buyer = new Buyer(market);
expect(buyer.fillWithBestPrices('Apples', 10)).toEqual(42.5);
});
it("fill 50 apples", () => {
let buyer = new Buyer(market); let buyer = new Buyer(market);
expect(buyer.fillWithBestPrices('Apples', 50)).toEqual(0); expect(buyer.fillWithBestPrices('Apples', 50)).toEqual(0);
}); });
it("fill 100 apples", () => {
let buyer = new Buyer(market);
expect(buyer.fillWithBestPrices('Apples', 100)).toEqual(0);
});
it("fill 1000 apples", () => {
let buyer = new Buyer(market);
expect(buyer.fillWithBestPrices('Apples', 100)).toEqual(0);
});
}); });