Skip to content

Instantly share code, notes, and snippets.

@AntonC9018
Created April 25, 2025 18:17
Show Gist options
  • Save AntonC9018/d3d959ddf9f5ee1657269110df19fc58 to your computer and use it in GitHub Desktop.
Save AntonC9018/d3d959ddf9f5ee1657269110df19fc58 to your computer and use it in GitHub Desktop.
Lab2 example from video
using System.Diagnostics;
class General
{
public static void Main()
{
var kitty = new DomesticCat(
new()
{
FurSoftness = 1,
AgeInYears = 3,
},
"Kitty");
var cub = new Lion(
new()
{
FurSoftness = 5,
AgeInYears = 1,
});
var lion = new Lion(
new()
{
FurSoftness = 5,
AgeInYears = 10,
},
cubs: [cub]);
var wolf = new Wolf(
new()
{
EyeCount = 2,
AgeInYears = 10,
});
var dogA = new DomesticDog(
name: "A",
ageInYears: 5,
breed: Breed.Dachshund);
var dogB = new DomesticDog(
name: "B",
ageInYears: 6,
breed: Breed.Labrador);
var strayCat = new DomesticCat();
Console.WriteLine($"Cub softness on average: {lion.AverageCubSoftness}");
Animal[] animals = [
kitty,
cub,
lion,
wolf,
dogA,
dogB,
strayCat,
DomesticDog.CreateRandom(Random.Shared)];
var thingsWithVoice =
animals.OfType<IShowOffVoice>().ToList();
AllShowOffVoice();
AllShowOffVoice();
void AllShowOffVoice()
{
foreach (var voice in thingsWithVoice)
{
voice.ShowOffVoice();
}
}
foreach (var a in animals)
{
if (a is IDomesticated named)
{
var name = named.Name;
Console.WriteLine(name);
}
}
foreach (var a in animals)
{
a.OnYearPassed();
}
}
}
interface INamed
{
public string Name { get; }
}
interface IDomesticated : INamed
{
}
interface IPrintable
{
}
interface ISerializer<T>
{
public void WriteToFile(T obj, TextWriter output);
public T ReadFromFile(TextReader input);
}
class AnimalParams
{
public required int AgeInYears;
public required int EyeCount;
}
abstract class Animal
{
private int _ageInYears;
private int _eyeCount;
protected Animal(AnimalParams animalParams)
{
Debug.Assert(animalParams.AgeInYears >= 0);
_ageInYears = animalParams.AgeInYears;
_eyeCount = animalParams.EyeCount;
}
protected Animal()
{
}
public int Age
{
get
{
return _ageInYears;
}
}
public void OnYearPassed()
{
_ageInYears++;
OnYearPassedTrigger();
}
protected virtual void OnYearPassedTrigger()
{
}
protected void TestStuff()
{
Console.WriteLine("Testing");
}
}
class CatParams
{
public const int DefaultEyeCount = 2;
public required int FurSoftness;
public required int AgeInYears;
public int EyeCount = DefaultEyeCount;
}
abstract class Cat : Animal
{
private int _furSoftness;
public int FurSoftness
{
get
{
TestStuff();
return _furSoftness;
}
}
protected Cat() :
base(new()
{
EyeCount = CatParams.DefaultEyeCount,
AgeInYears = 0,
})
{
}
protected Cat(CatParams catParams)
: base(new()
{
EyeCount = catParams.EyeCount,
AgeInYears = catParams.AgeInYears,
})
{
_furSoftness = catParams.FurSoftness;
}
protected override void OnYearPassedTrigger()
{
_furSoftness = Math.Max(0, _furSoftness - 1);
}
}
sealed class DomesticCat : Cat, IDomesticated
{
private string _name;
public string Name
{
get
{
return _name;
}
}
public DomesticCat() : base()
{
_name = "unnamed";
}
public DomesticCat(
CatParams catParams,
string firstName)
: base(catParams)
{
var lastName = GetLastNameBasedOnSoftness();
_name = $"{firstName} {lastName}";
}
private string GetLastNameBasedOnSoftness()
{
switch (FurSoftness)
{
case 0:
{
return "Doe";
}
case 1:
{
return "Meow";
}
default:
{
return "Hello";
}
}
}
}
sealed class Lion : Cat, IShowOffVoice
{
private readonly Lion[] _cubs;
public Lion(CatParams catParams, Lion[]? cubs = null)
: base(catParams)
{
if (cubs != null)
{
Debug.Assert(cubs.All(x => x.Age < Age));
_cubs = cubs;
}
else
{
_cubs = [];
}
}
public float AverageCubSoftness
{
get
{
if (_cubs.Length == 0)
{
return 0;
}
int t = 0;
foreach (var c in _cubs)
{
t += c.FurSoftness;
}
return (float) t / (float) _cubs.Length;
}
}
public void ShowOffVoice()
{
Console.WriteLine("Roar");
}
sealed class LionWriter : ISerializer<Lion>
{
public void WriteToFile(Lion obj, TextWriter output)
{
foreach (var cub in obj._cubs)
{
this.WriteToFile(cub, output);
}
}
public Lion ReadFromFile(TextReader input)
{
}
}
}
interface IShowOffVoice
{
public void ShowOffVoice();
}
abstract class Dog : Animal, IShowOffVoice
{
protected Dog(AnimalParams animalParams)
: base(animalParams)
{
}
public abstract void ShowOffVoice();
}
public enum Breed
{
Labrador,
Dachshund,
Unknown,
Count,
}
sealed class DomesticDog : Dog, IDomesticated
{
private readonly Breed _breed;
private readonly string _name;
public DomesticDog(string name, int ageInYears, Breed breed)
: base(new()
{
EyeCount = 2,
AgeInYears = ageInYears,
})
{
_name = name;
_breed = breed;
}
public string Name
{
get
{
return _name;
}
}
public override void ShowOffVoice()
{
var voice = GetVoiceString();
Console.WriteLine(voice);
}
private string GetVoiceString()
{
switch (_breed)
{
case Breed.Dachshund:
{
return "Woof";
}
case Breed.Labrador:
{
return "Bark";
}
case Breed.Unknown:
{
return "Oof";
}
default:
{
Debug.Fail("Unrecognized dog type");
return "";
}
}
}
private static readonly string[] _PossibleRandomNames = [
"Good Boy",
"Luke",
"Sharik",
];
public static DomesticDog CreateRandom(Random r)
{
// ...
string name;
{
int nameIndex = r.Next(_PossibleRandomNames.Length);
name = _PossibleRandomNames[nameIndex];
}
int ageInYears;
{
ageInYears = r.Next(15);
}
Breed breed;
{
int breedAsNumber = r.Next((int) Breed.Count);
breed = (Breed) breedAsNumber;
}
return new DomesticDog(
name,
ageInYears,
breed);
}
}
sealed class Wolf : Dog, INamed
{
public Wolf(AnimalParams animalParams)
: base(animalParams)
{
}
public string Name
{
get
{
return "Wolf";
}
}
public override void ShowOffVoice()
{
Console.WriteLine("Howl");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment