Browse Source

Added initial Parking Lot files

master
Teknikode 4 years ago
parent
commit
777416f85f

+ 22
- 0
ParkingLot.sln View File

@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParkingLot", "ParkingLot\ParkingLot.csproj", "{D881E283-2360-4F94-AF77-936FCA9DA0B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D881E283-2360-4F94-AF77-936FCA9DA0B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D881E283-2360-4F94-AF77-936FCA9DA0B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D881E283-2360-4F94-AF77-936FCA9DA0B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D881E283-2360-4F94-AF77-936FCA9DA0B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

+ 6
- 0
ParkingLot/App.config View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

+ 25
- 0
ParkingLot/Parking Lot/Level.cs View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace ParkingLot
{
public class Level
{
private List<Row> rows;

public List<Row> Rows
{
get { return rows; }
set { rows = value; }
}

public Level()
{
SetDefaults();
}

public void SetDefaults()
{
Rows = new List<Row>();
}
}
}

+ 194
- 0
ParkingLot/Parking Lot/ParkingLot.cs View File

@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Timers;
using ParkingLot.Vehicles;

namespace ParkingLot
{
public class ParkingLot
{
private const double _BaseRate = 1.0; // The rate per RateTime
private const double _FlatRate = 40.0; // The flat rate applied for times over MaxTime
private const int _RateTime = 15; // The time in minutes for the base rate
private const int _MinTime = 60; // The minimum time for being charged
private const int _MaxTime = 480; // The maximum time for the rate to be applied

public event Action<double> VehicleLeftEvent;

private List<Level> levels;

public List<Level> Levels
{
get { return levels; }
set { levels = value; }
}

public ParkingLot()
{
SetDefaults();
}

public void SetDefaults()
{
Levels = new List<Level>();
}

public static ParkingLot CreateParkingLot(int levels, int maxRows, int maxSpaces)
{
ParkingLot newParkingLot = new ParkingLot();
List<Level> newLevels = new List<Level>();
Random rand = new Random();
for (int l = 0; l < levels; l++)
{
Level newLevel = new Level();
int randRows = rand.Next(1, maxRows);
List<Row> newRows = new List<Row>();
for (int r = 0; r < randRows; r++)
{
Row newRow = new Row();
int randSpaces = rand.Next(1, maxSpaces);
List<Space> newSpaces = new List<Space>();
for (int s = 0; s < randSpaces; s++)
{
Space newSpace = new Space();
int randType = rand.Next(0, 10);
if (randType < 3)
{
newSpace.SpaceType = SpaceType.Motorcycle;
}
else if (randType >= 3 && randType < 6)
{
newSpace.SpaceType = SpaceType.Compact;
}
else if (randType >= 6)
{
newSpace.SpaceType = SpaceType.Large;
}
newSpaces.Add(newSpace);
}
newRow.Spaces = newSpaces;
newRows.Add(newRow);
}
newLevel.Rows = newRows;
newLevels.Add(newLevel);
}
newParkingLot.Levels = newLevels;

return newParkingLot;
}

public bool AddVehicle(Vehicle vehicle)
{
bool vehicleAdded = false;

int levelID = 0;
foreach (Level level in Levels)
{
int rowID = 0;
foreach (Row row in level.Rows)
{
int numOpen = 0;
for (int i = 0; i < row.Spaces.Count; i++)
{
Space space = row.Spaces[i];
if (space.CurrentVehicle == null &&
((vehicle.VehicleType == VehicleType.Motorcycle && (space.SpaceType == SpaceType.Motorcycle || space.SpaceType == SpaceType.Compact))
|| (vehicle.VehicleType == VehicleType.Car && (space.SpaceType == SpaceType.Compact || space.SpaceType == SpaceType.Large))
|| (vehicle.VehicleType == VehicleType.Bus && space.SpaceType == SpaceType.Large)))
{
numOpen++;
if (numOpen == vehicle.NumSpaces)
{
vehicle.TimeArrived = DateTime.Now;
vehicle.LeaveTimer.Enabled = true;
for (int x = 0; x < vehicle.NumSpaces; x++)
{
vehicle.Level = levelID;
vehicle.Row = rowID;
vehicle.Spaces.Add(i - x);
row.Spaces[i - x].CurrentVehicle = vehicle;
}
vehicle.LeaveTimer.Elapsed += (sender, e) => VehicleLeaveHandler(sender, e, vehicle.Level, vehicle.Row, vehicle.Spaces);
vehicleAdded = true;
break;
}
}
else
{
numOpen = 0;
}
}
rowID++;
if (vehicleAdded)
break;
}
levelID++;
if (vehicleAdded)
break;
}

return vehicleAdded;
}

private void VehicleLeaveHandler(object sender, EventArgs e, int level, int row, List<int> spaces)
{
double price = RemoveVehicle(level, row, spaces);
var vehicleLeft = VehicleLeftEvent;
if (vehicleLeft != null)
{
vehicleLeft(price);
}
}

public double RemoveVehicle(int level, int row, List<int> spaces)
{
double price = 0;

if (Levels.Count > level)
{
Level foundLevel = Levels[level];
if (foundLevel.Rows.Count > row)
{
Row foundRow = foundLevel.Rows[row];
if (foundRow.Spaces.Count > spaces.Max())
{
bool vehicleFound = false;
DateTime foundTime = DateTime.Now;
foreach (int space in spaces)
{
Space foundSpace = foundRow.Spaces[space];
if (foundSpace.CurrentVehicle != null)
{
vehicleFound = true;
foundTime = foundSpace.CurrentVehicle.TimeArrived;
foundSpace.CurrentVehicle.LeaveTimer.Enabled = false;
foundSpace.CurrentVehicle = null;
}
}
if (vehicleFound)
{
TimeSpan timeSpan = DateTime.Now.Subtract(foundTime);
double numMinutes = timeSpan.TotalSeconds; // For testing purposes, use seconds as our 'minutes'
if (numMinutes <= _MinTime)
{
price = _BaseRate*(_MinTime/_RateTime);
}
else if (numMinutes > _MaxTime)
{
price = _FlatRate;
}
else
{
price = _BaseRate*(numMinutes/_RateTime);
}
}
}
}
}

return price;
}
}
}

+ 25
- 0
ParkingLot/Parking Lot/Row.cs View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace ParkingLot
{
public class Row
{
private List<Space> spaces;

public List<Space> Spaces
{
get { return spaces; }
set { spaces = value; }
}

public Row()
{
SetDefaults();
}

public void SetDefaults()
{
Spaces = new List<Space>();
}
}
}

+ 34
- 0
ParkingLot/Parking Lot/Space.cs View File

@@ -0,0 +1,34 @@
using ParkingLot.Vehicles;

namespace ParkingLot
{
public class Space
{
private SpaceType spaceType;

public SpaceType SpaceType
{
get { return spaceType; }
set { spaceType = value; }
}

private Vehicle currentVehicle;

public Vehicle CurrentVehicle
{
get { return currentVehicle; }
set { currentVehicle = value; }
}

public Space()
{
SetDefaults();
}

public void SetDefaults()
{
SpaceType = SpaceType.Large;
CurrentVehicle = null;
}
}
}

+ 9
- 0
ParkingLot/Parking Lot/SpaceType.cs View File

@@ -0,0 +1,9 @@
namespace ParkingLot
{
public enum SpaceType
{
Motorcycle,
Compact,
Large
}
}

+ 67
- 0
ParkingLot/ParkingLot.csproj View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D881E283-2360-4F94-AF77-936FCA9DA0B1}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ParkingLot</RootNamespace>
<AssemblyName>ParkingLot</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Parking Lot\Level.cs" />
<Compile Include="Parking Lot\ParkingLot.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Parking Lot\Row.cs" />
<Compile Include="Parking Lot\Space.cs" />
<Compile Include="Parking Lot\SpaceType.cs" />
<Compile Include="Vehicles\Vehicle.cs" />
<Compile Include="Vehicles\VehicleType.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 50
- 0
ParkingLot/Program.cs View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ParkingLot.Vehicles;

namespace ParkingLot
{
class Program
{
static void Main(string[] args)
{
ParkingLot parkingLot = ParkingLot.CreateParkingLot(5, 5, 20);
parkingLot.VehicleLeftEvent += ParkingLot_VehicleLeftEvent;
Console.WriteLine("Type 'add' to add a vehicle. 'exit' to close the program.");
bool run = true;

while (run)
{
string command = Console.ReadLine();
switch (command)
{
case "add":
Vehicle newVehicle = Vehicle.CreateVehicle(600);
bool success = parkingLot.AddVehicle(newVehicle);
if (success)
{
Console.WriteLine("{0} Parked Successfully at Level {1}, Row {2}, Space {3}", newVehicle.VehicleType, newVehicle.Level, newVehicle.Row, string.Join(",", newVehicle.Spaces));
}
else
{
Console.WriteLine("The parking lot was full for the {0}", newVehicle.VehicleType);
}
break;
case "exit":
run = false;
break;
default:
break;
}
}
}

private static void ParkingLot_VehicleLeftEvent(double price)
{
Console.WriteLine("Vehicle Left - Price: ${0}", price);
}
}
}

+ 36
- 0
ParkingLot/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ParkingLot")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ParkingLot")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d881e283-2360-4f94-af77-936fca9da0b1")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 108
- 0
ParkingLot/Vehicles/Vehicle.cs View File

