【freecodecamp】 Exact Change 收银机程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/towrabbit/article/details/81295184

要求

设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数.

cid 是一个二维数组,存着当前可用的找零.

当收银机中的钱不够找零时返回字符串 “Insufficient Funds”. 如果正好则返回字符串 “Closed”.

否则, 返回应找回的零钱列表,且由大到小存在二维数组中.
比如:
checkCashRegister(3.26, 100.00, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.10], [“QUARTER”, 4.25], [“ONE”, 90.00], [“FIVE”, 55.00], [“TEN”, 20.00], [“TWENTY”, 60.00], [“ONE HUNDRED”, 100.00]]) 应该返回
[[“TWENTY”, 60.00], [“TEN”, 20.00], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.50], [“DIME”, 0.20], [“PENNY”, 0.04]].

function checkCashRegister(price, cash, cid) {
var change = cash-price;//要找的数值
  var sum = 0 ;
  var cashObject = function(options){
    this.name = options.name || "none";
    this.number = options.number || 0;
    this.singlePrice = options.singlePrice || 0;
    this.sumPrice = options.sumPrice || 0;
    this.id = 0;
  };
  cashObject.prototype.countNumber = function(){
    if(this.sumPrice !==0 && this.singlePrice !== 0 ){
      this.number = Math.round(this.sumPrice/this.singlePrice);
    }
  };

  //一个输入收银机现有的钱以及要找的钱并确定钱够不够找的函数 采用输入 所有的钱的对象组合 不够找返回"无法找钱" 能够找返回要怎么找钱(按照题目要求是返回找钱的数组)
  var back = [];//要找回去的钱 数组形式
  var backObj = {};//要找回的钱 对象形式 不同面值货币作为键值对应个数
  function getReturned(cashObjs,change){
    //一个函数 如果直接能用大的钱找那么就直接用大的钱如果不够去找小的钱够不够找
    for(var i = biggestCash.id; i > 0 ; i -- ){
        var currentCashObj = cashObjs[i-1];

        var CashNumInNeed = Math.floor(change/currentCashObj.singlePrice);

        //如果足够
        if(CashNumInNeed !== 0 && CashNumInNeed <= currentCashObj.number){
            change = change - CashNumInNeed*currentCashObj.singlePrice;
            backObj[currentCashObj.name] = CashNumInNeed;
            back.push([currentCashObj.name,CashNumInNeed*currentCashObj.singlePrice]);
        }else if(CashNumInNeed > currentCashObj.number){//如果不够就找当前钱币值下最多的
            change = change - currentCashObj.sumPrice;
            backObj[currentCashObj.name] = currentCashObj.number;
            back.push([currentCashObj.name,currentCashObj.sumPrice]);
        }
        //如果完成了找钱

        //保留后两位
        change = Math.round(change*100)/100;
        if(change === 0)return back;

    }
    if(change !== 0 )return "Insufficient Funds";
  }
  cashObject.prototype.getSinglePrice = function(){
    if(this.name){
      switch(this.name){
        case "PENNY":
          this.singlePrice = 0.01;
          this.id = 1;
          break;
        case "NICKEL":
          this.singlePrice = 0.05;
          this.id = 2;
          break;
        case "DIME":
          this.singlePrice = 0.1;
          this.id = 3;
          break;
        case "QUARTER":
          this.singlePrice = 0.25;
          this.id = 4;
          break;
        case "ONE":
          this.singlePrice = 1;
          this.id = 5;
          break;
        case "FIVE":
          this.singlePrice = 5;
          this.id = 6;
          break;
        case "TEN":
          this.singlePrice = 10;
          this.id = 7;
          break;
        case "TWENTY":
          this.singlePrice = 20;
          this.id = 8;
          break;
        case "ONE HUNDRED":
          this.singlePrice = 100;
          this.id = 9;
          break;
        default:
          this.singlePrice = 0;
      }
    }
  };
  var cashObjects = [];
  var smallestCash,biggestCash;
  //生成一个有钱币面值和数量等信息的 不同面值的钱的对象数组
  for(var i = 0 ; i < cid.length; i ++ ){
    var newCashObj = new cashObject({
      name:cid[i][0],
      sumPrice:cid[i][1],
    });
    newCashObj.getSinglePrice();
    newCashObj.countNumber();
    if(smallestCash === undefined && newCashObj.number !== 0){
      smallestCash = newCashObj;
    }
    if(newCashObj.number !== 0){biggestCash = newCashObj;}
    cashObjects.push(newCashObj);
  }

  //获取机器内所有的钱的总和
  for(var x = 0 ; x < cashObjects.length ; x++){
    sum += cashObjects[x].sumPrice;
  }

  sum = Math.round(sum*100)/100;
  //如果正好则返回"Closed"
  if(sum ===(cash - price) )return "Closed";
  var giveBack = getReturned(cashObjects,change);
  return giveBack;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

猜你喜欢

转载自blog.csdn.net/towrabbit/article/details/81295184
今日推荐