Skip to content

Instantly share code, notes, and snippets.

@Elnee
Last active May 21, 2018 17:27
Show Gist options
  • Select an option

  • Save Elnee/cd19fabed683b9812e14b59506623bf6 to your computer and use it in GitHub Desktop.

Select an option

Save Elnee/cd19fabed683b9812e14b59506623bf6 to your computer and use it in GitHub Desktop.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment