patterncsharpMinor
Elevator Interview Problem OOP
Viewed 0 times
problemelevatorinterviewoop
Problem
Can someone please critique my Elevator problem - I wanted to use OOP principles and coding standards.
Also, does logic make sense?
So I imagine that if the elevator is moving up and the request is floor below it will need to finish all up/above requests etc.
```
internal class Program
{
private const string _quit = "q";
private static void Main(string[] args)
{
var manager = new Manager();
var input = string.Empty;
while (input != _quit) {
Console.Write("Enter floor: ");
input = Console.ReadLine();
int floor;
if (int.TryParse(input, out floor))
manager.ButtonPressed(floor);
else if (input == _quit)
Console.WriteLine("GoodBye!");
else
Console.WriteLine("You have pressed an incorrect floor, Please try again");
}
}
}
internal enum Status
{
GoingUp,
GoingDown,
Stopped
}
internal class Elevator
{
public int TopFloor;
public Elevator(int topFloor)
{
TopFloor = topFloor;
}
public int CurrentFloor { get; set; } = 1;
public Status Status { get; set; } = Status.Stopped;
public void MoveUp(int floor)
{
Status = Status.GoingUp;
Console.WriteLine("Going up to: {0}", floor);
CurrentFloor = floor;
OpenDoor();
CloseDoor();
}
public void MoveDown(int floor)
{
Status = Status.GoingDown;
Console.WriteLine("Going down to: {0}", floor);
CurrentFloor = floor;
OpenDoor();
CloseDoor();
}
private void OpenDoor()
{
Console.WriteLine("Door opening");
}
private void CloseDoor()
{
Console.WriteLine("Door closing, at floor: {0}", this.CurrentFloor);
}
}
internal class Request
{
public Request(int floor)
{
Floor = floor;
Also, does logic make sense?
- Once elevator starts going up it needs to finish all requests for above floors, same for down.
So I imagine that if the elevator is moving up and the request is floor below it will need to finish all up/above requests etc.
```
internal class Program
{
private const string _quit = "q";
private static void Main(string[] args)
{
var manager = new Manager();
var input = string.Empty;
while (input != _quit) {
Console.Write("Enter floor: ");
input = Console.ReadLine();
int floor;
if (int.TryParse(input, out floor))
manager.ButtonPressed(floor);
else if (input == _quit)
Console.WriteLine("GoodBye!");
else
Console.WriteLine("You have pressed an incorrect floor, Please try again");
}
}
}
internal enum Status
{
GoingUp,
GoingDown,
Stopped
}
internal class Elevator
{
public int TopFloor;
public Elevator(int topFloor)
{
TopFloor = topFloor;
}
public int CurrentFloor { get; set; } = 1;
public Status Status { get; set; } = Status.Stopped;
public void MoveUp(int floor)
{
Status = Status.GoingUp;
Console.WriteLine("Going up to: {0}", floor);
CurrentFloor = floor;
OpenDoor();
CloseDoor();
}
public void MoveDown(int floor)
{
Status = Status.GoingDown;
Console.WriteLine("Going down to: {0}", floor);
CurrentFloor = floor;
OpenDoor();
CloseDoor();
}
private void OpenDoor()
{
Console.WriteLine("Door opening");
}
private void CloseDoor()
{
Console.WriteLine("Door closing, at floor: {0}", this.CurrentFloor);
}
}
internal class Request
{
public Request(int floor)
{
Floor = floor;
Solution
I see a few things I would do differently. For one, I would not use a
I would keep a simple
Queue. At best, it's providing you an order in which floor buttons were pressed. While the buttons may be pressed in any order, an elevator moves in sequential order be it up or down.I would keep a simple
List where true denotes a floor to stop at. Once you stop at a floor, no need to Dequeue. Instead just set the flag to false. By keeping it simple, you won't have to jump through hoops if you are going up but someone presses buttons for 10, 5, 2, 9, 7.Context
StackExchange Code Review Q#161431, answer score: 2
Revisions (0)
No revisions yet.