From ae058e44b52e4c618aceccffc4fb65e2fc4d0625 Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 2 Apr 2024 15:11:15 +0530 Subject: [PATCH 1/4] Added solution for Car Crash problem --- Car.cs | 30 ++++++++++++++++ CarOperationHandler.cs | 46 ++++++++++++++++++++++++ ExceptionHandler.cs | 81 ++++++++++++++++++++++++++++++++++++++++++ Exceptions.cs | 32 +++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 Car.cs create mode 100644 CarOperationHandler.cs create mode 100644 ExceptionHandler.cs create mode 100644 Exceptions.cs diff --git a/Car.cs b/Car.cs new file mode 100644 index 0000000..b2a6840 --- /dev/null +++ b/Car.cs @@ -0,0 +1,30 @@ +namespace CarApplication +{ + public class Car + { + public static void DrivingCar() + { + Random randomNumber = new Random(); + int chanceOfUncertainity = randomNumber.Next(1, 10); + + switch (chanceOfUncertainity) + { + case 1: + throw new OutOfGasException("Car ran out of gas!"); + case 2: + throw new DriverUnderInfluenceException("Driver is intoxicated!"); + case 3: + throw new SpeedingException("Speed Alert!"); + case 4: + throw new MechanicalFailureException("Mechanical Issues Detected"); + case 5: + throw new DriverDistractedException("Driver Distracted Observation!"); + case 6: + throw new CarCrashException("Car crash occurred due to uncertain reasons!"); + default: + Console.WriteLine("No car crash occurred."); + break; + } + } + } +} \ No newline at end of file diff --git a/CarOperationHandler.cs b/CarOperationHandler.cs new file mode 100644 index 0000000..fde852c --- /dev/null +++ b/CarOperationHandler.cs @@ -0,0 +1,46 @@ +namespace CarApplication +{ + class CarOperationHandler + { + public static void Main() + { + try + { + var car = new Car(); + Car.DrivingCar(); + } + catch (OutOfGasException ex) + { + ExceptionHandler.HandleOutOfGasException(ex); + } + catch (DriverUnderInfluenceException ex) + { + ExceptionHandler.HandleDriverUnderInfluenceException(ex); + } + catch (SpeedingException ex) + { + ExceptionHandler.HandleSpeedingException(ex); + } + catch (MechanicalFailureException ex) + { + ExceptionHandler.HandleMechanicalFailureException(ex); + } + catch (DriverDistractedException ex) + { + ExceptionHandler.HandleDriverDistractedException(ex); + } + catch (CarCrashException ex) + { + ExceptionHandler.HandleCarCrashException(ex); + } + catch (Exception ex) + { + ExceptionHandler.HandleUnexpectedException(ex); + } + finally + { + ExceptionHandler.SafeDriveCheck(); + } + } + } +} \ No newline at end of file diff --git a/ExceptionHandler.cs b/ExceptionHandler.cs new file mode 100644 index 0000000..3c604d4 --- /dev/null +++ b/ExceptionHandler.cs @@ -0,0 +1,81 @@ +namespace CarApplication +{ + public class ExceptionHandler + { + public static void HandleOutOfGasException(OutOfGasException ex) + { + Console.WriteLine($"Car running out of gas: {ex.Message}"); + RefuelCar(); + } + + public static void HandleDriverUnderInfluenceException(DriverUnderInfluenceException ex) + { + Console.WriteLine($"Alcohol detection system detected some illegal behaviours : {ex.Message}"); + DrinkAndDriveWarning(); + } + + public static void HandleSpeedingException(SpeedingException ex) + { + Console.WriteLine($"Speed limit Detection detected High Speed: {ex.Message}"); + HighSpeedAlert(); + } + + public static void HandleMechanicalFailureException(MechanicalFailureException ex) + { + Console.WriteLine($"Mechanical Issues found in the Car, contact mechanic: {ex.Message}"); + ContactNearestMechanic(); + } + + public static void HandleDriverDistractedException(DriverDistractedException ex) + { + Console.WriteLine($"Sensors and cameras detected driver to be distracted, please focus: {ex.Message}"); + DriverBehaviourAlert(); + } + + public static void HandleCarCrashException(CarCrashException ex) + { + Console.WriteLine($"Car crash occurred: {ex.Message}"); + CallEmergencyServices(); + } + + public static void HandleUnexpectedException(Exception ex) + { + Console.WriteLine($"An unexpected error occurred: {ex.Message}"); + } + + public static void SafeDriveCheck() + { + Console.WriteLine("Your car stopped, Did you reached safely?"); + } + + private static void RefuelCar() + { + Console.WriteLine("Refueling the car..."); + } + + private static void DrinkAndDriveWarning() + { + Console.WriteLine("DON'T DRINK AND DRIVE..."); + } + + private static void HighSpeedAlert() + { + Console.WriteLine("SLOWER DOWN THE SPEED..."); + } + + private static void ContactNearestMechanic() + { + Console.WriteLine("Searching for near by mechanic services..."); + } + + private static void DriverBehaviourAlert() + { + Console.WriteLine("STOP GETTING DISTRACTED..."); + } + + private static void CallEmergencyServices() + { + Console.WriteLine("Calling emergency services..."); + } + } +} \ No newline at end of file diff --git a/Exceptions.cs b/Exceptions.cs new file mode 100644 index 0000000..0fbd28e --- /dev/null +++ b/Exceptions.cs @@ -0,0 +1,32 @@ +namespace CarApplication +{ + public class OutOfGasException : Exception + { + public OutOfGasException(string message) : base(message) { } + } + + public class DriverUnderInfluenceException : Exception + { + public DriverUnderInfluenceException(string message) : base(message) { } + } + + public class SpeedingException : Exception + { + public SpeedingException(string message) : base(message) { } + } + + public class MechanicalFailureException : Exception + { + public MechanicalFailureException(string message) : base(message) { } + } + + public class DriverDistractedException : Exception + { + public DriverDistractedException(string message) : base(message) { } + } + + public class CarCrashException : Exception + { + public CarCrashException(string message) : base(message) { } + } +} \ No newline at end of file From 6c9663dc26c10fc1ab19dee27a391222e5e917de Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 2 Apr 2024 17:36:35 +0530 Subject: [PATCH 2/4] Updated DrivingCar method --- Car.cs | 6 +++--- CarOperationHandler.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Car.cs b/Car.cs index b2a6840..5b1d3f5 100644 --- a/Car.cs +++ b/Car.cs @@ -2,12 +2,12 @@ namespace CarApplication { public class Car { - public static void DrivingCar() + public void DrivingCar() { Random randomNumber = new Random(); - int chanceOfUncertainity = randomNumber.Next(1, 10); + int randomDrivingEvent = randomNumber.Next(1, 10); - switch (chanceOfUncertainity) + switch (randomDrivingEvent) { case 1: throw new OutOfGasException("Car ran out of gas!"); diff --git a/CarOperationHandler.cs b/CarOperationHandler.cs index fde852c..ffb1bcf 100644 --- a/CarOperationHandler.cs +++ b/CarOperationHandler.cs @@ -7,7 +7,7 @@ public static void Main() try { var car = new Car(); - Car.DrivingCar(); + car.DrivingCar(); } catch (OutOfGasException ex) { From e53500b76324290922fa8a1fcd515b7c9261f5e3 Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Wed, 3 Apr 2024 13:04:44 +0530 Subject: [PATCH 3/4] Added extra line --- Car.cs | 2 +- CarOperationHandler.cs | 2 +- ExceptionHandler.cs | 2 +- Exceptions.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Car.cs b/Car.cs index 5b1d3f5..7d33c60 100644 --- a/Car.cs +++ b/Car.cs @@ -27,4 +27,4 @@ public void DrivingCar() } } } -} \ No newline at end of file +} diff --git a/CarOperationHandler.cs b/CarOperationHandler.cs index ffb1bcf..585acf6 100644 --- a/CarOperationHandler.cs +++ b/CarOperationHandler.cs @@ -43,4 +43,4 @@ public static void Main() } } } -} \ No newline at end of file +} diff --git a/ExceptionHandler.cs b/ExceptionHandler.cs index 3c604d4..bb93d58 100644 --- a/ExceptionHandler.cs +++ b/ExceptionHandler.cs @@ -78,4 +78,4 @@ private static void CallEmergencyServices() Console.WriteLine("Calling emergency services..."); } } -} \ No newline at end of file +} diff --git a/Exceptions.cs b/Exceptions.cs index 0fbd28e..5707b8f 100644 --- a/Exceptions.cs +++ b/Exceptions.cs @@ -29,4 +29,4 @@ public class CarCrashException : Exception { public CarCrashException(string message) : base(message) { } } -} \ No newline at end of file +} From 125177ab4014e98358b720a11e18bd687f4f7cdf Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Wed, 3 Apr 2024 15:04:01 +0530 Subject: [PATCH 4/4] Renamed ExceptionHandler class --- ExceptionHandler.cs => CarExceptionHandler.cs | 2 +- CarOperationHandler.cs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename ExceptionHandler.cs => CarExceptionHandler.cs (98%) diff --git a/ExceptionHandler.cs b/CarExceptionHandler.cs similarity index 98% rename from ExceptionHandler.cs rename to CarExceptionHandler.cs index bb93d58..01b15c5 100644 --- a/ExceptionHandler.cs +++ b/CarExceptionHandler.cs @@ -1,6 +1,6 @@ namespace CarApplication { - public class ExceptionHandler + public class CarExceptionHandler { public static void HandleOutOfGasException(OutOfGasException ex) { diff --git a/CarOperationHandler.cs b/CarOperationHandler.cs index 585acf6..8fc2703 100644 --- a/CarOperationHandler.cs +++ b/CarOperationHandler.cs @@ -11,35 +11,35 @@ public static void Main() } catch (OutOfGasException ex) { - ExceptionHandler.HandleOutOfGasException(ex); + CarExceptionHandler.HandleOutOfGasException(ex); } catch (DriverUnderInfluenceException ex) { - ExceptionHandler.HandleDriverUnderInfluenceException(ex); + CarExceptionHandler.HandleDriverUnderInfluenceException(ex); } catch (SpeedingException ex) { - ExceptionHandler.HandleSpeedingException(ex); + CarExceptionHandler.HandleSpeedingException(ex); } catch (MechanicalFailureException ex) { - ExceptionHandler.HandleMechanicalFailureException(ex); + CarExceptionHandler.HandleMechanicalFailureException(ex); } catch (DriverDistractedException ex) { - ExceptionHandler.HandleDriverDistractedException(ex); + CarExceptionHandler.HandleDriverDistractedException(ex); } catch (CarCrashException ex) { - ExceptionHandler.HandleCarCrashException(ex); + CarExceptionHandler.HandleCarCrashException(ex); } catch (Exception ex) { - ExceptionHandler.HandleUnexpectedException(ex); + CarExceptionHandler.HandleUnexpectedException(ex); } finally { - ExceptionHandler.SafeDriveCheck(); + CarExceptionHandler.SafeDriveCheck(); } } }