Compare commits

...

7 Commits

7 changed files with 158 additions and 65 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

View File

@ -1,30 +1,101 @@
using System;
using System.IO;
using System.Collections.Generic;
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;
readonly string category;
private string name;
public string Name
{
get;
set;
}
public Book(string name)
public InMemoryBook(string name) : base(name)
{
Name = name;
grades = new List<double>();
}
public void AddGrade(double grade)
public override void AddGrade(double grade)
{
if (grade <= 100 && grade >= 0)
{
@ -60,41 +131,15 @@ namespace GradeBook
}
}
public event GradeAddedDelegate GradeAdded;
public override event GradeAddedDelegate GradeAdded;
public Statistics GetStatistics()
public override 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.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;
result.Add(grade);
}
return result;

View File

@ -8,19 +8,23 @@ 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);
var book = new Book("Jason's Grade Book");
var book = new DiskBook("Jason's Grade Book");
book.GradeAdded += OnGradeAdded;
book.GradeAdded += OnGradeAdded;
book.GradeAdded -= OnGradeAdded;
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: ");
@ -29,12 +33,13 @@ namespace GradeBook
{
Console.WriteLine("Receiving termination signal; Termiate program");
break;
} else
}
else
{
try
{
Console.WriteLine($"Received: {input}");
book.AddGrade(Convert.ToDouble(input));
book.AddGrade(Convert.ToDouble(input));
// book.AddGrade('A');
}
catch (ArgumentException ex)
@ -53,11 +58,7 @@ namespace GradeBook
}
}
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}");
return input;
}
static void OnGradeAdded(object sender, EventArgs e)

View File

@ -1,10 +1,54 @@
using System;
namespace GradeBook
{
public class Statistics
{
public double Average;
public double Average
{
get
{
return Sum / Count;
}
}
public double highGrade;
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;
}
}
}

View File

@ -9,7 +9,7 @@ namespace GradeBook.Tests
public void BookCalculateAnAverageGrade()
{
// arrange
var book = new Book("");
var book = new InMemoryBook("");
book.AddGrade(89.1);
book.AddGrade(90.5);
book.AddGrade(77.3);

View File

@ -77,9 +77,9 @@ namespace GradeBook.Tests
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;
}
@ -92,9 +92,9 @@ namespace GradeBook.Tests
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;
}
@ -107,7 +107,7 @@ namespace GradeBook.Tests
Assert.Equal(book1.Name, "New Name");
}
private void SetName(Book book, string name)
private void SetName(InMemoryBook book, string name)
{
book.Name = name;
}
@ -133,9 +133,9 @@ namespace GradeBook.Tests
}
private Book GetBook(string name)
private InMemoryBook GetBook(string name)
{
return new Book(name);
return new InMemoryBook(name);
}
}
}