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
97 changes: 97 additions & 0 deletions Assignment-5/CarDrive.ts
Original file line number Diff line number Diff line change
@@ -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.");
Comment thread
mukulpalol16 marked this conversation as resolved.
}
}

function handleBreakFailDamage() {
Comment thread
mukulpalol16 marked this conversation as resolved.
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);
}
12 changes: 12 additions & 0 deletions Assignment-5/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -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.",
};
42 changes: 42 additions & 0 deletions Assignment-5/Exceptions.ts
Original file line number Diff line number Diff line change
@@ -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
};