Elnee
5/21/2018 - 3:25 PM

JavaScript OOP

JavaScript OOP

class Printer {
  constructor(label = "Unknown", pageSize = "A4", amountOfDye = 100, amountOfPaper = 50, powerState = "off") {
    this.label = label;
    this.pageSize = pageSize;
    this.amountOfDye = amountOfDye;
    this.amountOfPaper = amountOfPaper;
    this.powerState = powerState;
  }
  addPaper(amount) {
    this.amountOfPaper += amount;
  }
  fillCartridge(amount) {
    this.amountOfDye += amount;
  }
  turnOn() {
    this.powerState = "on";
  }
  turnOff() {
    this.powerState = "off";
  }
  setPageSize(size) {
    this.pageSize = size;
  }
  print(amount) {
    if (this.powerState != "on") {
      console.log("Printer is off");
      return;
    }
    for (let i = 0; i < amount; i++) {
      if (this.amountOfDye == 0)
        console.log("Fill the cartridge!");
      if (this.amountOfPaper == 0)
        console.log("Please, add paper to printer");
      if (this.amountOfDye == 0 || this.amountOfPaper == 0) return;
      this.amountOfDye -= 1;
      this.amountOfPaper -= 1;
    }
  }
  info() {
    console.log("Name: ", this.label);
    console.log("Amount of paper: ", this.amountOfPaper);
    console.log("Amount of dye: ", this.amountOfDye);
    console.log("Page size: ", this.pageSize);
    console.log("Power state: ", this.powerState);
  }
}

let printer = new Printer();