Question

In: Computer Science

This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...

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.

Solutions

Expert Solution

// 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


Related Solutions

JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As a reminder, you can...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT