diff --git a/Car.cs b/Car.cs new file mode 100644 index 0000000..7d33c60 --- /dev/null +++ b/Car.cs @@ -0,0 +1,30 @@ +namespace CarApplication +{ + public class Car + { + public void DrivingCar() + { + Random randomNumber = new Random(); + int randomDrivingEvent = randomNumber.Next(1, 10); + + switch (randomDrivingEvent) + { + 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; + } + } + } +} diff --git a/CarExceptionHandler.cs b/CarExceptionHandler.cs new file mode 100644 index 0000000..01b15c5 --- /dev/null +++ b/CarExceptionHandler.cs @@ -0,0 +1,81 @@ +namespace CarApplication +{ + public class CarExceptionHandler + { + 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..."); + } + } +} diff --git a/CarOperationHandler.cs b/CarOperationHandler.cs new file mode 100644 index 0000000..8fc2703 --- /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) + { + CarExceptionHandler.HandleOutOfGasException(ex); + } + catch (DriverUnderInfluenceException ex) + { + CarExceptionHandler.HandleDriverUnderInfluenceException(ex); + } + catch (SpeedingException ex) + { + CarExceptionHandler.HandleSpeedingException(ex); + } + catch (MechanicalFailureException ex) + { + CarExceptionHandler.HandleMechanicalFailureException(ex); + } + catch (DriverDistractedException ex) + { + CarExceptionHandler.HandleDriverDistractedException(ex); + } + catch (CarCrashException ex) + { + CarExceptionHandler.HandleCarCrashException(ex); + } + catch (Exception ex) + { + CarExceptionHandler.HandleUnexpectedException(ex); + } + finally + { + CarExceptionHandler.SafeDriveCheck(); + } + } + } +} diff --git a/Exceptions.cs b/Exceptions.cs new file mode 100644 index 0000000..5707b8f --- /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) { } + } +}