Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Assignment-5(ExceptionHandling)/customExceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class DuplicateProductException extends Error {
constructor(message) {
super(message);
this.name = 'DuplicateProductException';
}
}

class ProductNotFoundException extends Error {
constructor(message) {
super(message);
this.name = 'ProductNotFoundException';
}
}

class InsufficientQuantityException extends Error {
constructor(message) {
super(message);
this.name = 'InsufficientQuantityException';
}
}

class InvalidInputDataException extends Error {
constructor(message) {
super(message);
this.name = 'InvalidInputDataException';
}
}

module.exports = {
DuplicateProductException,
ProductNotFoundException,
InsufficientQuantityException,
InvalidInputDataException
};
138 changes: 138 additions & 0 deletions Assignment-5(ExceptionHandling)/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const { DuplicateProductException, InvalidInputDataException, ProductNotFoundException, InsufficientQuantityException } = require('./customExceptions');
const { ProductManager } = require('./productManager');
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

const productManager = new ProductManager();

function addProduct() {
rl.question('Enter product ID: ', id => {
rl.question('Enter product name: ', name => {
rl.question('Enter product price: ', price => {
rl.question('Enter product quantity: ', quantity => {
try {
productManager.addProduct(id, name, parseFloat(price), parseInt(quantity));
console.log('Product added successfully.');
} catch (error) {
if (error instanceof DuplicateProductException) {
console.log(error.message);
} else if (error instanceof InvalidInputDataException) {
console.log('Invalid input:', error.message);
} else {
console.log('Unknown error occurred:', error.message);
}
}
mainMenu();
});
});
});
});
}

function updateProduct() {
rl.question('Enter product ID to update: ', id => {
if(!productManager.getProductById(id)) {
console.log(`Product with ID ${id} not found.`);
mainMenu();
return;
}
rl.question('Enter new name: ', newName => {
rl.question('Enter new price: ', newPrice => {
rl.question('Enter new quantity: ', newQuantity => {
try {
productManager.updateProduct(id, newName, parseFloat(newPrice), parseInt(newQuantity));
console.log('Product updated successfully.');
} catch (error) {
if (error instanceof ProductNotFoundException) {
console.log(error.message);
} else if (error instanceof InvalidInputDataException) {
console.log('Invalid input:', error.message);
} else {
console.log('Unknown error occurred:', error.message);
}
}
mainMenu();
});
});
});
});
}

function deleteProduct() {
rl.question('Enter product ID to delete: ', id => {
try {
productManager.deleteProduct(id);
console.log('Product deleted successfully.');
} catch (error) {
if (error instanceof ProductNotFoundException) {
console.log(error.message);
} else {
console.log('Unknown error occurred:', error.message);
}
}
mainMenu();
});
}

function sellProduct() {
rl.question('Enter product ID to sell: ', id => {
if(!productManager.getProductById(id)) {
console.log(`Product with ID ${id} not found.`);
mainMenu();
return;
}
rl.question('Enter quantity to sell: ', quantity => {
try {
productManager.sellProduct(id, parseInt(quantity));
console.log('Product sold successfully.');
} catch (error) {
if (error instanceof ProductNotFoundException || error instanceof InsufficientQuantityException) {
console.log(error.message);
} else {
console.log('Unknown error occurred:', error.message);
}
}
mainMenu();
});
});
}

function displayMenu() {
console.log('1. Add Product');
console.log('2. Update Product');
console.log('3. Delete Product');
console.log('4. Sell Product');
console.log('5. Exit');
}

function mainMenu() {
displayMenu();
rl.question('Enter your choice: ', choice => {
switch (choice) {
case '1':
addProduct();
break;
case '2':
updateProduct();
break;
case '3':
deleteProduct();
break;
case '4':
sellProduct();
break;
case '5':
rl.close();
break;
default:
console.log('Invalid choice. Please try again.');
mainMenu();
}
});
}

mainMenu();
10 changes: 10 additions & 0 deletions Assignment-5(ExceptionHandling)/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Product {
constructor(id, name, price, quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
}

module.exports = Product;
57 changes: 57 additions & 0 deletions Assignment-5(ExceptionHandling)/productManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { DuplicateProductException, InvalidInputDataException, ProductNotFoundException, InsufficientQuantityException } = require("./customExceptions");

class ProductManager {
constructor() {
this.products = [];
}

addProduct(id, name, price, quantity) {
if (this.getProductById(id)) {
throw new DuplicateProductException(`Product with ID ${id} already exists.`);
}
if (price < 0 || quantity < 0) {
throw new InvalidInputDataException('Price and quantity must be non-negative.');
}
const product = new Product(id, name, price, quantity);
this.products.push(product);
}

updateProduct(id, newName, newPrice, newQuantity) {
const product = this.getProductById(id);
if (!product) {
throw new ProductNotFoundException(`Product with ID ${id} not found.`);
}
if (newPrice < 0 || newQuantity < 0) {
throw new InvalidInputDataException('Price and quantity must be non-negative.');
}
product.name = newName;
product.price = newPrice;
product.quantity = newQuantity;
}

deleteProduct(id) {
const index = this.products.findIndex(product => product.id === id);
if (index === -1) {
throw new ProductNotFoundException(`Product with ID ${id} not found.`);
}
this.products.splice(index, 1);
}

getProductById(id) {
return this.products.find(product => product.id === id);
}

sellProduct(id, quantity) {
const product = this.getProductById(id);
if (!product) {
throw new ProductNotFoundException(`Product with ID ${id} not found.`);
}
if (quantity > product.quantity) {
throw new InsufficientQuantityException(`Insufficient quantity for product with ID ${id}.`);
}
product.quantity -= quantity;
}
}

module.exports = { ProductManager, DuplicateProductException, ProductNotFoundException, InsufficientQuantityException, InvalidInputDataException };

9 changes: 9 additions & 0 deletions Assignment-6/models/Employee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Employee {
public name: string;
public address: string;

constructor(name: string, address: string) {
this.name = name;
this.address = address;
}
}
11 changes: 11 additions & 0 deletions Assignment-6/services/Database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Employee } from '../models/Employee';

export interface IEmployeeRepository {
save(employee: Employee): void;
}

export class Database implements IEmployeeRepository {
public save(employee: Employee): void {
console.log(`Saving employee ${employee.name} to the database.`);
}
}
14 changes: 14 additions & 0 deletions Assignment-6/services/EmployeeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Employee } from '../models/Employee';
import { IEmployeeRepository } from './Database';

export class EmployeeService {
private employeeRepository: IEmployeeRepository;

constructor(employeeRepository: IEmployeeRepository) {
this.employeeRepository = employeeRepository;
}

public saveEmployee(employee: Employee): void {
this.employeeRepository.save(employee);
}
}