I am a newbie when it comes to Object Oriented Programming and I have read a lot of online articles and lessons so I want to try it out on my own. I've written a console app in C# and tried implementing Object Oriented Design and SOLID Principles.
The console app checks if a person has an access to a certain floor. Also, The app is in its initial stage so right now the Elevator accomodates a single person.
Although the app works. any feedback,comments, and suggestions will be greatly appreciated especially in terms of SOLID principles or OOP Design.
here is the code below
class Program
{
static void Main(string[] args)
{
Person maintenanceEmployee = new MaintenanceEmployee();
Elevator elev = new Elevator(maintenanceEmployee);
elev.GoToFloor(Floors.FourthFloor);
Person guest = new Guest();
Elevator elev2 = new Elevator(guest);
elev2.GoToFloor(Floors.SecondFloor);
Person empfromthirdfloor = new EmployeeFromThirdFloor();
Elevator elev3 = new Elevator(empfromthirdfloor);
elev2.GoToFloor(Floors.ThirdFloor);
Console.ReadLine();
}
}
public enum Floors
{
FirstFloor = 1,
SecondFloor = 2,
ThirdFloor = 3,
FourthFloor = 4,
FifthFloor = 5
}
public class Elevator
{
Person person;
public Elevator(Person _person)
{
this.person = _person;
}
public void GoToFloor(Floors _floor)
{
if (person.HasAccess(_floor))
{
Console.WriteLine("Elevating to " + _floor.ToString());
}
else
{
Console.WriteLine("No Access to " + _floor.ToString());
}
}
}
public abstract class Person
{
public Floors[] accessibleFloors;
public bool HasAccess(Floors _floor)
{
foreach (Floors i in accessibleFloors)
{
if (_floor == i)
{
return true;
}
}
return false;
}
}
public class MaintenanceEmployee : Person
{
public Floors[] _accessibleFloors = { Floors.FirstFloor, Floors.SecondFloor, Floors.ThirdFloor, Floors.FourthFloor, Floors.FifthFloor };
public MaintenanceEmployee()
{
//Not sure if this is the best practice
//Reason why I did it like this is because
//I need to change the arrays of accessibleFloors
//in the abstract class "Person" in order to run successfully
base.accessibleFloors = _accessibleFloors;
}
}
public class EmployeeFromThirdFloor : Person
{
public Floors[] _accessibleFloors = { Floors.ThirdFloor };
public EmployeeFromThirdFloor()
{
//Not sure if this is the best practice
//Reason why I did it like this is because
//I need to change the arrays of accessibleFloors
//of the abstract class "Person" in order to run successfully
base.accessibleFloors = _accessibleFloors;
}
}
public class Guest : Person
{
public Floors[] _accessibleFloors = { Floors.SecondFloor };
public Guest()
{
//Not sure if this is the best practice
//Reason why I did it like this is because
//I need to change the arrays of accessibleFloors
//of the abstract class "Person" in order to run successfully
base.accessibleFloors = _accessibleFloors;
}
}