1.0.8 • Published 8 months ago

devops_assignment2 v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Bookstore Management System

The Bookstore Management System is a TypeScript-based project designed to manage a simple bookstore. It provides essential functionalities such as adding new books to the inventory, borrowing books for temporary use, and returning borrowed books to the bookstore. This project ensures the correctness of its core features through automated testing using Jest, a popular testing framework.

Features Overview

The system provides the following features:

1. Add Book

This feature allows administrators to add a new book to the bookstore's inventory. The book details, including the title, author, and availability status, are recorded.

2. Borrow Book

Customers can borrow a book from the bookstore if it is available. Once borrowed, the book is marked as unavailable in the inventory to prevent others from borrowing it until it is returned.

3. Return Book

Borrowed books can be returned to the bookstore, which updates their status in the inventory, making them available for borrowing again.

Technologies and Tools Used

The project is built using the following tools and technologies:

  • TypeScript: A strongly typed superset of JavaScript, used as the primary programming language for this project.
  • Jest: A testing framework that ensures the application’s functionality is working as intended by automating test cases.
  • Node.js: Provides the runtime environment to execute TypeScript and JavaScript code efficiently.

Project Setup Guide

To set up this project on your local machine, follow these steps:

Step 1: Clone the Repository

Start by cloning the repository from GitHub:

git clone https://github.com/your-username/bookstore-management.git
cd bookstore-management 

Step 2: Install Dependencies

Ensure you have Node.js installed. Then, install the required project dependencies using the following command:

npm install

This will install the following:

  • TypeScript
  • Jest
  • Other project dependencies

Step 3: Compile TypeScript

To compile your TypeScript code to JavaScript, run the following command:

npx tsc

Step 4: Run the Jest Tests

The core functionality of this project is tested with Jest. The following tests are implemented:

  • Add Book: Tests that a book is correctly added to the inventory.
  • Borrow Book: Tests that a book can be borrowed, and updates the inventory correctly.
  • Return Book: Tests that a borrowed book can be returned, and the inventory is updated. To run the tests, use
npm run test

This will run all Jest tests and display the results in your terminal.

Step 5: Run the Application

To run the application, use the following command:

ndoe dist/index.js

Make sure your main code is located in index.ts or modify the above path accordingly.

Project Structure

The project is organized as follows:

bookstore-management/
├── dist/               # Compiled JavaScript files
├── src/                # Source TypeScript files
│   ├── book.ts         # Defines the Book class
│   ├── bookstore.ts    # Implements the Bookstore class with add, borrow, return functionality
│   └── index.ts        # Entry point for the application
├── __tests__/          # Jest test files
│   ├── book.test.ts    # Test for the add book functionality
│   ├── borrow.test.ts  # Test for the borrow book functionality
│   └── return.test.ts  # Test for the return book functionality
├── package.json        # Project metadata and dependencies
└── tsconfig.json       # TypeScript configuration

File Descriptions

  • src/book.ts: Contains the definition of the Book class.
  • src/bookstore.ts: Contains the Bookstore class that manages the book inventory and handles add, borrow, and return operations.
  • src/index.ts: The entry point for running the application.

Test Files

-tests/book.test.ts: Contains tests for adding a book. -tests/borrow.test.ts: Contains tests for borrowing a book. -tests/return.test.ts: Contains tests for returning a book.

Example Code

Here is an example of how the core classes and tests are structured.

book.ts

export class Book {
    constructor(public title: string, public author: string, public available: boolean = true) {}
}

bookstore.ts

import { Book } from './book';

export class Bookstore {
    private inventory: Book[] = [];

    addBook(book: Book): void {
        this.inventory.push(book);
    }

    borrowBook(title: string): string {
        const book = this.inventory.find(b => b.title === title && b.available);
        if (!book) {
            return 'Book is not available';
        }
        book.available = false;
        return 'Book borrowed successfully';
    }

    returnBook(title: string): string {
        const book = this.inventory.find(b => b.title === title && !b.available);
        if (!book) {
            return 'Book was not borrowed';
        }
        book.available = true;
        return 'Book returned successfully';
    }
}

book.test.ts

import { Book } from '../src/book';
import { Bookstore } from '../src/bookstore';

test('Add a new book to the inventory', () => {
    const bookstore = new Bookstore();
    const book = new Book('The Great Gatsby', 'F. Scott Fitzgerald');
    bookstore.addBook(book);
    expect(bookstore['inventory'].length).toBe(1);
});

borrow.text.ts

import { Book } from '../src/book';
import { Bookstore } from '../src/bookstore';

test('Borrow a book from the inventory', () => {
    const bookstore = new Bookstore();
    const book = new Book('1984', 'George Orwell');
    bookstore.addBook(book);
    const result = bookstore.borrowBook('1984');
    expect(result).toBe('Book borrowed successfully');
});

return.text.ts

return.test.ts
import { Book } from '../src/book';
import { Bookstore } from '../src/bookstore';

test('Return a borrowed book to the inventory', () => {
    const bookstore = new Bookstore();
    const book = new Book('To Kill a Mockingbird', 'Harper Lee');
    bookstore.addBook(book);
    bookstore.borrowBook('To Kill a Mockingbird');
    const result = bookstore.returnBook('To Kill a Mockingbird');
    expect(result).toBe('Book returned successfully');
});

License

This project is licensed under the MIT License - see the LICENSE file for details.

This should give a clear description of your project, setup instructions, and an overview of the core functionality! Let me know if you'd like to make any changes.

1.0.8

8 months ago

1.0.6

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago