Initial commit

This commit is contained in:
Mark J Price 2022-02-27 19:08:52 +00:00
parent bc96ccb183
commit 18f89e91d4
62 changed files with 1661 additions and 2 deletions

View file

@ -0,0 +1,22 @@
namespace Packt.Shared;
public struct DisplacementVector
{
public int X { get; set; }
public int Y { get; set; }
public DisplacementVector(int initialX, int initialY)
{
X = initialX;
Y = initialY;
}
public static DisplacementVector operator +(
DisplacementVector vector1,
DisplacementVector vector2)
{
return new(
vector1.X + vector2.X,
vector1.Y + vector2.Y);
}
}

View file

@ -0,0 +1,14 @@
namespace Packt.Shared;
public class DvdPlayer : IPlayable
{
public void Pause()
{
WriteLine("DVD player is pausing.");
}
public void Play()
{
WriteLine("DVD player is playing.");
}
}

View file

@ -0,0 +1,22 @@
namespace Packt.Shared;
public class Employee : Person
{
public string? EmployeeCode { get; set; }
public DateTime HireDate { get; set; }
public new void WriteToConsole()
{
WriteLine(format:
"{0} was born on {1:dd/MM/yy} and hired on {2:dd/MM/yy}",
arg0: Name,
arg1: DateOfBirth,
arg2: HireDate);
}
public override string ToString()
{
return $"{Name}'s code is {EmployeeCode}";
}
}

View file

@ -0,0 +1,11 @@
namespace Packt.Shared;
public interface IPlayable
{
void Play();
void Pause();
void Stop() // default interface implementation
{
WriteLine("Default implementation of Stop.");
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Console" Static="true" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,98 @@
namespace Packt.Shared;
public class Person : object, IComparable<Person?>
{
// properties
public string? Name { get; set; }
public DateTime DateOfBirth { get; set; }
// methods
public void WriteToConsole()
{
WriteLine($"{Name} was born on a {DateOfBirth:dddd}.");
}
// delegate field
public event EventHandler? Shout;
// data field
public int AngerLevel;
// method
public void Poke()
{
AngerLevel++;
if (AngerLevel >= 3)
{
/*
// if something is listening...
if (Shout != null)
{
// ...then call the delegate
Shout(this, EventArgs.Empty);
}
*/
// simplified syntax available in C# 6 or later
Shout?.Invoke(this, EventArgs.Empty);
}
}
public int CompareTo(Person? other)
{
int position;
if ((this is not null) && (other is not null))
{
if ((Name is not null) && (other.Name is not null))
{
// if both Name values are not null,
// use the string implementation of CompareTo
position = Name.CompareTo(other.Name);
}
else if ((Name is not null) && (other.Name is null))
{
position = -1; // else this Person precedes other Person
}
else if ((Name is null) && (other.Name is not null))
{
position = 1; // else this Person follows other Person
}
else
{
position = 0; // this Person and other Person are at same position
}
}
else if ((this is not null) && (other is null))
{
position = -1; // this Person precedes other Person
}
else if ((this is null) && (other is not null))
{
position = 1; // this Person follows other Person
}
else
{
position = 0; // this Person and other Person are at same position
}
return position;
}
// overridden methods
public override string ToString()
{
return $"{Name} is a {base.ToString()}";
}
public void TimeTravel(DateTime when)
{
if (when <= DateOfBirth)
{
throw new PersonException("If you travel back in time to a date earlier than your own birth, then the universe will explode!");
}
else
{
WriteLine($"Welcome to {when:yyyy}!");
}
}
}

View file

@ -0,0 +1,56 @@
namespace Packt.Shared;
public class PersonComparer : IComparer<Person?>
{
public int Compare(Person? x, Person? y)
{
int position;
if ((x is not null) && (y is not null))
{
if ((x.Name is not null) && (y.Name is not null))
{
// if both Name values are not null...
// ...compare the Name lengths...
int result = x.Name.Length.CompareTo(y.Name.Length);
/// ...if they are equal...
if (result == 0)
{
// ...then compare by the Names...
return x.Name.CompareTo(y.Name);
}
else
{
// ...otherwise compare by the lengths.
position = result;
}
}
else if ((x.Name is not null) && (y.Name is null))
{
position = -1; // else x Person precedes y Person
}
else if ((x.Name is null) && (y.Name is not null))
{
position = 1; // else x Person follows y Person
}
else
{
position = 0; // x Person and y Person are at same position
}
}
else if ((x is not null) && (y is null))
{
position = -1; // x Person precedes y Person
}
else if ((x is null) && (y is not null))
{
position = 1; // x Person follows y Person
}
else
{
position = 0; // x Person and y Person are at same position
}
return position;
}
}

View file

@ -0,0 +1,11 @@
namespace Packt.Shared;
public class PersonException : Exception
{
public PersonException() : base() { }
public PersonException(string message) : base(message) { }
public PersonException(string message, Exception innerException)
: base(message, innerException) { }
}

View file

@ -0,0 +1,15 @@
using System.Text.RegularExpressions; // to get Regex
namespace Packt.Shared;
public static class StringExtensions
{
public static bool IsValidEmail(this string input)
{
// use a simple regular expression to check
// that the input string is a valid email
return Regex.IsMatch(input,
@"[a-zA-Z0-9\.-_]+@[a-zA-Z0-9\.-_]+");
}
}