@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Timers;

namespace ParkingLot.Vehicles
{
public class Vehicle
{
private VehicleType vehicleType;

public VehicleType VehicleType
{
get { return vehicleType; }
set { vehicleType = value; }
}

private int numSpaces;

public int NumSpaces
{
get { return numSpaces; }
set { numSpaces = value; }
}

private DateTime timeArrived;

public DateTime TimeArrived
{
get { return timeArrived; }
set { timeArrived = value; }
}

private int level;

public int Level
{
get { return level; }
set { level = value; }
}

private int row;

public int Row
{
get { return row; }
set { row = value; }
}

private List<int> space;

public List<int> Spaces
{
get { return space; }
set { space = value; }
}

public event Action<double> VehicleLeftEvent;

private Timer leaveTimer;

public Timer LeaveTimer
{
get { return leaveTimer; }
set { leaveTimer = value; }
}

public Vehicle()
{
SetDefaults();
}

public void SetDefaults()
{
VehicleType = VehicleType.Car;
NumSpaces = 1;
TimeArrived = DateTime.Now;
Level = 0;
Row = 0;
Spaces = new List<int>();
}

public static Vehicle CreateVehicle(int maxTime)
{
Vehicle newVehicle = new Vehicle();
Random rand = new Random();
int randType = rand.Next(1, 10);
if (randType < 3)
{
newVehicle.VehicleType = VehicleType.Motorcycle;
newVehicle.NumSpaces = 1;
}
else if (randType <= 7)
{
newVehicle.VehicleType = VehicleType.Car;
newVehicle.NumSpaces = 1;
}
else if (randType > 7)
{
newVehicle.VehicleType = VehicleType.Bus;
newVehicle.NumSpaces = 5;
}
int randTimer = rand.Next(1, maxTime);
newVehicle.LeaveTimer = new Timer(randTimer * 1000);

return newVehicle;
}
}
}

+ 9
- 0
ParkingLot/Vehicles/VehicleType.cs View File

@@ -0,0 +1,9 @@
namespace ParkingLot.Vehicles
{
public enum VehicleType
{
Motorcycle,
Car,
Bus
}
}

Loading…
Cancel
Save