ts_book_store
v1.0.0
Published
Here’s a README file for your bookstore project written in TypeScript, which includes Jest tests for adding, borrowing, and returning books. You can customize this as per your specific needs.
Downloads
65
Readme
Here’s a README file for your bookstore project written in TypeScript, which includes Jest tests for adding, borrowing, and returning books. You can customize this as per your specific needs.
Bookstore Management System
This is a TypeScript-based project that implements a simple bookstore management system. It provides functionality for adding books, borrowing books, and returning books. The program is tested using Jest, ensuring the correctness of the core features.
Features
- Add Book: Adds a new book to the bookstore's inventory.
- Borrow Book: Allows a user to borrow a book from the bookstore.
- Return Book: Allows a user to return a borrowed book to the bookstore.
Technologies Used
- TypeScript: The main language for the project.
- Jest: Used for testing the application’s core features.
- Node.js: Runtime environment to execute the TypeScript code.
Project Setup
1. Clone the repository
git clone https://github.com/your-username/bookstore-management.git
cd bookstore-management
2. Install dependencies
Ensure you have Node.js installed on your machine. Then install the necessary dependencies:
npm install
This will install the following:
- TypeScript
- Jest
- Other project dependencies
3. Compile TypeScript
To compile your TypeScript code to JavaScript, run the following command:
npx tsc
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.
5. Running the Project
After compiling the TypeScript files, run the project by executing the JavaScript files with Node.js:
node 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.test.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.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.