From 6a9d614a7ed758971a9f153a9a674b32391c05b8 Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Tue, 2 Apr 2024 18:14:51 +0530 Subject: [PATCH 1/5] Assignment-5 added --- Assignment-5/CarDrive.ts | 107 +++++++++++++++++++++++++++++++++++++ Assignment-5/Exceptions.ts | 35 ++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 Assignment-5/CarDrive.ts create mode 100644 Assignment-5/Exceptions.ts diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts new file mode 100644 index 0000000..416dc27 --- /dev/null +++ b/Assignment-5/CarDrive.ts @@ -0,0 +1,107 @@ +import { + CollisionAccidentException, + HitAndRunAccidentException, + EnvironmentalAccidentException, + CarBrakeFailException + } from './exceptions'; + + function driveCar() { + const isCarBreakFail = true; + const isCollision = false; + const isEnvironmental = false; + + if (isCarBreakFail) { + throw new CarBrakeFailException( + "Car Break Fail due to NonService of car, Have proper service" + ); + } else if (isCollision) { + throw new CollisionAccidentException( + "Collision with another vehicle occurred." + ); + } else if (isEnvironmental) { + throw new EnvironmentalAccidentException( + "Environmental conditions such as thunderstorm. Drive with caution." + ); + } else { + throw new HitAndRunAccidentException("Hit and run incident happened."); + } + } + + function handleBreakFailDamage() { + callMechanic(); + callTowTruck(); + } + + function handleCollisionAccident() { + callEmergencyServices(); + assessInjuries(); + fileInsuranceClaim(); + } + + function handleHitAndRunAccident() { + callEmergencyServices(); + inspectDamage(); + notifyAuthorities(); + } + + function handleNatureRelatedAccident() { + callEmergencyServices(); + seekShelter(); + fileInsuranceClaim(); + } + + function callMechanic() { + console.log('Mechanic called to assess and repair the car damage.'); + } + + function fileInsuranceClaim() { + console.log('Insurance claim filed for the accident.'); +} + + function callEmergencyServices() { + console.log("Emergency services are being contacted."); + } + + function callTowTruck() { + console.log("Tow truck is on the way to clear the vehicle."); + } + + function assessInjuries() { + console.log( + "Injuries are being assessed, and medical assistance is provided if needed." + ); + } + + function inspectDamage() { + console.log("Damage to the vehicle is being inspected."); + } + + function notifyAuthorities() { + console.log("Police is being notified about the hit and run incident."); + } + + function seekShelter() { + console.log("Seeking shelter due to environmental conditions."); + } + + function cleanUpAfterAccident() { + console.log("Cleanup procedures initiated."); + } + + try { + driveCar(); + } catch (e) { + if (e instanceof CarBrakeFailException) { + handleBreakFailDamage(); + } else if (e instanceof CollisionAccidentException) { + handleCollisionAccident(); + } else if (e instanceof EnvironmentalAccidentException) { + handleNatureRelatedAccident(); + } else if (e instanceof HitAndRunAccidentException) { + handleHitAndRunAccident(); + } else { + console.error("An error occurred:", e.message); + } + } finally { + cleanUpAfterAccident(); + } diff --git a/Assignment-5/Exceptions.ts b/Assignment-5/Exceptions.ts new file mode 100644 index 0000000..d354010 --- /dev/null +++ b/Assignment-5/Exceptions.ts @@ -0,0 +1,35 @@ + 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; + } + } + + export { + CollisionAccidentException, + HitAndRunAccidentException, + EnvironmentalAccidentException, + CarBrakeFailException + }; + \ No newline at end of file From f4ac96b1b05d5a299069c7456186c421f95b3d54 Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Tue, 2 Apr 2024 18:43:07 +0530 Subject: [PATCH 2/5] Assignment-5 added --- Assignment-5/CarDrive.ts | 76 +++++++++++++++++++++++++------------- Assignment-5/Exceptions.ts | 11 +++++- 2 files changed, 59 insertions(+), 28 deletions(-) diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts index 416dc27..9e5c9db 100644 --- a/Assignment-5/CarDrive.ts +++ b/Assignment-5/CarDrive.ts @@ -1,14 +1,41 @@ 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 { + cleanUpAfterAccident(); + } + function driveCar() { const isCarBreakFail = true; const isCollision = false; const isEnvironmental = false; + const isAbrupltyStop = false; if (isCarBreakFail) { throw new CarBrakeFailException( @@ -18,6 +45,10 @@ import { throw new CollisionAccidentException( "Collision with another vehicle occurred." ); + } else if (isAbrupltyStop) { + throw new CarAbrupltyStopException ( + "Automatically stop and not starting." + ); } else if (isEnvironmental) { throw new EnvironmentalAccidentException( "Environmental conditions such as thunderstorm. Drive with caution." @@ -32,20 +63,26 @@ import { callTowTruck(); } + function AbrupltyException() { + callMechanic(); + callTowTruck(); + seekShelter(); + } + function handleCollisionAccident() { - callEmergencyServices(); + callAmbulance(); assessInjuries(); fileInsuranceClaim(); } function handleHitAndRunAccident() { - callEmergencyServices(); + callAmbulance(); inspectDamage(); - notifyAuthorities(); + callPolice(); } function handleNatureRelatedAccident() { - callEmergencyServices(); + callAmbulance(); seekShelter(); fileInsuranceClaim(); } @@ -58,8 +95,12 @@ import { console.log('Insurance claim filed for the accident.'); } - function callEmergencyServices() { - console.log("Emergency services are being contacted."); + function isAbrupltyStop() { + console.log('Abruplty Stop the car and check the car'); +} + + function callAmbulance() { + console.log("Ambulance are being contacted."); } function callTowTruck() { @@ -68,7 +109,7 @@ import { function assessInjuries() { console.log( - "Injuries are being assessed, and medical assistance is provided if needed." + "Assessing injuries and providing necessary medical assistance." ); } @@ -76,7 +117,8 @@ import { console.log("Damage to the vehicle is being inspected."); } - function notifyAuthorities() { + + function callPolice() { console.log("Police is being notified about the hit and run incident."); } @@ -87,21 +129,3 @@ import { function cleanUpAfterAccident() { console.log("Cleanup procedures initiated."); } - - try { - driveCar(); - } catch (e) { - if (e instanceof CarBrakeFailException) { - handleBreakFailDamage(); - } else if (e instanceof CollisionAccidentException) { - handleCollisionAccident(); - } else if (e instanceof EnvironmentalAccidentException) { - handleNatureRelatedAccident(); - } else if (e instanceof HitAndRunAccidentException) { - handleHitAndRunAccident(); - } else { - console.error("An error occurred:", e.message); - } - } finally { - cleanUpAfterAccident(); - } diff --git a/Assignment-5/Exceptions.ts b/Assignment-5/Exceptions.ts index d354010..074cdbf 100644 --- a/Assignment-5/Exceptions.ts +++ b/Assignment-5/Exceptions.ts @@ -25,11 +25,18 @@ this.name = this.constructor.name; } } + + class CarAbrupltyStopException extends Error { + constructor(message: string) { + super(message); + this.name = this.constructor.name; + } + } export { CollisionAccidentException, HitAndRunAccidentException, EnvironmentalAccidentException, - CarBrakeFailException + CarBrakeFailException, + CarAbrupltyStopException }; - \ No newline at end of file From f89738fb2ba48e4643e289f0ea5a5e6900b88db1 Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Wed, 3 Apr 2024 10:39:18 +0530 Subject: [PATCH 3/5] converted function to Enums and also did formatting --- Assignment-5/CarDrive.ts | 85 +++++++++++-------------------------- Assignment-5/ErrorHanler.ts | 12 ++++++ 2 files changed, 36 insertions(+), 61 deletions(-) create mode 100644 Assignment-5/ErrorHanler.ts diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts index 9e5c9db..07fe5ed 100644 --- a/Assignment-5/CarDrive.ts +++ b/Assignment-5/CarDrive.ts @@ -1,3 +1,4 @@ +import { Actions } from './ErrorHanler'; import { CarAbrupltyStopException, CollisionAccidentException, @@ -21,14 +22,14 @@ import { } else if (e instanceof HitAndRunAccidentException) { console.log("Hit and Run Accident Exception occurred."); handleHitAndRunAccident(); - }else if (e instanceof CarAbrupltyStopException){ + } else if (e instanceof CarAbrupltyStopException){ console.log("Car Abruplty Stop Exception occurred."); - AbrupltyException(); + abrupltyException(); } else { console.error("An error occurred:", e.message); } } finally { - cleanUpAfterAccident(); + checkAndReset(); } function driveCar() { @@ -36,7 +37,7 @@ import { const isCollision = false; const isEnvironmental = false; const isAbrupltyStop = false; - + if (isCarBreakFail) { throw new CarBrakeFailException( "Car Break Fail due to NonService of car, Have proper service" @@ -46,7 +47,7 @@ import { "Collision with another vehicle occurred." ); } else if (isAbrupltyStop) { - throw new CarAbrupltyStopException ( + throw new CarAbrupltyStopException( "Automatically stop and not starting." ); } else if (isEnvironmental) { @@ -59,73 +60,35 @@ import { } function handleBreakFailDamage() { - callMechanic(); - callTowTruck(); + console.log(Actions.CALL_MECHANIC); + console.log(Actions.CALL_TOW_TRUCK); } - function AbrupltyException() { - callMechanic(); - callTowTruck(); - seekShelter(); + 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() { - callAmbulance(); - assessInjuries(); - fileInsuranceClaim(); + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.ASSESS_INJURIES); + console.log(Actions.FILE_INSURANCE_CLAIM); } function handleHitAndRunAccident() { - callAmbulance(); - inspectDamage(); - callPolice(); + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.INSPECT_DAMAGE); + console.log(Actions.CALL_POLICE); } function handleNatureRelatedAccident() { - callAmbulance(); - seekShelter(); - fileInsuranceClaim(); + console.log(Actions.SEEK_SHELTER); + console.log(Actions.CALL_AMBULANCE); + console.log(Actions.FILE_INSURANCE_CLAIM); } - - function callMechanic() { - console.log('Mechanic called to assess and repair the car damage.'); - } - - function fileInsuranceClaim() { - console.log('Insurance claim filed for the accident.'); -} - function isAbrupltyStop() { - console.log('Abruplty Stop the car and check the car'); -} - - function callAmbulance() { - console.log("Ambulance are being contacted."); - } - - function callTowTruck() { - console.log("Tow truck is on the way to clear the vehicle."); - } - - function assessInjuries() { - console.log( - "Assessing injuries and providing necessary medical assistance." - ); - } - - function inspectDamage() { - console.log("Damage to the vehicle is being inspected."); - } - - - function callPolice() { - console.log("Police is being notified about the hit and run incident."); - } - - function seekShelter() { - console.log("Seeking shelter due to environmental conditions."); - } - - function cleanUpAfterAccident() { - console.log("Cleanup procedures initiated."); + function checkAndReset() { + console.log(Actions.CHECK_AND_RESET); } diff --git a/Assignment-5/ErrorHanler.ts b/Assignment-5/ErrorHanler.ts new file mode 100644 index 0000000..fec211f --- /dev/null +++ b/Assignment-5/ErrorHanler.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.", +}; From 97a8da7895e37658f2f82ad314457fa6e9ad3c69 Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Wed, 3 Apr 2024 11:48:16 +0530 Subject: [PATCH 4/5] converted constants to enums --- Assignment-5/CarDrive.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts index 07fe5ed..ee3a2f6 100644 --- a/Assignment-5/CarDrive.ts +++ b/Assignment-5/CarDrive.ts @@ -1,4 +1,12 @@ import { Actions } from './ErrorHanler'; + +const StatusEnum = { + isCarBreakFail: true, + isCollision: false, + isEnvironmental: false, + isAbruptlyStop: false +}; + import { CarAbrupltyStopException, CollisionAccidentException, @@ -33,24 +41,19 @@ import { } function driveCar() { - const isCarBreakFail = true; - const isCollision = false; - const isEnvironmental = false; - const isAbrupltyStop = false; - - if (isCarBreakFail) { + if (StatusEnum.isCarBreakFail) { throw new CarBrakeFailException( "Car Break Fail due to NonService of car, Have proper service" ); - } else if (isCollision) { + } else if (StatusEnum.isCollision) { throw new CollisionAccidentException( "Collision with another vehicle occurred." ); - } else if (isAbrupltyStop) { + } else if (StatusEnum.isAbruptlyStop) { throw new CarAbrupltyStopException( "Automatically stop and not starting." ); - } else if (isEnvironmental) { + } else if (StatusEnum.isEnvironmental) { throw new EnvironmentalAccidentException( "Environmental conditions such as thunderstorm. Drive with caution." ); From 3b51871adea72f6847fd68529ca0b6d5ee28cbc0 Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Wed, 3 Apr 2024 12:13:37 +0530 Subject: [PATCH 5/5] file name changed --- Assignment-5/CarDrive.ts | 2 +- Assignment-5/{ErrorHanler.ts => ErrorHandler.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Assignment-5/{ErrorHanler.ts => ErrorHandler.ts} (100%) diff --git a/Assignment-5/CarDrive.ts b/Assignment-5/CarDrive.ts index ee3a2f6..6777067 100644 --- a/Assignment-5/CarDrive.ts +++ b/Assignment-5/CarDrive.ts @@ -1,4 +1,4 @@ -import { Actions } from './ErrorHanler'; +import { Actions } from './ErrorHandler'; const StatusEnum = { isCarBreakFail: true, diff --git a/Assignment-5/ErrorHanler.ts b/Assignment-5/ErrorHandler.ts similarity index 100% rename from Assignment-5/ErrorHanler.ts rename to Assignment-5/ErrorHandler.ts