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
30 changes: 30 additions & 0 deletions Car.cs
Comment thread
Poyam-ITT marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
81 changes: 81 additions & 0 deletions CarExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -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...");
}
}
}
46 changes: 46 additions & 0 deletions CarOperationHandler.cs
Comment thread
Poyam-ITT marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
32 changes: 32 additions & 0 deletions Exceptions.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
}