Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit Ch3 HW 6 #799

Open
wants to merge 1 commit into
base: Chapter3/Homework/6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Src/BootCamp.Chapter/ContactsCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ public class ContactsCenter
public ContactsCenter(string peopleFile)
{
// load people
}
_people = Person.ConvertFromFile(peopleFile);
}

/// <summary>
/// Gets people by filter predicate.
/// </summary>
/// <returns></returns>
public List<Person> Filter(Predicate<Person> predicate)
{
var people = new List<Person>();
// ToDo: implement applying filter.
List<Person> people = new List<Person>();
//implement applying filter.
foreach (Person person in _people)
{
if (predicate(person))
{
people.Add(person);
}
}

return people;
}
}
Expand Down
30 changes: 30 additions & 0 deletions Src/BootCamp.Chapter/PeopleDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BootCamp.Chapter
{
public static class PeopleDemo
{
const string INPUT_FILE = @"Input/MOCK_DATA.csv";
public static event EventHandler OnStart;
public static event EventHandler OnStop;
public static event EventHandler OnPredicate;

public static void Run()
{
OnStart?.Invoke(null, new EventArgs());

ContactsCenter center = new ContactsCenter(INPUT_FILE);

PeoplePredicates.OnPredicate += OnPredicate;
Predicate<Person> predicate = (person) => PeoplePredicates.IsA(person);
predicate += (person) => PeoplePredicates.IsB(person);
predicate += (person) => PeoplePredicates.IsC(person);

center.Filter(predicate);

OnStop?.Invoke(null, new EventArgs());
}
}
}
26 changes: 17 additions & 9 deletions Src/BootCamp.Chapter/PeoplePredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ namespace BootCamp.Chapter
{
public static class PeoplePredicates
{
public static EventHandler OnPredicate;

/// <summary>
/// a) over 18, who do not live in UK, whose surename does not contain letter 'a'.
/// </summary>
/// <returns></returns>
public static bool IsA(Person person) => false;
public static bool IsA(Person person) => RunPredicateEvent() && person.IsOver18 && !person.IsLivingInUK && !person.HasAInLastName;

/// <summary>
/// b) under 18, who do not live in UK, whose surename does not contain letter 'a'.
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public static bool IsB(Person person) => false;
public static bool IsB(Person person) => RunPredicateEvent() && !person.IsOver18 && !person.IsLivingInUK && !person.HasAInLastName;

/// <summary>
/// c) who do not live in UK, whose surename and name does not contain letter 'a'.
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public static bool IsC(Person person) => false;
}
/// <summary>
/// c) who do not live in UK, whose surename and name does not contain letter 'a'.
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public static bool IsC(Person person) => RunPredicateEvent() && !person.IsLivingInUK && !person.HasAInLastName && !person.HasAInName;

private static bool RunPredicateEvent()
{
OnPredicate?.Invoke(null, new EventArgs());
return true;
}
}
}
73 changes: 68 additions & 5 deletions Src/BootCamp.Chapter/Person.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
namespace BootCamp.Chapter
using System.Collections.Generic;
using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;

namespace BootCamp.Chapter
{
public class Person
{
// add missing properties
}
public class Person
{
// add missing properties
public string Name { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public string Country { get; set; }
public string Email { get; set; }
public string StreetAddress { get; set; }

public bool IsOver18 => (DateTime.Now.Year - BirthDate.Year) > 18;
public bool IsLivingInUK => Country == "UK";
public bool HasAInName => Name.ToLower().Contains('a');
public bool HasAInLastName => LastName.ToLower().Contains('a');

public static List<Person> ConvertFromFile(string filename)
{
if (string.IsNullOrEmpty(filename) || !File.Exists(filename)) throw new FileNotFoundException();

//Read the file
List<string[]> peopleProperties = new List<string[]>();
using (TextFieldParser parser = new TextFieldParser(filename))
{
if (parser.EndOfData) throw new Exception("File is empty");
//Set delimiter
parser.Delimiters = new string[] { "," };
//Skip the first line as it's just property names
parser.ReadLine();
//Parse the remaining lines
while (!parser.EndOfData)
{
peopleProperties.Add(parser.ReadFields());
}
}

//Convert to Person objects
List<Person> people = new List<Person>();
foreach (string[] personProperties in peopleProperties)
{
if (personProperties.Length != 7) throw new Exception($"Line ({personProperties}) does not have all the properties needed to make a Person object.");
people.Add(new Person()
{
Name = personProperties[0],
LastName = personProperties[1],
BirthDate = DateTime.Parse(personProperties[2]),
Gender = Enum.Parse<Gender>(personProperties[3]),
Country = personProperties[4],
Email = personProperties[5],
StreetAddress = personProperties[6]
});
}

return people;
}
}

public enum Gender
{
Male,
Female
}
}
42 changes: 35 additions & 7 deletions Src/BootCamp.Chapter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
using System;
using System.Collections.Generic;

namespace BootCamp.Chapter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
class Program
{
public static event EventHandler OnAppClose;
static void Main(string[] args)
{
//Set events
PeopleDemo.OnStart += PeopleDemoStarted;
PeopleDemo.OnStop += PeopleDemoFinished;
PeopleDemo.OnPredicate += PeoplePredicateTriggered;
OnAppClose += AppClosed;

PeopleDemo.Run();

OnAppClose?.Invoke(null, new EventArgs());
Console.ReadLine();
}

public static void PeopleDemoStarted(object sender, EventArgs e)
{
Console.WriteLine("Demo Started.");
}
public static void PeopleDemoFinished(object sender, EventArgs e)
{
Console.WriteLine("Demo Finished.");
}
public static void PeoplePredicateTriggered(object sender, EventArgs e)
{
Console.WriteLine("People Predicate Triggered.");
}
public static void AppClosed(object sender, EventArgs e)
{
Console.WriteLine("Program Finished Running.");
}
}
}