Compare commits
13 Commits
controllin
...
master
Author | SHA1 | Date | |
---|---|---|---|
5396b614a0 | |||
f014ffcea4 | |||
9ea0fbe973 | |||
0435f6592e | |||
55ce261ed8 | |||
fe71127f3a | |||
f7ef36beab | |||
2b07530bb3 | |||
4623c69c69 | |||
3a7ae11abb | |||
d47a773760 | |||
394a847465 | |||
1b64a1fa8b |
@ -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
1
gradebook/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*Book.txt
|
@ -1,31 +1,118 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace GradeBook
|
namespace GradeBook
|
||||||
{
|
{
|
||||||
public 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;
|
private List<double> grades;
|
||||||
public string Name;
|
|
||||||
|
|
||||||
public Book(string name)
|
readonly string category;
|
||||||
|
|
||||||
|
private string name;
|
||||||
|
|
||||||
|
public InMemoryBook(string name) : base(name)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
grades = new List<double>();
|
grades = new List<double>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddGrade(double grade)
|
public override 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 AddLetterGrade(char letter)
|
public void AddGrade(char letter)
|
||||||
{
|
{
|
||||||
switch (letter)
|
switch (letter)
|
||||||
{
|
{
|
||||||
@ -44,39 +131,15 @@ namespace GradeBook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Statistics GetStatistics()
|
public override event GradeAddedDelegate GradeAdded;
|
||||||
|
|
||||||
|
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.lowGrade = Math.Min(grade, result.lowGrade);
|
result.Add(grade);
|
||||||
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,14 +8,23 @@ namespace GradeBook
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
// var book = new Book("Scott's Grade Book");
|
var book = new DiskBook("Jason's Grade Book");
|
||||||
// book.AddGrade(89.1);
|
book.GradeAdded += OnGradeAdded;
|
||||||
// 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: ");
|
||||||
@ -24,12 +33,14 @@ 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)
|
||||||
{
|
{
|
||||||
@ -47,11 +58,13 @@ namespace GradeBook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var stats = book.GetStatistics();
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine($"The lowest grade is {stats.lowGrade}");
|
static void OnGradeAdded(object sender, EventArgs e)
|
||||||
Console.WriteLine($"The highesst grade is {stats.highGrade}");
|
{
|
||||||
Console.WriteLine($"The average grade is {stats.Average:N1}");
|
// Main is static, so it can only call static methods, hence return type is static
|
||||||
|
Console.WriteLine("A line has added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,54 @@
|
|||||||
|
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 Book("");
|
var book = new InMemoryBook("");
|
||||||
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,8 +4,36 @@ 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()
|
||||||
{
|
{
|
||||||
@ -49,9 +77,9 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "New Name");
|
Assert.Equal(book1.Name, "New Name");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetBookSetName(ref Book book, string name)
|
private void GetBookSetName(ref InMemoryBook book, string name)
|
||||||
{
|
{
|
||||||
book = new Book(name);
|
book = new InMemoryBook(name);
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,9 +92,9 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "Book1");
|
Assert.Equal(book1.Name, "Book1");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetBookSetName(Book book, string name)
|
private void GetBookSetName(InMemoryBook book, string name)
|
||||||
{
|
{
|
||||||
book = new Book(name);
|
book = new InMemoryBook(name);
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +107,7 @@ namespace GradeBook.Tests
|
|||||||
Assert.Equal(book1.Name, "New Name");
|
Assert.Equal(book1.Name, "New Name");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetName(Book book, string name)
|
private void SetName(InMemoryBook book, string name)
|
||||||
{
|
{
|
||||||
book.Name = name;
|
book.Name = name;
|
||||||
}
|
}
|
||||||
@ -105,9 +133,9 @@ namespace GradeBook.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Book GetBook(string name)
|
private InMemoryBook GetBook(string name)
|
||||||
{
|
{
|
||||||
return new Book(name);
|
return new InMemoryBook(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user