計算超市產品的合適補貨數量,需要考慮以下參數:
超市產品的當前庫存
超市產品的最大庫存容量
紙箱數量
補貨數量必須是“Carton Quantity”的倍數。 如果紙箱數量為 24,補貨數量必須為 24、48、72 等。
補貨門檻
僅當“當前庫存”低於“補貨閾值”時才會進行補貨。
您可以用 JAVA / Javascript 或任何其他編程語言回答。
a) 請提供一個函數來計算超市產品的合適補貨數量。
例如
產品:可口可樂
當前庫存:100
最大庫存容量:200
紙箱數量:24
補貨門檻:120
預計補貨數量:96
b) 請為 (a) 部分的答案提供單元測試函數。
(a)
let fn=(nowitem,maxitem,additem,limititem)=>{
    if(nowitem<limititem){
        let returnitem=0;
        while((returnitem+nowitem+additem)<maxitem){
            returnitem+=additem;
        }
        return returnitem;
    }
    return 0;
}
最近在練習design patterns,所以試著用design patterns寫看看。
有甚麼地方可以改進,再麻煩指點
//try.js
class Product {
    constructor(name = '', currentInventory = 0) {
        this.name = name,
        this.currentInventory = currentInventory
    }
    setReplenishmentThreshold(replenishmentThreshold = 0) {
        this.replenishmentThreshold = replenishmentThreshold
    }
}
class WareHouse{
    constructor(maxInventoryCapacity=200, numberOfCartons=1){
        this.maxInventoryCapacity = maxInventoryCapacity,
        this.numberOfCartons = numberOfCartons
    }
}
let create = {
    'product': function (name, currentInventory) {
        return new Product(name, currentInventory)
    },
    'warehouse': function (maxInventoryCapacity, numberOfCartons) {
        return new WareHouse(maxInventoryCapacity, numberOfCartons)
    }
}
//suitNumber = function(要進貨的商品、補貨門檻、要進貨的倉庫 )
let suitNumber = function (countProduct, replenishmentThreshold, warehouse) {
    let replenishmentQuantity = 0 //要進貨的數量
    countProduct.setReplenishmentThreshold(replenishmentThreshold)//設定補貨門檻
    if (countProduct.replenishmentThreshold >= countProduct.currentInventory) {
        let multiple = parseInt((warehouse.maxInventoryCapacity - countProduct.currentInventory) / (warehouse.numberOfCartons))
        replenishmentQuantity = multiple * (warehouse.numberOfCartons)
        console.log('multiple',multiple);
    }
    return replenishmentQuantity
} 
export default{
    create,
    suitNumber
}
//try.test.js
import tryjs from './js/try.js';
let cola = tryjs.create.product('可口可樂', 100) //名稱、現在數量
let wareHouse = tryjs.create.warehouse(200, 24) //倉庫的最大值、目前紙箱數量
let suitNumber = tryjs.suitNumber(cola,120,wareHouse)//要進貨的商品、補貨門檻、要進貨的倉庫
test('要進貨的數量 ', () => {
    expect(suitNumber).toBe(96)
})