Bookstore Inventory System

Problem:

You are tasked with creating a system to manage books in a bookstore.

The system will store book information (title, author, price, and stock), and it will allow users to check the availability of books,

calculate the total cost of purcha…


This content originally appeared on DEV Community and was authored by Erasmus Kotoka

Problem:

You are tasked with creating a system to manage books in a bookstore.

The system will store book information (title, author, price, and stock), and it will allow users to check the availability of books,

calculate the total cost of purchasing books, and display books that are in stock.

that is the answer to it :

// Book constructor function

function Book(title, author, price, stock) {

this.title = title;

this.author = author;

this.price = price;

this.stock = stock;

// Method to check availability

this.isAvailable = function () {

return this.stock > 0 ? "In Stock" : "Out of Stock";

};

// Method to purchase books

this.purchaseBooks = function (quantity) {

if (this.stock >= quantity) {

  this.stock -= quantity;

  return `Total cost: $${(this.price * quantity).toFixed(2)}`;

} else {

  return "Not enough stock";

}

};

}

// Create book instances

const book1 = new Book("The Catcher in the Rye", "J.D. Salinger", 10, 5);

const book2 = new Book("To Kill a Mockingbird", "Harper Lee", 12, 2);

const book3 = new Book("1984", "George Orwell", 15, 0);

// Array of book objects

const books = [book1, book2, book3];

// Function to display available books

function displayAvailableBooks(books) {

books.forEach((book) => {

console.log(`${book.title} by ${book.author} - ${book.isAvailable()}`);

});

}

// Call the function to display available books

displayAvailableBooks(books);

// Example purchase

console.log(book1.purchaseBooks(3)); // Purchasing 3 copies of "The Catcher in the Rye"

KEEPCOding #WITHKOtoka


This content originally appeared on DEV Community and was authored by Erasmus Kotoka


Print Share Comment Cite Upload Translate Updates
APA

Erasmus Kotoka | Sciencx (2024-09-18T13:26:23+00:00) Bookstore Inventory System. Retrieved from https://www.scien.cx/2024/09/18/bookstore-inventory-system/

MLA
" » Bookstore Inventory System." Erasmus Kotoka | Sciencx - Wednesday September 18, 2024, https://www.scien.cx/2024/09/18/bookstore-inventory-system/
HARVARD
Erasmus Kotoka | Sciencx Wednesday September 18, 2024 » Bookstore Inventory System., viewed ,<https://www.scien.cx/2024/09/18/bookstore-inventory-system/>
VANCOUVER
Erasmus Kotoka | Sciencx - » Bookstore Inventory System. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/18/bookstore-inventory-system/
CHICAGO
" » Bookstore Inventory System." Erasmus Kotoka | Sciencx - Accessed . https://www.scien.cx/2024/09/18/bookstore-inventory-system/
IEEE
" » Bookstore Inventory System." Erasmus Kotoka | Sciencx [Online]. Available: https://www.scien.cx/2024/09/18/bookstore-inventory-system/. [Accessed: ]
rf:citation
» Bookstore Inventory System | Erasmus Kotoka | Sciencx | https://www.scien.cx/2024/09/18/bookstore-inventory-system/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.