Compare commits

...

25 Commits

Author SHA1 Message Date
5396b614a0 Added URL for the course 2021-04-19 06:10:11 +00:00
f014ffcea4 Renamed repository as pluralsight-csharp-fundamentals 2021-02-23 02:58:22 +00:00
9ea0fbe973 Finished Refactoring Statistics 2021-02-23 00:41:44 +00:00
0435f6592e Finished Using IDisposable 2021-02-23 00:02:47 +00:00
55ce261ed8 Finished Defining an Abstract Class 2021-02-22 23:19:37 +00:00
fe71127f3a Finished Defining an Abstract Class 2021-02-22 12:26:08 +00:00
f7ef36beab Finished Deriving from a Base Class 2021-02-22 11:53:59 +00:00
2b07530bb3 Finished Defining an Event & Subscribing to an Event 2021-02-22 11:25:40 +00:00
4623c69c69 Finished Using Multi-cast Delegates 2021-02-22 10:37:35 +00:00
3a7ae11abb Finished Defining a Delegate 2021-02-22 10:11:30 +00:00
d47a773760 Finished Defining const Members 2021-02-22 03:59:18 +00:00
394a847465 Finished Defining Property Getters & Setters 2021-02-22 03:34:13 +00:00
1b64a1fa8b Finished Overload Methods; Finished Defining Properties 2021-02-22 03:28:38 +00:00
237b8fc391 Finished Solution, Throwing & Catching Exceptions, repo at 9f4de32 2021-02-22 00:56:24 +00:00
615d267706 Finished Challenge: Taking User Input from Console; By myself 2021-02-22 00:12:35 +00:00
41ca2586dc Finished Pattern Matching with switch 2021-02-19 04:33:27 +00:00
dd47f5cf72 Finished Switching with the swtich Statement 2021-02-19 03:54:21 +00:00
009cdc3919 Looping with for, foreach, do, while 2021-02-19 03:13:57 +00:00
6c7ba8624f Finished Looking For Reference Types & Value Types & The Special Case of String in .NET 2021-02-19 01:07:25 +00:00
8ce65cb61e Finished Passing Parameters by Reference 2021-02-19 00:30:31 +00:00
6f9f69b55f Finished Testing Object References & Referencing Different Objects 2021-02-18 01:41:40 +00:00
b6387e0e8c Finished Testing Your Code, Refactoring for Testability 2021-02-17 12:33:05 +00:00
41bf0f3824 Finished Referencing Projects and Packages 2021-02-17 02:30:04 +00:00
c36b3f5caf Added first assertion 2021-02-17 00:45:17 +00:00
dfdcdd1281 Creating a Unit Test Project 2021-02-17 00:14:18 +00:00
9 changed files with 491 additions and 26 deletions

View File

