Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiacomanici committed Aug 23, 2019
1 parent c1459e4 commit fa78903
Show file tree
Hide file tree
Showing 182 changed files with 5,803 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http:https://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>{E08F3A24-A75F-4EE3-AF92-D9913F1D928B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>AbstractFactory_Cars_Begin</RootNamespace>
<AssemblyName>AbstractFactory_Cars_Begin</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</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="Classes\Car.cs" />
<Compile Include="Classes\Engine.cs" />
<Compile Include="Classes\FuelStorage.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
85 changes: 85 additions & 0 deletions DesignPatternsCreational/AbstractFactory_Cars_Begin/Classes/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Timers;

namespace AbstractFactory_Cars_Begin.Classes
{
public class Car
{
private Timer _fuelComsumptionTimer;

public Engine Engine { get; private set; }
public FuelStorage FuelStorage { get; private set; }
public int Speed { get; private set; }

public Car(Engine engine, FuelStorage fuelStorage)
{
Engine = engine;
FuelStorage = fuelStorage;

_fuelComsumptionTimer = new Timer(500);
_fuelComsumptionTimer.Elapsed += FuelComsumptionTimer_Elapsed;
}

public void TurnOn()
{
if (Engine.State == EngineState.Stopped)
{
Engine.State = EngineState.Started;
Console.WriteLine($"Car is turned on and has FuelStorage with {FuelStorage.AvailableUnits} {FuelStorage.Unit}");
_fuelComsumptionTimer.Start();
}
else
{
throw new InvalidOperationException("The car is already started.");
}
}

public void AccelerateBy(int speedIncrement)
{
if (Engine.State == EngineState.Started)
{
Engine.State = EngineState.Moving;
}

if (Engine.State == EngineState.Moving)
{
Speed += speedIncrement;
Console.WriteLine($"Car accelerated by {speedIncrement} km/h. Speed is now {Speed} km/h");
}
else
{
throw new InvalidOperationException("You should first start the car and then accelerate.");
}
}

public void TurnOff()
{
if (Engine.State != EngineState.Stopped)
{
Engine.State = EngineState.Stopped;
Console.WriteLine("Car is turned off");
_fuelComsumptionTimer.Stop();
}
else
{
throw new InvalidOperationException("The car is already stopped.");
}
}

private void FuelComsumptionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
var consumption = Engine.GetComsumptionInHalfASecondBasedOnSpeed(Speed);
FuelStorage.AvailableUnits -= consumption;
if (FuelStorage.AvailableUnits > 0)
{
Console.WriteLine($"Consumption={consumption} {FuelStorage.Unit} for speed={Speed} km/h so, FuelStorage has {FuelStorage.AvailableUnits} {FuelStorage.Unit}");
}
else
{
FuelStorage.AvailableUnits = 0;
Console.WriteLine("Fuel storage is empty, so the car is being stopped");
TurnOff();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace AbstractFactory_Cars_Begin.Classes
{
public enum EngineState
{
Stopped,
Started,
Moving
}

public class Engine
{
public EngineState State { get; set; }

public double GetComsumptionInHalfASecondBasedOnSpeed(double speed)
{
if (State != EngineState.Stopped)
{
switch (speed)
{
case double n when (n == 0):
return 0.01;

case double n when (0 < n && n <= 50):
return 0.5;

case double n when (50 < n && n <= 80):
return 0.7;

case double n when (80 < n && n <= 110):
return 1;

case double n when (110 < n):
return 2;
}
}
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Timers;

namespace AbstractFactory_Cars_Begin.Classes
{
public class FuelStorage
{
private Timer _fillStorageTimer;

public double CapacityInUnits { get; protected set; }
public double AvailableUnits { get; set; }
public string Unit { get; protected set; }

public bool IsFilling
{
get { return _fillStorageTimer.Enabled; }
}

public FuelStorage(double capacityInUnits, double availableUnits, string unit)
{
CapacityInUnits = capacityInUnits;
AvailableUnits = availableUnits;
Unit = unit;

_fillStorageTimer = new Timer(500);
_fillStorageTimer.Elapsed += FillStorageTimer_Elapsed;
}

public void StartFilling()
{
Console.WriteLine($"Started filling storage from {AvailableUnits} {Unit} and capacity={CapacityInUnits} {Unit}");
_fillStorageTimer.Start();
}

public void StopFilling()
{
Console.WriteLine($"Stopped filling storage at {AvailableUnits} {Unit} and capacity={CapacityInUnits} {Unit}");
_fillStorageTimer.Stop();
}

private void FillStorageTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (AvailableUnits >= CapacityInUnits)
{
AvailableUnits = CapacityInUnits;
StopFilling();
}
else
{
var increment = GetUnitsToFillInHalfASecond();
AvailableUnits += increment;
Console.WriteLine($"Availability is being increased by={increment} {Unit} so, there are {AvailableUnits} {Unit} available and capacity={CapacityInUnits} {Unit}");
}
}

private double GetUnitsToFillInHalfASecond()
{
return 1;
}
}
}
26 changes: 26 additions & 0 deletions DesignPatternsCreational/AbstractFactory_Cars_Begin/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading;
using AbstractFactory_Cars_Begin.Classes;

namespace AbstractFactory_Cars_Begin
{
class Program
{
static void Main(string[] args)
{
var car = new Car(new Engine(), new FuelStorage(10, 8, "liters"));
car.TurnOn();
while (car.Engine.State != EngineState.Stopped)
{
car.AccelerateBy(30);
Thread.Sleep(1000);
}

car.FuelStorage.StartFilling();
Thread.Sleep(3000);
if (car.FuelStorage.IsFilling)
{
car.FuelStorage.StopFilling();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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("AbstractFactory_Cars_Begin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AbstractFactory_Cars_Begin")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[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("e08f3a24-a75f-4ee3-af92-d9913f1d928b")]

// 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")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http:https://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>{5D9B0778-29FB-4B71-97BF-1A921546729A}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>AbstractFactory_Cars_End</RootNamespace>
<AssemblyName>AbstractFactory_Cars_End</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</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="Classes\Car.cs" />
<Compile Include="Classes\Combustion\CombustionEngine.cs" />
<Compile Include="Classes\Combustion\CombustionFactory.cs" />
<Compile Include="Classes\Combustion\GasTank.cs" />
<Compile Include="Classes\Contracts\Engine.cs" />
<Compile Include="Classes\Contracts\FuelStorage.cs" />
<Compile Include="Classes\Contracts\ICarFactory.cs" />
<Compile Include="Classes\Electric\ElectricCarFactory.cs" />
<Compile Include="Classes\Electric\ElectricBattery.cs" />
<Compile Include="Classes\Electric\ElectricEngine.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions DesignPatternsCreational/AbstractFactory_Cars_End/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Loading

0 comments on commit fa78903

Please sign in to comment.