Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Chapter11/AbstractionDesign/AbstractionDesign.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractionDesign", "AbstractionDesign\AbstractionDesign.csproj", "{5B600C0B-F23D-491E-9931-9A53C125EB71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B600C0B-F23D-491E-9931-9A53C125EB71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B600C0B-F23D-491E-9931-9A53C125EB71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B600C0B-F23D-491E-9931-9A53C125EB71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B600C0B-F23D-491E-9931-9A53C125EB71}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F37435DC-CFB8-4516-86C0-687625610CB9}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{5B600C0B-F23D-491E-9931-9A53C125EB71}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>AbstractionDesign</RootNamespace>
<AssemblyName>AbstractionDesign</AssemblyName>
<TargetFrameworkVersion>v4.7.2</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.Drawing" />
<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="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tools\Camera.cs" />
<Compile Include="Tools\Laser.cs" />
<Compile Include="Tools\Sensor.cs" />
<Compile Include="Tools\TouchProbe.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions Chapter11/AbstractionDesign/AbstractionDesign/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.7.2" />
</startup>
</configuration>
261 changes: 261 additions & 0 deletions Chapter11/AbstractionDesign/AbstractionDesign/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AbstractionDesign.Tools;

namespace AbstractionDesign
{
class Program
{
private delegate void CommandHandler(IEnumerable<string> parameters);
private static Sensor CurrentSensor;

static void Main(string[] args)
{
while (true)
{
Console.Write("> ");
var input = Console.ReadLine() ?? string.Empty;
var tokens = input.Split(' ');

if (tokens.Length < 1)
{
Console.WriteLine("Please supply a command");
continue;
}

var command = tokens.First();
var parameters = tokens.Skip(1);

var handlers = new Dictionary<string, CommandHandler>
{
{"select", SelectTool},
{"move", Move},
{"zoom", Zoom},
{"capture-image", CaptureImage},
{"measure-height", MeasureHeight},
{"pitch", Pitch},
{"roll", Roll},
{"raise", Raise},
{"lower", Lower},
{"get-pressure", GetPressure},
{"quit", Quit}
};

if (!handlers.ContainsKey(command))
{
Console.WriteLine($"Unrecognized command: {command}");
}
else
{
handlers[command].Invoke(parameters);
}
}
}

private static void SelectTool(IEnumerable<string> parameters)
{
switch (parameters.FirstOrDefault())
{
case "camera":
CurrentSensor = new Camera();
Console.WriteLine("Tool is now the camera");
break;
case "laser":
CurrentSensor = new Laser();
Console.WriteLine("Tool is now the laser");
break;
case "touchprobe":
CurrentSensor = new TouchProbe();
Console.WriteLine("Tool is now the touch probe");
break;
default:
Console.WriteLine("Possible Tools are camera, laser or touchprobe");
break;
}
}

private static void Move(IEnumerable<string> parameters)
{
if (CurrentSensor != null)
{
float x, y;

if (float.TryParse(parameters.FirstOrDefault(), out x) && float.TryParse(parameters.Skip(1).FirstOrDefault(), out y))
{
CurrentSensor.Move(x, y);
}
else
{
Console.WriteLine("Move requires two floating-point parameters (eg: 1.5 2.5)");
}
}
else
{
Console.WriteLine("Must select a tool!");
}
}

private static void Zoom(IEnumerable<string> parameters)
{
var camera = CurrentSensor as Camera;

if (camera != null)
{
float level;

if (float.TryParse(parameters.FirstOrDefault(), out level))
{
camera.Zoom(level);
}
else
{
Console.WriteLine("Zooming camera requires a floating-point parameter (eg: 1.5)");
}
}
else
{
Console.WriteLine("Must select camera to zoom!");
}
}

private static void CaptureImage(IEnumerable<string> parameters)
{
var camera = CurrentSensor as Camera;

if (camera != null)
{
Console.WriteLine(camera.Capture()); //TODO
}
else
{
Console.WriteLine("Must select camera to capture an image!");
}
}

private static void MeasureHeight(IEnumerable<string> parameters)
{
var laser = CurrentSensor as Laser;

if (laser != null)
{
Console.WriteLine(laser.Measure());
}
else
{
Console.WriteLine("Must select laser to measure height!");
}
}

private static void Pitch(IEnumerable<string> parameters)
{
var touchProbe = CurrentSensor as TouchProbe;

if (touchProbe != null)
{
float level;

if (float.TryParse(parameters.FirstOrDefault(), out level))
{
touchProbe.Pitch(level);
}
else
{
Console.WriteLine("Pitch requires a floating-point parameter (eg: 1.5)");
}
}
else
{
Console.WriteLine("Must select touch probe to pitch!");
}
}

private static void Roll(IEnumerable<string> parameters)
{
var touchProbe = CurrentSensor as TouchProbe;

if (touchProbe != null)
{
float level;

if (float.TryParse(parameters.FirstOrDefault(), out level))
{
touchProbe.Roll(level);
}
else
{
Console.WriteLine("Roll requires a floating-point parameter (eg: 1.5)");
}
}
else
{
Console.WriteLine("Must select touch probe to roll!");
}
}

private static void Raise(IEnumerable<string> parameters)
{
var touchProbe = CurrentSensor as TouchProbe;

if (touchProbe != null)
{
float level;

if (float.TryParse(parameters.FirstOrDefault(), out level))
{
touchProbe.Raise(level);
}
else
{
Console.WriteLine("Raise requires a floating-point parameter (eg: 1.5)");
}
}
else
{
Console.WriteLine("Must select touch probe to raise!");
}
}

private static void Lower(IEnumerable<string> parameters)
{
var touchProbe = CurrentSensor as TouchProbe;

if (touchProbe != null)
{
float level;

if (float.TryParse(parameters.FirstOrDefault(), out level))
{
touchProbe.Lower(level);
}
else
{
Console.WriteLine("Lower requires a floating-point parameter (eg: 1.5)");
}
}
else
{
Console.WriteLine("Must select touch probe to lower!");
}
}

private static void GetPressure(IEnumerable<string> parameters)
{
var touchProbe = CurrentSensor as TouchProbe;

if (touchProbe != null)
{
Console.WriteLine($"Pressure: {touchProbe.GetPressure().Value}");
}
else
{
Console.WriteLine("Must select touch probe to get pressure!");
}
}

private static void Quit(IEnumerable<string> parameters)
{
Environment.Exit(0);
}
}
}
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;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("AbstractionDesign")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AbstractionDesign")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("5b600c0b-f23d-491e-9931-9a53c125eb71")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading