LoginSignup
0
0

More than 5 years have passed since last update.

OwnMemo VendingMachine

Posted at

It is my own memo.

The solidity is too difficult.
So, try creating vending machines with javascript and try smartContract understanding.

class Item {
  constructor({name, price}) {
    this.name = name;
    this.price = price;
  }
}

class VendingMachine {
  constructor() {
    this.stockMap = new Map();
    this.inputMoney = 0;
  }

  setItemList({itemList}) {
    itemList.forEach((item, itemId) => {
      this.stockMap.set(itemId, item);
    });
  }

  isInputedMoney({moneyNum}) {
    this.inputMoney = moneyNum;
    console.log(`input ${this.inputMoney} Yen`);
  }

  isPushedBtn({itemId}) {
    if(this.stockMap.has(itemId) !== true) {
      console.log(`not have item`);
      return;
    }
    const buyItem = this.stockMap.get(itemId);
    if(this.inputMoney < buyItem.price) {
      console.log(`insufficient money.`);
      return;
    }
    console.log(`Buy ${buyItem.name}`)
  }
}

//make Item
const cola = new Item({name: "Cola", price: 100});
const cider = new Item({name: "Cider", price: 100});
const orangeJuice = new Item({name: "OrangeJuice", price: 100});

//make vendingMachine
const vendingMachine = new VendingMachine();
vendingMachine.setItemList({itemList: [cola, cider, orangeJuice]});

//run code
vendingMachine.isInputedMoney({moneyNum: 100});
vendingMachine.isPushedBtn({itemId: 1})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0