说明
实现一个简单的仓储系统,可以不断转入和转出货物,货物最多有两级,数字代表转入或者转出的数量,转出时,不能出现爆仓的情况。
测试用例
{productA:{a:1,b:2,e:3,c:{c1:23}}productB:{e:6}}//爆仓情况{productA:{a:3}}
类
class Depository{constructor(options){}transferIn(cargo){}transferOut(cargo){}}
class Depository{constructor(options){const {size} = optionsthis.size = isNaN(Number(size)) ? 0 :Number(size)this.products = {}}transferIn(cargo){let flag = true, msg ='入货成功'for(let pro in cargo) {if(typeof cargo[pro] === 'number'){if(typeof this.products[pro] === 'object'){return {flag:false,msg:'货物类型不正确'}}if(this.products[pro]){this.products[pro] += cargo[pro]}else{this.products[pro] = cargo[pro]}}else{if(typeof this.products[pro] === 'number'){return {flag:false,msg:'货物类型不正确'}}this.products[pro] = {}for(let subPro in cargo[pro]){if(this.products[pro][subPro]){this.products[pro][subPro] += cargo[pro][subPro]}else{this.products[pro][subPro] = cargo[pro][subPro]}}}}return {flag,msg ,products:this.products}}transferOut(cargo){let flag = true, msg ='出货成功'for(let pro in cargo) {if(typeof cargo[pro] === 'number'){if(typeof this.products[pro] === 'object'){flag = falsereturn {flag,msg :'货物类型不正确'}}if(this.products[pro]){if(this.products[pro] - cargo[pro]< 0 ){return {flag:false ,msg :'爆仓了:'+pro + '不够了'}}this.products[pro] -= cargo[pro]}else{return {flag:false ,msg :'爆仓了:'+pro + '不够了'}}}else{if(typeof this.products[pro] === 'number' ||!this.products[pro]){return {flag,msg :'货物类型不正确'}}for(let subPro in cargo[pro]){if(this.products[pro][subPro]){if(this.products[pro][subPro] - cargo[pro][subPro]< 0 ){return {flag:false ,msg :'爆仓了:'+pro+':'+subPro + '不够了'}}this.products[pro][subPro] -= cargo[pro][subPro]}else{return {flag,msg :'货物类型不正确'}}}}}return {flag,msg}}}const de = new Depository({})console.log(de.transferIn({a:234}))console.log(de.transferIn({b:234}))console.log(de.transferIn({b:{e:34}}))console.log(de.transferOut({a:23}))console.log(de.transferOut({b:3}))console.log(de.transferIn({c:{ef:34}}))console.log(de.transferOut({c:{ef:348}}))
https://codepen.io/robinson90/pen/YzGpEjy
优化之后
1 针对嵌套的结构提供扁平化结构,提供商品类目扁平化的方法
2 针对仓储做key的私有化判断
3 针对入仓以及出仓的部分都做扁平化
class Depository {constructor(params = {}) {this.store = {};this.transferIn(this.flat(params));}flat(params = {}) {let final = {};function baseFlat(params, baseKey) {Object.keys(params).forEach((key) => {let value = params[key];let finalKey = baseKey ? `${baseKey}_${key}` : key;if (typeof value === "object" && value !== null) {baseFlat(value, finalKey);} else {final[finalKey] = value;}});return final;}return baseFlat(params);}transferIn(params = {}) {let flag = true,msg = "入货成功";const newParams = this.flat(params);Object.keys(newParams).forEach((key) => {if (this.store[key]) {this.store[key] += newParams[key];} else {this.store[key] = newParams[key];}});return {flag,msg,products: this.store,};}transferOut(params = {}) {let flag = true,msg = "出货成功";const newParams = this.flat(params);try {Object.keys(newParams).forEach((key) => {if (this.store[key] !== undefined) {if (this.store[key] - newParams[key] < 0) {const error = new Error("");error.info = {flag: false,msg: key + " 不够了",};throw error;}this.store[key] -= newParams[key];} else {const error = new Error("");error.info = {flag: false,msg: "没有这类货物",};throw error;}});} catch (error) {return {flag: false,msg: error.info.msg,};}return {flag,msg,};}}
备注:相比而言,新的扁平化之后的结构,也在商品类目上支持了同父类目既可以是商品,也可以是商品父类。
