2020-08-11 11:55:43 +00:00
|
|
|
class Buyer {
|
|
|
|
constructor(market) {
|
|
|
|
this.market = market;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build an order preference table for particular product
|
|
|
|
* @param product
|
|
|
|
*/
|
|
|
|
buildOrderPreference(product) {
|
|
|
|
let sellers = [];
|
|
|
|
|
|
|
|
// build up the initial list
|
2020-08-11 20:12:17 +00:00
|
|
|
this.market.sellers.forEach((seller, i) => {
|
2020-08-11 11:55:43 +00:00
|
|
|
if (seller.inventory.hasOwnProperty(product)) {
|
|
|
|
const price = seller.quote(product);
|
2020-08-11 20:12:17 +00:00
|
|
|
sellers.push({id: seller.id, price: price, index: i, quantity:seller.inventory[product].quantity})
|
2020-08-11 11:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// sort by best price
|
|
|
|
sellers.sort((a, b) => {
|
|
|
|
return a.price - b.price;
|
|
|
|
})
|
|
|
|
|
|
|
|
return sellers;
|
|
|
|
}
|
2020-08-11 20:12:17 +00:00
|
|
|
|
2020-08-11 11:55:43 +00:00
|
|
|
/**
|
|
|
|
* This method should get the best price for a given product
|
|
|
|
* across all sellers
|
|
|
|
*/
|
|
|
|
getBestPrice(product) {
|
|
|
|
let lowestPrice = null;
|
|
|
|
this.market.sellers.forEach(seller => {
|
|
|
|
if (seller.inventory.hasOwnProperty(product)) {
|
|
|
|
const price = seller.quote(product);
|
|
|
|
if (lowestPrice === null || lowestPrice > price) lowestPrice = price;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return lowestPrice || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method should optimise price when filling an order
|
|
|
|
* if the quantity is greater than any single seller can accomodate
|
|
|
|
* then the next cheapest seller should be used.
|
|
|
|
*/
|
|
|
|
fillWithBestPrices(product, quantity) {
|
|
|
|
|
2020-08-11 20:12:17 +00:00
|
|
|
let wantedQuantity = quantity;
|
2020-08-11 11:55:43 +00:00
|
|
|
let sellerPreference = this.buildOrderPreference(product);
|
|
|
|
|
|
|
|
console.log(sellerPreference);
|
|
|
|
|
2020-08-11 20:12:17 +00:00
|
|
|
let reciept = [];
|
|
|
|
|
|
|
|
while (sellerPreference.length > 0 && wantedQuantity > 0) {
|
2020-08-11 11:55:43 +00:00
|
|
|
|
|
|
|
let seller = sellerPreference.shift();
|
|
|
|
|
2020-08-11 20:12:17 +00:00
|
|
|
let r = this.market.sellers[seller.index].sell(product, wantedQuantity);
|
|
|
|
wantedQuantity = (wantedQuantity - r.boughtQuantity) < 0 ? 0 : (wantedQuantity - r.boughtQuantity);
|
2020-08-11 11:55:43 +00:00
|
|
|
|
2020-08-11 20:12:17 +00:00
|
|
|
reciept.push(r);
|
2020-08-11 11:55:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:12:17 +00:00
|
|
|
console.log(reciept);
|
|
|
|
|
|
|
|
console.log(reciept.reduce((a, cv) => {
|
|
|
|
return a.cost + cv.cost;
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
2020-08-11 11:55:43 +00:00
|
|
|
throw Error("Not Implemented");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method should optimise for sellers with the largest inventory when filling an order
|
|
|
|
* if the quantity is greater than any single seller can accomodate
|
|
|
|
* then the next largest seller should be used.
|
|
|
|
* if multiple sellers have the same amount of inventory
|
|
|
|
* you should use the cheaper of the two.
|
|
|
|
*/
|
|
|
|
fillWithLargestSellers(product, quantity) {
|
|
|
|
throw Error("Not Implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {Buyer}
|