In: Computer Science
This is for Javascript programming language:
A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object.
The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects.
B. Methods to add Library Class
The class should have the following methods:
o A function named simulate. In this function create a while loop that runs 30 times to simulate 30 days. Each day, iterate over all the books and checkout the book if it is not checked out, check it in if it is. Book Class
o A method named is CheckedOut that returns true if the book is checked out and false if not. o A method named CheckOut that displays “Checking Out … ” Be sure to replace with the actual title of the book.
o A method named CheckIn that displays “Checking In …” Be sure to replace with the actual title of the book.
C. Test Program After you have created the classes.
Create three Book objects and store the following data in them:
Title
Author
Item #1 Code Complete Steve McConnell
Item #2 The Art of Unit Testing Roy Osherove
Item #3 Domain Driven Design Eric Evans Store all three Book objects in an array named “catalog” in the Library class.
Next, create a Library object.
Now, call the simulate function on the library object you just created.
// Class Book to store title and author
class Book{
//constructor to accept title and author
constructor(_title, _author){
this.title = _title;
this.author = _author;
}
}
//Class library to store array of books
class Library{
//constructor that takes an array of books
constructor(_books){
this.catalog = _books;
//create an array to store the status of each book
//assign an initial value false to all the books
//checked out --> True, Checked in --> False
this.status = new Array(_books.length).fill(false);
}
//method simulate to simulate checkin and checkout
simulate(){
//use while loop for 30 days
let days = 30;
while(days > 0){
//for each book in the library
for (let i = 0; i < this.catalog.length; i++){
//check if the booked is checked out?
if(this.CheckedOut(i)){
//check in the book
this.CheckIn(i);
}
else{
//check out the book
this.CheckOut(i);
}
}
//decrement the days
days--;
}
}
//Method CheckedOut, accepts index of the book
CheckedOut(index){
//return the status
return this.status[index];
}
//Method CheckOut
CheckOut(index){
//get the book title
let title = this.catalog[index].title;
//get the book author
let author = this.catalog[index].author;
//display the output
console.log(`Checking Out: \n-------------\nTitle : ${title}\nAuthor: ${author}\n`);
//change the status
this.status[index] = true;
}
//Method CheckIn
CheckIn(index){
//get the book title
let title = this.catalog[index].title;
//get the book author
let author = this.catalog[index].author;
//display the output
console.log(`Checking In: \n-------------\nTitle : ${title}\nAuthor: ${author}\n`);
//change the status
this.status[index] = false;
}
}
//Main Function for Driving the Simulation
let main = ()=>{
//Create 3 books
let books = [];
books.push(new Book("Code Complete", "Steve McConnell"));
books.push(new Book("The Art of Unit Testing" ,"Roy Osherove"));
books.push(new Book("Domain Driven Design","Eric Evans"));
//Create a library object
let library = new Library(books);
//Simulate
library.simulate();
};
//Call the driver function
main();
OUTPUT