题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
// 商品
const goods = [
    {name: 'A1', price: 2, num: 0},
    {name: 'A2', price: 3, num: 0},
    {name: 'A3', price: 4, num: 0},
    {name: 'A4', price: 5, num: 0},
    {name: 'A5', price: 8, num: 0},
    {name: 'A6', price: 6, num: 0},
]
// 钱
const moneyBox = [
    {value: '1', num: 0},
    {value: '2', num: 0},
    {value: '5', num: 0},
    {value: '10', num: 0},
]
let totalMoney = 0
rl.on('line', function (line) {
    const data = line.split(';').filter(item=>item)
    data.forEach(item=>{
        const tempArr = item.split(' ')
        const key = tempArr[0]
        switch(key) {
            //初始化
            case 'r':
                const goodsNum = tempArr[1].split('-')
                const moneyNum = tempArr[2].split('-')
                goodsNum.map((item, index)=>{
                    goods[index].num = item
                })
                moneyNum.map((item, index)=>{
                    moneyBox[index].num = item
                })
                console.log('S001:Initialization is successful')
                break
            //投币
            case 'p':
                const moneyValue = Number(tempArr[1])
                if(![1, 2, 5, 10].includes(moneyValue)) {
                    console.log('E002:Denomination error')
                } else if ((calc1and2()<moneyValue) && ![1,2].includes(moneyValue)) {
                    console.log('E003:Change is not enough, pay fail')
                } else if(calcGoodsTotalNum()==0) {
                    console.log('E005:All the goods sold out')
                } else {
                    moneyBox.forEach(item=>{
                        if(+item.value==moneyValue) {
                            item.num++
                        }
                    })
                    totalMoney+=moneyValue
                    console.log(`S002:Pay success,balance=${totalMoney}`)
                }
                break
            //购买商品
            case 'b':
                const goodsName = tempArr[1]
                if(!goods.map(item=>item.name).includes(goodsName)) {
                    console.log('E006:Goods does not exist')
                } else if(findNowGoods(goodsName).num==0) {
                    console.log('E007:The goods sold out')
                } else if(totalMoney<findNowGoods(goodsName).price) {
                    console.log('E008:Lack of balance')
                } else {
                    totalMoney-=findNowGoods(goodsName).price
                    findNowGoods(goodsName).num--
                    console.log(`S003:Buy success,balance=${totalMoney}`)
                }
                break
            //退币
            case 'c':
                if(totalMoney==0) {
                    console.log('E009:Work failure')
                } else if(totalMoney>0) {
                    const res = countMoney(totalMoney)
                    updateMoneyBox(res)
                    for(let k in res) {
                        console.log(`${k} yuan coin number=${res[k]}`)
                    }
                    totalMoney = 0
                }
                break
            //查询类别
            case 'q':
                const num = tempArr[1]
                // 商品0 存钱盒1
                if(num==0) {
                    goods.forEach(item=>{
                        console.log(`${item.name} ${item.price} ${item.num}`)
                    })
                } else if(num==1) {
                    moneyBox.forEach(item=>{
                        console.log(`${item.value} yuan coin number=${item.num}`)
                    })
                } else {
                    console.log('E010:Parameter error')
                }
                break
            default:
                console.log('E010:Parameter error')
        }
    })
});
// 计算1和2元钱币总和
function calc1and2() {
    const money1 = moneyBox[0]
    const money2 = moneyBox[1]
    return +money1.value * money1.num + +money2.value * money2.num
}
// 计算商品总数量
function calcGoodsTotalNum() {
    let total = 0
    goods.map(item=>{
        total+=item.num
    })
    return total
}
// 找出当前商品
function findNowGoods(name) {
    return goods.filter(item=>name==item.name)[0]
}
// 找零钱
function countMoney(num) {
    let res = {
        '1': 0,
        '2': 0,
        '5': 0,
        '10': 0,
    }
    const c10 = Math.floor(num/10)
    if(moneyBox.filter(item=>item.value=='10')[0].num>=c10) {
        res['10'] = c10
    } else {
        res['10'] = moneyBox.filter(item=>item.value=='10')[0].num
    }
    num = num - res['10']*10
    const c5 = Math.floor(num/5)
    if(moneyBox.filter(item=>item.value=='5')[0].num>=c5) {
        res['5'] = c5
    } else {
        res['5'] = moneyBox.filter(item=>item.value=='5')[0].num
    }
    num = num - res['5']*5
    const c2 = Math.floor(num/2)
    if(moneyBox.filter(item=>item.value=='2')[0].num>=c2) {
        res['2'] = c2
    } else {
        res['2'] = moneyBox.filter(item=>item.value=='2')[0].num
    }
    num = num - res['2']*2
    const c1 = num
    if(moneyBox.filter(item=>item.value=='1')[0].num>=c1) {
        res['1'] = c1
    } else {
        res['1'] = moneyBox.filter(item=>item.value=='1')[0].num
    }
    return res
}
//更新盒子钱数
function updateMoneyBox(res) {
    for(let k in res) {
        switch(k) {
            case '1':
                moneyBox[0].num-=res[k]
                break
            case '2':
                moneyBox[1].num-=res[k]
                break
            case '5':
                moneyBox[2].num-=res[k]
                break
            case '10':
                moneyBox[3].num-=res[k]
                break
        }
    }
}

全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务