— javaScript — 1 min read
1// Declare the class 2class Books {3 /* constructor() */4 //Use a constructor method to set up the template for the object5 constructor(title, author, ISBN, copies){6 this.title = title;7 this.author = author8 this.ISBN = ISBN9 this.copies = copies10 }11 /*avail()*/12 //Set up a getter method to run the getAvailability method13 get avail() {14 return this.getAvailability();15 }1617 /*getAvailability()*/18 // This method checks if there are no copies, low on stock, or in stock.19 getAvailability(){20 if(this.copies === 0){21 return 'out of stock'22 } else if ( this.copies < 10 && this.copies > 0){23 return 'low stock'24 }25 return 'In stock' 26 }2728 /*sell()*/29 /*This method decreases the amount of copies by the number of books sold. You can pass the number of books sold as an 30 argument. It also defaults to 1 if no argument is given */31 sell(numSold = 1){32 this.copies -= numSold3334 return `${this.copies} left in inventory`35 }3637 /*restock()*/38 /* This method will increase the amount of copies based the number you pass in as an aarugument. It also defaults39 to 5 if no argument is given */40 restock(numCopies = 5){41 this.copies += numCopies42 return `You now have ${this.copies} in stock.`43 }4445 46}4748/* TEST TEST TEST */49// Instantiate a new book50let book1 = new Books('Harry Potter & The Sorcerors Stone', 'JK Rowling', '1234asdf', 20 )51//Checking to make sure the book is all there.52console.log(book1)53//checking to see how many books are avail54console.log(book1.avail())55// Testing the sell method56console.log(book1.sell(5))57//Restock method58console.log(book1.restock(20))
Something wrong here? Create an issue