Compare commits
No commits in common. "master" and "controlling_the_flow_of_execution" have entirely different histories.
master
...
controllin
@ -1,5 +1,3 @@
|
|||||||
# pluralsight-csharp-fundamentals
|
# pluralsight-dotnet-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)
|
This is a repository for learning everything about .NET from pluralsight. Including multiple learning Paths
|
||||||
|
|
||||||
Notes are within wiki page
|
|
1
gradebook/.gitignore
vendored
1
gradebook/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
*Book.txt
|
|
@ -1,118 +1,31 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace GradeBook
|
namespace GradeBook
|
||||||
{
|
{
|
||||||
public delegate void GradeAddedDelegate(object sender, EventArgs args);
|
public class Book
|
||||||
|
|
||||||
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;
|
private List<double> grades;
|
||||||
|
public string Name;
|
||||||
|
|
||||||
readonly string category;
|
public Book(string name)
|
||||||
|
|
||||||
private string name;
|
|
||||||
|
|
||||||
public InMemoryBook(string name) : base(name)
|
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
grades = new List<double>();
|
grades = new List<double>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void AddGrade(double grade)
|
public void AddGrade(double grade)
|
||||||
{
|
{
|
||||||
if (grade <= 100 && grade >= 0)
|
if (grade <= 100 && grade >= 0)
|
||||||
{
|
{
|
||||||
this.grades.Add(grade);
|
this.grades.Add(grade);
|
||||||
|
|
||||||
if (GradeAdded != null)
|
|
||||||
{
|
|
||||||
// Somebody is listening/subscribing
|
|
||||||
GradeAdded(this, new EventArgs()); // sender = this, pass; Raising event
|
|
||||||
}
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Invalid {nameof(grade)}");
|
throw new ArgumentException($"Invalid {nameof(grade)}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddGrade(char letter)
|
public void AddLetterGrade(char letter)
|
||||||
{
|
{
|
||||||
switch (letter)
|
switch (letter)
|
||||||
{
|
{
|
||||||
@ -131,15 +44,39 @@ namespace GradeBook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override event GradeAddedDelegate GradeAdded;
|
public Statistics GetStatistics()
|
||||||
|
|
||||||
public override Statistics GetStatistics()
|
|
||||||
{
|
{
|
||||||
var result = new Statistics();
|
var result = new Statistics();
|
||||||
|
result.Average = 0.0;
|
||||||
|
result.highGrade = double.MinValue;
|
||||||
|
result.lowGrade = double.MaxValue;
|
||||||
|
|
||||||
foreach (var grade in this.grades)
|
foreach (var grade in this.grades)
|
||||||
{
|
{
|
||||||
result.Add(grade);
|
result.lowGrade = Math.Min(grade, result.lowGrade);
|
||||||
|
result.highGrade = Math.Max(grade, result.highGrade);
|
||||||
|
result.Average += grade;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Average /= grades.Count;
|
||||||
|
|
||||||
|
switch (result.Average)
|
||||||
|
{
|
||||||
|
case var d when d >= 90.0:
|
||||||
|
result.Letter = 'A';
|
||||||
|
break;
|
||||||
|
case var d when d >= 80.0:
|
||||||
|
result.Letter = 'B';
|
||||||
|
break;
|
||||||
|
case var d when d >= 70.0:
|
||||||
|
result.Letter = 'C';
|
||||||
|
break;
|
||||||
|
case var d when d >= 60.0:
|
||||||
|
result.Letter = 'D';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
result.Letter = 'F';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -8,23 +8,14 @@ namespace GradeBook
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
var book = new DiskBook("Jason's Grade Book");
|
// var book = new Book("Scott's Grade Book");
|
||||||
book.GradeAdded += OnGradeAdded;
|
// book.AddGrade(89.1);
|
||||||
|
// book.AddGrade(90.5);
|
||||||
|
// book.AddGrade(87.2);
|
||||||
|
|
||||||
|
var book = new Book("Jason's Grade Book");
|
||||||
string input;
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Give input: ");
|
Console.WriteLine("Give input: ");
|
||||||
@ -33,14 +24,12 @@ namespace GradeBook
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Receiving termination signal; Termiate program");
|
Console.WriteLine("Receiving termination signal; Termiate program");
|
||||||
break;
|
break;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Received: {input}");
|
Console.WriteLine($"Received: {input}");
|
||||||
book.AddGrade(Convert.ToDouble(input));
|
book.AddGrade(Convert.ToDouble(input));
|
||||||
// book.AddGrade('A');
|
|
||||||
}
|
}
|
||||||
catch (ArgumentException ex)
|
catch (ArgumentException ex)
|
||||||
{
|
{
|
||||||
@ -58,13 +47,11 @@ namespace GradeBook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
var stats = book.GetStatistics();
|
||||||
}
|
|
||||||
|
|
||||||
static void OnGradeAdded(object sender, EventArgs e)
|
Console.WriteLine($"The lowest grade is {stats.lowGrade}");
|
||||||
{
|
Console.WriteLine($"The highesst grade is {stats.highGrade}");
|
||||||
// Main is static, so it can only call static methods, hence return type is static
|
Console.WriteLine($"The average grade is {stats.Average:N1}");
|
||||||
Console.WriteLine("A line has added");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,10 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace GradeBook
|
namespace GradeBook
|
||||||
{
|
{
|
||||||
public class Statistics
|
public class Statistics
|
||||||
{
|
{
|
||||||
public double Average
|
public double Average;
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return Sum / Count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public double highGrade;
|
public double highGrade;
|
||||||
public double lowGrade;
|
public double lowGrade;
|
||||||
public char Letter
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ namespace GradeBook.Tests
|
|||||||
public void BookCalculateAnAverageGrade()
|
public void BookCalculateAnAverageGrade()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
var book = new InMemoryBook("");
|
var book = new Book("");
|
||||||
book.AddGrade(89.1);
|
book.AddGrade(89.1);
|
||||||
book.AddGrade(90.5);
|
book.AddGrade(90.5);
|
||||||
book.AddGrade(77.3);
|
book.AddGrade(77.3);
|
||||||
|
@ -4,36 +4,8 @@ using Xunit;
|
|||||||
namespace GradeBook.Tests
|
namespace GradeBook.Tests
|
||||||
{
|
{
|
||||||
using GradeBook;
|
using GradeBook;
|
||||||
|
|
||||||
public delegate string WriteLogDelegate(string logMessage);
|
|
||||||
|
|
||||||
public class TypeTests
|
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]
|
[Fact]
|
||||||
public void ValueTypesAlsoPassByValue()
|
public void ValueTypesAlsoPassByValue()
|
||||||
{
|
{
|
||||||
@ -77,9 +49,9 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "New Name");
|
Assert.Equal(book1.Name, "New Name");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetBookSetName(ref InMemoryBook book, string name)
|
private void GetBookSetName(ref Book book, string name)
|
||||||
{
|
{
|
||||||
book = new InMemoryBook(name);
|
book = new Book(name);
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,9 +64,9 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "Book1");
|
Assert.Equal(book1.Name, "Book1");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetBookSetName(InMemoryBook book, string name)
|
private void GetBookSetName(Book book, string name)
|
||||||
{
|
{
|
||||||
book = new InMemoryBook(name);
|
book = new Book(name);
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +79,7 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "New Name");
|
Assert.Equal(book1.Name, "New Name");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetName(InMemoryBook book, string name)
|
private void SetName(Book book, string name)
|
||||||
{
|
{
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
@ -133,9 +105,9 @@ namespace GradeBook.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private InMemoryBook GetBook(string name)
|
private Book GetBook(string name)
|
||||||
{
|
{
|
||||||
return new InMemoryBook(name);
|
return new Book(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user