-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 5 (Error Handling in Car Accident scenario) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mukulpalol16
wants to merge
8
commits into
main
Choose a base branch
from
Assignment5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e48722e
created ProblemStatement.md
mukulpalol16 02bfb93
added code files
mukulpalol16 19c9623
updated ProblemStatement.md
mukulpalol16 5638bcb
updated code files
mukulpalol16 229a4f4
updated README.md
mukulpalol16 86e5175
addressed review comments
mukulpalol16 99e843a
addressed review comments
mukulpalol16 1f7c65d
addressed review comments
mukulpalol16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| namespace CarAccidentErrorHandling | ||
| { | ||
| public class Car | ||
| { | ||
| public void Start() | ||
| { | ||
| Console.WriteLine("Car started."); | ||
| } | ||
|
|
||
| public void Run() | ||
| { | ||
| Console.WriteLine("Car is running."); | ||
| DrivingScenario scenario = (DrivingScenario)new Random().Next(1, 6); // Simulate random car failure/accident scenarios | ||
| switch (scenario) | ||
| { | ||
| case DrivingScenario.Collision: | ||
| throw new CollisionException("Car collided with another vehicle!"); | ||
| case DrivingScenario.RoadObstacle: | ||
| throw new RoadObstacleException("Car encountered a road obstacle!"); | ||
| case DrivingScenario.PedestrianCollision: | ||
| throw new PedestrianCollisionException("Car hit a pedestrian!"); | ||
| case DrivingScenario.MechanicalFailure: | ||
| throw new MechanicalFailureException("Car experienced a mechanical failure!"); | ||
| case DrivingScenario.Safe: | ||
| Console.WriteLine("Car reached its destination safely."); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public void Stop() | ||
| { | ||
| Console.WriteLine("Car stopped."); | ||
| } | ||
|
|
||
| public void Brake() | ||
| { | ||
| Console.WriteLine("Braking to avoid accident."); | ||
| } | ||
|
|
||
| public void Swerve() | ||
| { | ||
| Console.WriteLine("Swerving to avoid obstacle."); | ||
| } | ||
|
|
||
| public void ResetCarState() | ||
| { | ||
| Console.WriteLine("Reseting car's state like turn off lights, any indicator or alarms, set gear to neutral, etc.") | ||
| } | ||
|
|
||
| public enum DrivingScenario | ||
| { | ||
| Collision = 1, | ||
| RoadObstacle = 2, | ||
| PedestrianCollision = 3, | ||
| MechanicalFailure = 4, | ||
| Safe = 5 | ||
| } | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
CarAccidentErrorHandling/CarAccidentErrorHandling/CarOperator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| namespace CarAccidentErrorHandling | ||
| { | ||
| internal class CarOperator | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| Car car = new Car(); | ||
| try | ||
| { | ||
| car.Start(); | ||
| car.Run(); | ||
| } | ||
| catch (CollisionException collisionException) | ||
| { | ||
| Console.WriteLine("Car Accident: " + collisionException.Message); | ||
| car.Brake(); | ||
| } | ||
| catch (RoadObstacleException roadObstaclException) | ||
| { | ||
| Console.WriteLine("Road Obstacle: " + roadObstaclException.Message); | ||
| car.Swerve(); | ||
| car.Brake(); | ||
| } | ||
| catch (PedestrianCollisionException pedestrianException) | ||
| { | ||
| Console.WriteLine("Pedestrian Accident: " + pedestrianException.Message); | ||
| car.Brake(); | ||
| } | ||
| catch (MechanicalFailureException mechanicalFailureException) | ||
| { | ||
| Console.WriteLine("Mechanical Failure: " + mechanicalFailureException.Message); | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| Console.WriteLine("An unexpected error occurred: " + exception.Message); | ||
| } | ||
| finally | ||
| { | ||
| Console.WriteLine("Attempting to bring the car back to a normal state"); | ||
| car.Stop(); | ||
| car.ResetCarState(); | ||
| } | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
CarAccidentErrorHandling/CarAccidentErrorHandling/Exceptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace CarAccidentErrorHandling | ||
| { | ||
| public class CollisionException : Exception | ||
| { | ||
| public CollisionException(string message) : base(message) { } | ||
| } | ||
|
|
||
| public class RoadObstacleException : Exception | ||
| { | ||
| public RoadObstacleException(string message) : base(message) { } | ||
| } | ||
|
|
||
| public class PedestrianCollisionException : Exception | ||
| { | ||
| public PedestrianCollisionException(string message) : base(message) { } | ||
| } | ||
|
|
||
| public class MechanicalFailureException : Exception | ||
| { | ||
| public MechanicalFailureException(string message) : base(message) { } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Assignment - Error Handling in Car Accident Scenario | ||
|
|
||
|  | ||
|
|
||
| - You have to think about given image and come with scenarios. | ||
| - What are the try and catches are occurring into that image. You have to find different kind of exceptions. You have to write them in code with proper sequence. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be done without switch case as it breaks OCP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used switch case just so I can simulate different accident scenarios randomly. In actual case, we would have a method inside the Run method which will throw any of the exceptions that we'll get.
Let me know what you think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it is ok else everything looks good to me.