diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts new file mode 100644 index 0000000..6777067 --- /dev/null +++ b/Assignment-5/CarDrive.ts @@ -0,0 +1,97 @@ +import { Actions } from './ErrorHandler'; + +const StatusEnum = { + isCarBreakFail: true, + isCollision: false, + isEnvironmental: false, + isAbruptlyStop: false +}; + +import { + CarAbrupltyStopException, + CollisionAccidentException, + HitAndRunAccidentException, + EnvironmentalAccidentException, + CarBrakeFailException + } from './exceptions'; + + try { + driveCar(); + } catch (e) { + if (e instanceof CarBrakeFailException) { + console.log("Car Brake Fail Exception occurred."); + handleBreakFailDamage(); + } else if (e instanceof CollisionAccidentException) { + console.log("Collision Accident Exception occurred."); + handleCollisionAccident(); + } else if (e instanceof EnvironmentalAccidentException) { + console.log("Environmental Accident Exception occurred."); + handleNatureRelatedAccident(); + } else if (e instanceof HitAndRunAccidentException) { + console.log("Hit and Run Accident Exception occurred."); + handleHitAndRunAccident(); + } else if (e instanceof CarAbrupltyStopException){ + console.log("Car Abruplty Stop Exception occurred."); + abrupltyException(); + } else { + console.error("An error occurred:", e.message); + } + } finally { + checkAndReset(); + } + + function driveCar() { + if (StatusEnum.isCarBreakFail) { + throw new CarBrakeFailException( + "Car Break Fail due to NonService of car, Have proper service" + ); + } else if (StatusEnum.isCollision) { + throw new CollisionAccidentException( + "Collision with another vehicle occurred." + ); + } else if (StatusEnum.isAbruptlyStop) { + throw new CarAbrupltyStopException( + "Automatically stop and not starting." + ); + } else if (StatusEnum.isEnvironmental) { + throw new EnvironmentalAccidentException( + "Environmental conditions such as thunderstorm. Drive with caution." + ); + } else { + throw new HitAndRunAccidentException("Hit and run incident happened."); + } + } + + function handleBreakFailDamage() { + console.log(Actions.CALL_MECHANIC); + console.log(Actions.CALL_TOW_TRUCK); + } + + function abrupltyException() { + console.log(Actions.CHECK_ALL_BRAKE); + console.log(Actions.CALL_MECHANIC); + console.log(Actions.CALL_TOW_TRUCK); + console.log(Actions.SEEK_SHELTER); + } + + function handleCollisionAccident() { + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.ASSESS_INJURIES); + console.log(Actions.FILE_INSURANCE_CLAIM); + } + + function handleHitAndRunAccident() { + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.INSPECT_DAMAGE); + console.log(Actions.CALL_POLICE); + } + + function handleNatureRelatedAccident() { + console.log(Actions.SEEK_SHELTER); + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.FILE_INSURANCE_CLAIM); + } + + function checkAndReset() { + console.log(Actions.CHECK_AND_RESET); + } diff --git a/Assignment-5/ErrorHandler.ts b/Assignment-5/ErrorHandler.ts new file mode 100644 index 0000000..fec211f --- /dev/null +++ b/Assignment-5/ErrorHandler.ts @@ -0,0 +1,12 @@ +export const Actions = { + CALL_MECHANIC: "Mechanic called to assess and repair the car damage.", + FILE_INSURANCE_CLAIM: "Insurance claim filed for the accident.", + CHECK_ALL_BRAKE: "Check all the brake.", + CALL_AMBULANCE: "Ambulance are being contacted.", + CALL_TOW_TRUCK: "Tow truck is on the way to clear the vehicle.", + ASSESS_INJURIES: "Assessing injuries and providing necessary medical assistance.", + INSPECT_DAMAGE: "Damage to the vehicle is being inspected.", + CALL_POLICE: "Police is being notified about the hit and run incident.", + SEEK_SHELTER: "Seeking shelter due to environmental conditions.", + CHECK_AND_RESET: "check everthing in the cars and reset it if requirred.", +}; diff --git a/Assignment-5/Exceptions.ts b/Assignment-5/Exceptions.ts new file mode 100644 index 0000000..074cdbf --- /dev/null +++ b/Assignment-5/Exceptions.ts @@ -0,0 +1,42 @@ + class CarBrakeFailException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } + + class CollisionAccidentException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } + + class HitAndRunAccidentException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } + + class EnvironmentalAccidentException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } + + class CarAbrupltyStopException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } + + export { + CollisionAccidentException, + HitAndRunAccidentException, + EnvironmentalAccidentException, + CarBrakeFailException, + CarAbrupltyStopException + };