Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Daviddonadze/d6bdedb83958ffe5a1c95ffcf60a5162 to your computer and use it in GitHub Desktop.
Save Daviddonadze/d6bdedb83958ffe5a1c95ffcf60a5162 to your computer and use it in GitHub Desktop.
// Adult------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class Adult : Person
{
public string LicenseNumber { get; set; }
public string Job { get; set; }
public override string ToString()
{
//return string.Format("{0} - {1}, {2}, LicenseNumber = {3}", Name, Age, Job, LicenseNumber);
var famNm = $"{Name} - {Age},{Job}, LicenseNumber = {LicenseNumber}";
return famNm;
}
}
}
//Person------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
public int Age
{
get
{
return DateTime.Now.Year - DateOfBirth.Year;
}
}
}
}
//Family----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class Family
{
public int FamilyId { get; set; }
public string Nickname { get; set; }
public Adult Father { get; set; }
public Adult Mother { get; set; }
public List<Person> Children { get; set; }
public Person YoungestChild
{
get
{
Person Youngest = null;
int currMinAge = 999;
if (Children != null)
{
foreach (var person in Children)
{
if (person.Age < currMinAge)
{
Youngest = person;
currMinAge = person.Age;
}
}
}
return Youngest;
}
}
public bool IsNice
{
get
{ // Makes it Lower Case
var isParentsNice = Father.Name.StartsWith("o", StringComparison.CurrentCultureIgnoreCase)
|| Mother.Name.StartsWith("O", StringComparison.CurrentCultureIgnoreCase);
if (isParentsNice)
{
return true;
}
if (Children != null)
{
foreach (var kids in Children)
{
bool isNiceKid = kids.Name.StartsWith("o", StringComparison.CurrentCultureIgnoreCase);
if (isNiceKid) return true;
}
}
return false;
}
}
public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine(Nickname);
builder.AppendLine("Parents:");
builder.AppendLine(Father.ToString());
builder.AppendLine(Mother.ToString());
if (Children != null && Children.Count > 0)
{
builder.AppendLine("Kids:");
foreach (var kid in Children)
{
builder.AppendFormat("{0}-{1} \n", kid.Name, kid.Age);
}
}
return builder.ToString();
//return string.Format("{0}({1})", Nickname, FamilyId);
}
public Person OldestChild
{
get
{
Person Oldest = null;
int currMaxAge = 0;
if (Children != null)
{
foreach (var person in Children)
{
if (person.Age > currMaxAge)
{
Oldest = person;
currMaxAge = person.Age;
}
}
}
return Oldest;
}
}
}
}
//FamilyContext-----Used as DataBase------------------------
using Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Loops.Context
{
public class FamilyContext
{
List<Family> _families = new List<Family>();
public FamilyContext()
{
var family1 = new Family()
{
FamilyId = 1,
Nickname = "Family One ",
Father = new Adult() { Name = "Jim", DateOfBirth = DateTime.Now.AddYears(-34), Job = "Programmer", LicenseNumber = "2344454", PersonId = 5 },
Mother = new Adult() { Name = "Amy", DateOfBirth = DateTime.Now.AddYears(-33), Job = "Accountant", LicenseNumber = "8888", PersonId = 6 },
Children = new List<Person>()
{
new Person() {Name = "Bob",DateOfBirth = DateTime.Now.AddYears(-4),PersonId = 1},
new Person() {Name = "Ella",DateOfBirth = DateTime.Now.AddYears(-6),PersonId = 2},
new Person() {Name = "Sophia",DateOfBirth = DateTime.Now.AddYears(-3),PersonId = 3},
}
};
_families.Add(family1);
var family2 = new Family()
{
FamilyId = 2,
Nickname = "Family two ",
Father = new Adult() { Name = "Noah", DateOfBirth = DateTime.Now.AddYears(-28), Job = "Plumber", LicenseNumber = "12344454", PersonId = 15 },
Mother = new Adult() { Name = "Emma", DateOfBirth = DateTime.Now.AddYears(-25), Job = "Homemaker", LicenseNumber = "18888", PersonId = 16 },
Children = new List<Person>()
{
new Person() {Name = "Matthew",DateOfBirth = DateTime.Now.AddYears(-10),PersonId = 11},
new Person() {Name = "James",DateOfBirth = DateTime.Now.AddYears(-7),PersonId = 13},
}
};
_families.Add(family2);
var family3 = new Family()
{
FamilyId = 3,
Nickname = "Family three ",
Father = new Adult() { Name = "Bill", DateOfBirth = DateTime.Now.AddYears(-25), Job = "Driver", LicenseNumber = "12344454", PersonId = 35 },
Mother = new Adult() { Name = "Judy", DateOfBirth = DateTime.Now.AddYears(-23), Job = "Teacher", LicenseNumber = "18888", PersonId = 36 },
};
_families.Add(family3);
var family4 = new Family()
{
FamilyId = 4,
Nickname = "Family four",
Father = new Adult() { Name = "Orest", DateOfBirth = DateTime.Now.AddYears(-28), Job = "Architect ", LicenseNumber = "2344454", PersonId = 45 },
Mother = new Adult() { Name = "Lena", DateOfBirth = DateTime.Now.AddYears(-27), Job = "Accountant", LicenseNumber = "8888", PersonId = 46 },
Children = new List<Person>()
{
new Person() {Name = "Alexis",DateOfBirth =new DateTime(2008,10,21),PersonId = 41},
new Person() {Name = "Ella",DateOfBirth = new DateTime(2004,8,16),PersonId =42},
}
};
_families.Add(family4);
}
public List<Family> Families
{
get { return _families; }
}
}
}
// FamilyStatistics------Used as Business Logic----------------
using Loops.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class FamilyStatistics
{
private List<Family> _families = new List<Family>();
public FamilyStatistics()
{
var context = new FamilyContext();
_families = context.Families;
}
public List<Family> GetByParentName(string name)
{
var fams = new List<Family>();
foreach (var Family in _families)
{ //does not metter Later-Case it will be found by name
if (Family.Father.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase) // Makes it Lower Case
|| Family.Mother.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
{
fams.Add(Family);
}
}
return fams;
}
public List<Family> GetNiceFamilies()
{
var returnFamilies = new List<Family>();
foreach (var Family in _families)
{
if (Family.IsNice)
{
returnFamilies.Add(Family);
}
}
return returnFamilies;
}
public List<Family> GetFamilyWithNoKid()
{
var returnFamilies = new List<Family>();
foreach (var Family in _families)
{
if (Family.Children == null)
{
returnFamilies.Add(Family);
}
}
return returnFamilies;
}
public List<Family> GetFamilywithYongest()
{
var returnFamilies = new List<Family>();
int CurrMinAge = 999;
foreach (var Family in _families)
{
if (Family.Children != null)
{
if (Family.YoungestChild.Age < CurrMinAge)
{
CurrMinAge = Family.YoungestChild.Age;
}
}
}
foreach (var family in _families)
{
if (family.Children != null)
{
if (family.YoungestChild.Age == CurrMinAge)
{
{
returnFamilies.Add(family);
}
}
}
}
return returnFamilies;
}
public List<Family> GetFamilywithOldest()
{
var returnFamily = new List<Family>();
int CurrMaxAge = 0;
foreach (var Family in _families)
{
if (Family.Children != null)
{
if (Family.OldestChild.Age > CurrMaxAge)
{
CurrMaxAge = Family.OldestChild.Age;
}
}
foreach (var family in _families)
{
if (family.Children !=null)
{
if (family.OldestChild.Age == CurrMaxAge)
{
returnFamily.Add(family);
}
}
}
}
return returnFamily;
}
}
}
//Program------Calling Methods-------------------------------------------------------
using Loops.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
class Program
{
static void Main(string[] args)
{
var context = new FamilyContext();
var families = context.Families;
//PrintFamily(families); //call all the families
Console.WriteLine(families.Count);
FamilyStatistics stats = new FamilyStatistics();
//PrintFamily(stats.GetByParentName("emma")); // call family by children name
//PrintFamily(stats.GetNiceFamilies()); // Logic to choose Nice Families
//PrintFamily(stats.GetFamilyWithNoKid()); // Family With out Children
//PrintFamily(stats.GetFamilywithYongest()); // get Family with Youngest child
PrintFamily(stats.GetFamilywithOldest()); // Get family with Oldest child
Console.ReadLine();
}
private static bool PrintFamily(List<Family> families)
{
foreach (var family in families)
{
Console.WriteLine(family.ToString());
Console.WriteLine("--------------");
}
return true;
}
}
}
//Display info about families. >
//Family with most kids. >
//Family with no Kids. >
//Youngest Child. >
//Oldest Child. >
//Family father name. +
//Youngest Family +
//Family where Parent Name starts with "o" +
//find Awesome family (if any name starts with "o") +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment