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)
|
||||
|
||||
Notes are within wiki page
|
||||
This is a repository for learning everything about .NET from pluralsight. Including multiple learning Paths
|
1
gradebook/.gitignore
vendored
1
gradebook/.gitignore
vendored
@ -1 +0,0 @@
|
||||
*Book.txt
|
@ -1,118 +1,31 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GradeBook
|
||||
{
|
||||
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
|
||||
public class Book
|
||||
{
|
||||
private List<double> grades;
|
||||
public string Name;
|
||||
|
||||
readonly string category;
|
||||
|
||||
private string name;
|
||||
|
||||
public InMemoryBook(string name) : base(name)
|
||||
public Book(string name)
|
||||
{
|
||||
Name = name;
|
||||
grades = new List<double>();
|
||||
}
|
||||
|
||||
public override void AddGrade(double grade)
|
||||
public void AddGrade(double 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 AddGrade(char letter)
|
||||
public void AddLetterGrade(char letter)
|
||||
{
|
||||
switch (letter)
|
||||
{
|
||||
@ -131,15 +44,39 @@ namespace GradeBook
|
||||
}
|
||||
}
|
||||
|
||||
public override event GradeAddedDelegate GradeAdded;
|
||||
|
||||
public override Statistics GetStatistics()
|
||||
public Statistics GetStatistics()
|
||||
{
|
||||
var result = new Statistics();
|
||||
result.Average = 0.0;
|
||||
result.highGrade = double.MinValue;
|
||||
result.lowGrade = double.MaxValue;
|
||||
|
||||
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;
|
||||
|
@ -8,23 +8,14 @@ namespace GradeBook
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
var book = new DiskBook("Jason's Grade Book");
|
||||
book.GradeAdded += OnGradeAdded;
|
||||
// var book = new Book("Scott's Grade Book");
|
||||
// book.AddGrade(89.1);
|
||||
// book.AddGrade(90.5);
|
||||
// book.AddGrade(87.2);
|
||||
|
||||
var book = new Book("Jason's Grade Book");
|
||||
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: ");
|
||||
@ -33,14 +24,12 @@ namespace GradeBook
|
||||
{
|
||||
Console.WriteLine("Receiving termination signal; Termiate program");
|
||||
break;
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Received: {input}");
|
||||
book.AddGrade(Convert.ToDouble(input));
|
||||
// book.AddGrade('A');
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
@ -58,13 +47,11 @@ namespace GradeBook
|
||||
}
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
var stats = book.GetStatistics();
|
||||
|
||||
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");
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,54 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace GradeBook
|
||||
{
|
||||
public class Statistics
|
||||
{
|
||||
public double Average
|
||||
{
|
||||
get
|
||||
{
|
||||
return Sum / Count;
|
||||
}
|
||||
}
|
||||
public double Average;
|
||||
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;
|
||||
}
|
||||
public char Letter;
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace GradeBook.Tests
|
||||
public void BookCalculateAnAverageGrade()
|
||||
{
|
||||
// arrange
|
||||
var book = new InMemoryBook("");
|
||||
var book = new Book("");
|
||||
book.AddGrade(89.1);
|
||||
book.AddGrade(90.5);
|
||||
book.AddGrade(77.3);
|
||||
|
@ -4,36 +4,8 @@ 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()
|
||||
{
|
||||
@ -77,9 +49,9 @@ namespace GradeBook.Tests
|
||||
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;
|
||||
}
|
||||
|
||||
@ -92,9 +64,9 @@ namespace GradeBook.Tests
|
||||
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;
|
||||
}
|
||||
|
||||
@ -107,7 +79,7 @@ namespace GradeBook.Tests
|
||||
Assert.Equal(book1.Name, "New Name");
|
||||
}
|
||||
|
||||
private void SetName(InMemoryBook book, string name)
|
||||
private void SetName(Book book, string 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