PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
10
DefensiveProgramming/Barricades/Barricades.csproj
Normal file
10
DefensiveProgramming/Barricades/Barricades.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
53
DefensiveProgramming/Barricades/Course.cs
Normal file
53
DefensiveProgramming/Barricades/Course.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Barricades;
|
||||
|
||||
class Course(string name)
|
||||
{
|
||||
public string Name { get; private set; } = name;
|
||||
|
||||
//Consider why is dictionary private variable, and why not a property!
|
||||
//indexer serves as a barricade
|
||||
|
||||
private Dictionary<string, int> grades = new Dictionary<string, int>();
|
||||
|
||||
/// <summary>
|
||||
/// Assign the new grade to the student.
|
||||
/// Old value (if had existed) is replaced.
|
||||
/// </summary>
|
||||
/// <param name="name">Student's name</param>
|
||||
/// <returns>The student's grade or -1 if the student does not have a grade</returns>
|
||||
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if value is not valid grade [1-5]</exception>
|
||||
public int this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
//what if there is no student's name in the dictionary
|
||||
//throw exception, return special value? See error handling techniques
|
||||
bool exists = grades.TryGetValue(name, out int grade);
|
||||
return exists ? grade : -1;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 1 || value > 5)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Invalid grade {value}. It shoud be from 1 to 5");
|
||||
}
|
||||
|
||||
grades[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public double AverageGrade()
|
||||
{
|
||||
int sum = 0;
|
||||
foreach (var pair in grades)
|
||||
{
|
||||
int grade = pair.Value;
|
||||
Debug.Assert(grade >= 1 && grade <= 5,
|
||||
$"Invalid grade in dictionary {pair.Key} = {pair.Value}. This should never happens!");
|
||||
sum += grade;
|
||||
}
|
||||
return grades.Count > 0 ? (double)sum / grades.Count : 0;
|
||||
}
|
||||
}
|
||||
14
DefensiveProgramming/Barricades/Program.cs
Normal file
14
DefensiveProgramming/Barricades/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Barricades;
|
||||
using System.Text;
|
||||
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
|
||||
Course c = new Course("PI");
|
||||
c["Pero"] = 2;
|
||||
c["Ana"] = 5;
|
||||
c["Ivan"] = 5;
|
||||
c["Marko"] = 1;
|
||||
c["Luka"] = 3;
|
||||
//c["Marija"] = -7; //should throw an exception
|
||||
Console.WriteLine("Average grade: " + c.AverageGrade());
|
||||
Console.WriteLine("Šime has grade: " + c["Šime"]); //prints -1
|
||||
Reference in New Issue
Block a user