tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"lib": ["es2015", "dom"],
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./",
"declaration": true
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules"
]
}
// bookstore.ts : One File into which goes all of my Code
class Book {
constructor(
public id: string,
public title: string,
public author: string,
public price: number,
public stock: number
) {}
displayInfo(): void {
console.log(`${this.title} by ${this.author} - $${this.price} (${this.stock} in stock)`);
}
}
class Customer {
constructor(
public id: string,
public name: string,
public email: string
) {}
displayInfo(): void {
console.log(`Customer: ${this.name} (${this.email})`);
}
}
class Order {
private _books: { book: Book; quantity: number }[] = [];
private _totalPrice: number = 0;
constructor(
public id: string,
public customer: Customer
) {}
addBook(book: Book, quantity: number): void {
if (book.stock >= quantity) {
let existingBookIndex = -1;
for (let i = 0; i < this._books.length; i++) {
if (this._books[i].book.id === book.id) {
existingBookIndex = i;
break;
}
}
if (existingBookIndex !== -1) {
this._books[existingBookIndex].quantity += quantity;
} else {
this._books.push({ book, quantity });
}
this._totalPrice += book.price * quantity;
book.stock -= quantity;
} else {
throw new Error("Not enough stock");
}
}
displayInfo(): void {
console.log(`Order ${this.id} for:`);
this.customer.displayInfo();
console.log("Books ordered:");
this._books.forEach(({ book, quantity }) => {
console.log(` ${quantity}x ${book.title}`);
});
console.log(`Total Price: $${this._totalPrice.toFixed(2)}`);
}
}
class Bookstore {
private _inventory: Book[] = [];
private _customers: Customer[] = [];
private _orders: Order[] = [];
constructor(public name: string) {}
addBook(book: Book): void {
this._inventory.push(book);
}
addCustomer(customer: Customer): void {
this._customers.push(customer);
}
createOrder(customer: Customer): Order {
const order = new Order(`ORD-${this._orders.length + 1}`, customer);
this._orders.push(order);
return order;
}
displayInfo(): void {
console.log(`Welcome to ${this.name}!`);
console.log(`We have ${this._inventory.length} books in stock.`);
console.log(`We have served ${this._customers.length} customers.`);
console.log(`We have processed ${this._orders.length} orders.`);
}
}
// Main application logic
const myBookstore = new Bookstore("TypeScript Bookstore");
const book1 = new Book("B001", "TypeScript in Action", "John Doe", 29.99, 50);
const book2 = new Book("B002", "Node.js Essentials", "Jane Smith", 24.99, 30);
myBookstore.addBook(book1);
myBookstore.addBook(book2);
const customer1 = new Customer("C001", "Alice Johnson", "alice@example.com");
const customer2 = new Customer("C002", "Bob Williams", "bob@example.com");
myBookstore.addCustomer(customer1);
myBookstore.addCustomer(customer2);
const order1 = myBookstore.createOrder(customer1);
order1.addBook(book1, 2);
order1.addBook(book2, 1);
const order2 = myBookstore.createOrder(customer2);
order2.addBook(book2, 3);
console.log("Bookstore Information:");
myBookstore.displayInfo();
console.log("\nOrder Information:");
order1.displayInfo();
console.log();
order2.displayInfo();
console.log("\nUpdated Book Stock:");
book1.displayInfo();
book2.displayInfo();