// calculator.ts
export enum OperationType {
SUM = 'sum',
SUBTRACT = 'subtract',
MULTIPLY = 'multiply',
DIVIDE = 'divide'
}
export class CalculationError extends Error {
constructor(message: string) {
super(message);
this.name = 'CalculationError';
}
}
export interface Operation {
execute(a: number, b: number): number;
}
export class Calculator {
private operations: Map;
constructor() {
this.operations = new Map();
}
registerOperation(type: OperationType, operation: Operation): void {
this.operations.set(type, operation);
}
calculate(a: number, b: number, type: OperationType): number {
const operation = this.operations.get(type);
if (!operation) {
throw new CalculationError(`Operation ${type} not supported`);
}
return operation.execute(a, b);
}
}