@ -1,3 +1,5 @@
# pluralsight-dotnet-paths
# pluralsight-csharp-fundamentals
This is a repository for learning everything about .NET from pluralsight. Including multiple learning Paths
This is a repository for learning course [**C# Fundamentals**](https://app.pluralsight.com/library/courses/csharp-fundamentals-dev) from pluralsight under Path [**C# Development Fundamentals**](https://app.pluralsight.com/paths/skill/csharp)
Notes are within wiki page

1
gradebook/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*Book.txt

56
gradebook/gradebook.sln Normal file
View File

@ -0,0 +1,56 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{41D689B9-3B52-432B-97EF-63FB665C1AA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GradeBook", "src\GradeBook\GradeBook.csproj", "{B67A3D2F-4755-46E6-960C-8746BADB002C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C32AB033-6E6D-4451-9194-0F6933BC383D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GradeBook.Tests", "test\GradeBook.Tests\GradeBook.Tests.csproj", "{203083CF-9050-48AC-87CC-53AF6F8F79BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x64.ActiveCfg = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x64.Build.0 = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x86.ActiveCfg = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x86.Build.0 = Debug|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|Any CPU.Build.0 = Release|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x64.ActiveCfg = Release|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x64.Build.0 = Release|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x86.ActiveCfg = Release|Any CPU
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x86.Build.0 = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x64.ActiveCfg = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x64.Build.0 = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x86.ActiveCfg = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x86.Build.0 = Debug|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|Any CPU.Build.0 = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x64.ActiveCfg = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x64.Build.0 = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x86.ActiveCfg = Release|Any CPU
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B67A3D2F-4755-46E6-960C-8746BADB002C} = {41D689B9-3B52-432B-97EF-63FB665C1AA7}
{203083CF-9050-48AC-87CC-53AF6F8F79BE} = {C32AB033-6E6D-4451-9194-0F6933BC383D}
EndGlobalSection
EndGlobal

View File

@ -1,42 +1,148 @@
using System;
using System.IO;
using System.Collections.Generic;
namespace GradeBook
{
class Book
public delegate void GradeAddedDelegate(object sender, EventArgs args);
public class NamedObject
{
public NamedObject(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}
public interface IBook
{
void AddGrade(double grade);
Statistics GetStatistics();
string Name { get; }
event GradeAddedDelegate GradeAdded;
}
public abstract class Book : NamedObject, IBook
{
// Abstract class used to act as entrypoint for other class, so output
// can change based on given input. At this level, we cannot provide implementation
// Let derived class to determine details.
public Book(string name) : base(name)
{
}
public abstract void AddGrade(double grade);
public abstract event GradeAddedDelegate GradeAdded;
public abstract Statistics GetStatistics();
}
public class DiskBook : Book
{
public DiskBook(string name) : base(name)
{
}
public override event GradeAddedDelegate GradeAdded;
public override void AddGrade(double grade)
{
using(var writer = File.AppendText($"{Name}.txt"))
{
writer.WriteLine(grade);
if (GradeAdded != null)
{
GradeAdded(this, new EventArgs());
}
}
}
public override Statistics GetStatistics()
{
var result = new Statistics();
using (var reader = File.OpenText($"{Name}.txt"))
{
var line = reader.ReadLine();
while(line != null)
{
var number = double.Parse(line);
result.Add(number);
line = reader.ReadLine();
}
}
return result;
}
}
public class InMemoryBook : Book, IBook
{
private List<double> grades;
readonly string category;
private string name;
public Book(string name)
public InMemoryBook(string name) : base(name)
{
this.name = name;
Name = name;
grades = new List<double>();
}
public void AddGrade(double grade)
public override void AddGrade(double grade)
{
this.grades.Add(grade);
if (grade <= 100 && grade >= 0)
{
this.grades.Add(grade);
if (GradeAdded != null)
{
// Somebody is listening/subscribing
GradeAdded(this, new EventArgs()); // sender = this, pass; Raising event
}
} else
{
throw new ArgumentException($"Invalid {nameof(grade)}");
}
}
public void ShowStaticstics()
public void AddGrade(char letter)
{
double result = 0;
var highGrade = double.MinValue;
var lowGrade = double.MaxValue;
foreach (var number in this.grades)
switch (letter)
{
highGrade = Math.Max(number, highGrade);
lowGrade = Math.Min(number, lowGrade);
result += number;
case 'A':
AddGrade(90);
break;
case 'B':
AddGrade(80);
break;
case 'C':
AddGrade(70);
break;
default:
AddGrade(0);
break;
}
result /= grades.Count;
}
Console.WriteLine($"The average grade is {result:N1}");
Console.WriteLine($"The highest grade is {highGrade:N1}");
Console.WriteLine($"The lowest grade is {lowGrade:N1}");
public override event GradeAddedDelegate GradeAdded;
public override Statistics GetStatistics()
{
var result = new Statistics();
foreach (var grade in this.grades)
{
result.Add(grade);
}
return result;
}
}

View File

@ -8,11 +8,63 @@ namespace GradeBook
static void Main(string[] args)
{
var book = new Book("Scott's Grade Book");
book.AddGrade(89.1);
book.AddGrade(90.5);
book.AddGrade(87.2);
book.ShowStaticstics();
var book = new DiskBook("Jason's Grade Book");
book.GradeAdded += OnGradeAdded;
string input;
input = EnterGrades(book);
var stats = book.GetStatistics();
Console.WriteLine($"The lowest grade is {stats.lowGrade}");
Console.WriteLine($"The highesst grade is {stats.highGrade}");
Console.WriteLine($"The average grade is {stats.Average:N1}");
}
private static string EnterGrades(Book book)
{
string input;
while (true)
{
Console.WriteLine("Give input: ");
input = Console.ReadLine();
if (input == "Q" || input == "q")
{
Console.WriteLine("Receiving termination signal; Termiate program");
break;
}
else
{
try
{
Console.WriteLine($"Received: {input}");
book.AddGrade(Convert.ToDouble(input));
// book.AddGrade('A');
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Will always run for all catch scenario
Console.WriteLine("Always Done");
}
}
}
return input;
}
static void OnGradeAdded(object sender, EventArgs e)
{
// Main is static, so it can only call static methods, hence return type is static
Console.WriteLine("A line has added");
}
}
}

View File

@ -0,0 +1,54 @@
using System;
namespace GradeBook
{
public class Statistics
{
public double Average
{
get
{
return Sum / Count;
}
}
public double highGrade;
public double lowGrade;
public char Letter
{
get{
switch (Average)
{
case var d when d >= 90.0:
return 'A';
case var d when d >= 80.0:
return 'B';
case var d when d >= 70.0:
return 'C';
case var d when d >= 60.0:
return 'D';
default:
return 'F';
}
}
}
public double Sum;
public int Count;
public void Add(double number)
{
Sum += number;
Count += 1;
lowGrade = Math.Min(number, lowGrade);
highGrade = Math.Max(number, highGrade);
}
public Statistics()
{
Count = 0;
Sum = 0.0;
highGrade = double.MinValue;
lowGrade = double.MaxValue;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using Xunit;
namespace GradeBook.Tests
{
public class BookTests
{
[Fact]
public void BookCalculateAnAverageGrade()
{
// arrange
var book = new InMemoryBook("");
book.AddGrade(89.1);
book.AddGrade(90.5);
book.AddGrade(77.3);
// act
var result = book.GetStatistics();
// assert
Assert.Equal(85.6, result.Average, 1);
Assert.Equal(90.5, result.highGrade);
Assert.Equal(77.3, result.lowGrade);
Assert.Equal('B', result.Letter);
}
}
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\GradeBook\GradeBook.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,141 @@
using System;
using Xunit;
namespace GradeBook.Tests
{
using GradeBook;
public delegate string WriteLogDelegate(string logMessage);
public class TypeTests
{
int count = 0;
[Fact]
public void WriteLogDelegateCanPointToMethod()
{
string msg = "test";
WriteLogDelegate log = new WriteLogDelegate(ReturnMessage); // Instantiate a delegate
log += ReturnMessage2; // Multi-cast delegate
var result = log(msg); // Invoking a delegate
Assert.Equal(result, msg);
Assert.Equal(count, 2);
}
string ReturnMessage(string message)
{
count += 1;
return message;
}
string ReturnMessage2(string message)
{
count += 1;
return message;
}
[Fact]
public void ValueTypesAlsoPassByValue()
{
var x = GetInt();
Assert.Equal(3,x);
SetInt(ref x);
Assert.Equal(42, x);
}
private void SetInt(ref int x)
{
x = 42;
}
private int GetInt()
{
return 3;
}
[Fact]
public void StringsBehaveLikeValueTypes()
{
string name = "Jason";
MakeUppserCase(name); // try to change String like a reference
Assert.Equal("Jason", name); // ToUpper does not work on String, although it's reference. It's copied as value
}
private void MakeUppserCase(string arg)
{
arg.ToUpper();
}
[Fact]
public void CSharpCanPassByReference()
{
var book1 = GetBook("Book1");
GetBookSetName(ref book1, "New Name");
Assert.Equal(book1.Name, "New Name");
}
private void GetBookSetName(ref InMemoryBook book, string name)
{
book = new InMemoryBook(name);
book.Name = name;
}
[Fact]
public void CSharpIsPassByValue()
{
var book1 = GetBook("Book1");
GetBookSetName(book1, "New Name");
Assert.Equal(book1.Name, "Book1");
}
private void GetBookSetName(InMemoryBook book, string name)
{
book = new InMemoryBook(name);
book.Name = name;
}
[Fact]
public void CanSetNameFromReference()
{
var book1 = GetBook("Book1");
SetName(book1, "New Name");
Assert.Equal(book1.Name, "New Name");
}
private void SetName(InMemoryBook book, string name)
{
book.Name = name;
}
[Fact]
public void GetBookReturnsDifferentObjects()
{
var book1 = GetBook("Book1");
var book2 = GetBook("Book2");
Assert.Equal("Book1", book1.Name);
Assert.Equal("Book2", book2.Name);
}
[Fact]
public void TwoVarsCanReferenceSameObject()
{
var book1 = GetBook("Book1");
var book2 = book1;
Assert.Equal(book2.Name, book1.Name);
Assert.True(Object.ReferenceEquals(book1, book2));
}
private InMemoryBook GetBook(string name)
{
return new InMemoryBook(name);
}
}
}