W8P Practice

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab 8- Personal Data > Inheritance

1-Create a class library project, PersonLib, containing following classes:


public class Person
{
public static string Separator { get; set; } = "/";
private static int count = 0;
public static Person? CreateInstance(string data)
{
string[] arr = data.Split(Separator);
if (arr.Length < 3) return null;
if (byte.TryParse(arr[2], out byte age) == false) return null;
Person newPer = new Person()
{
Name = arr[0].Trim(),
Gender = arr[1].Trim(),
Age = age,
};
return newPer;
}

public int No { get; init; } = 0;


public virtual string Name { get; set; } = default!;
public virtual string Gender { get; set; } = default!;
public virtual byte Age { get; set; } = default;
public virtual string Info => $"name:{Name}, gender:{Gender}, age:{Age}";
public Person(string name="", string gender="", byte age=0)
{
No = ++count;
Name = name;
Gender = gender;
Age = age;
}
}

public class PersonList


{
protected List<Person> _pers = new();
public virtual bool Add(Person rect)
{
if (_pers.Any(x => x.No == rect.No)) return false;
_pers.Add(rect);
return true;
}
public virtual Person? Modify(int targetNo, string name, string gender, byte age)
{
var found = _pers.FirstOrDefault(x => x.No == targetNo);
if (found != null)
{
found.Name = name;
found.Gender = gender;
found.Age = age;
}
return found;
}
public virtual Person? Remove(int targetNo)
{
var found = _pers.FirstOrDefault(x => x.No == targetNo);
if (found != null)
{
if (_pers.Remove(found))
return found;
else
return null;
}
return null;
}
public virtual bool Remove(Person per)
{
return _pers.Remove(per);
}
public virtual void Clear()
{
_pers.Clear();
}

public List<Person> People => _pers;


public byte MaxAge => _pers.Max(x=>x.Age);
public int Count => _pers.Count;
}

2- Build the project to get the output as an assembly, PersonLib.dll

3-Create another WinForms project with following:

A-Adding a reference to the assembly file PersonLib.dll (built in the step 2)

B-Adding, and designing forms: MainForm, CreateForm, EditForm with following UIs
C-Create a delegate type, PersonEventHandler
public delegate void PersonEventHandler(object? sender, Person? per);

D-Create a new class PersonListX, inheriting from the class PersonList (existing in the PersonLib library)
public class PersonListX: PersonList
{
public event PersonEventHandler? Added;
public event PersonEventHandler? Removed;
public event PersonEventHandler? Modified;
public event PersonEventHandler? Cleared;

public override bool Add(Person per)


{
var result = base.Add(per);
if (result == true) Added?.Invoke(this, per);
return result;
}

public override bool Remove(Person per)


{
var result= base.Remove(per);
if (result == true)
{
Added?.Invoke(this, per);
}
return result;
}
public override Person? Remove(int targetNo)
{
var result= base.Remove(targetNo);
if (result != null) Removed?.Invoke(this, result);
return result;
}
public override Person? Modify(int targetNo, string name, string gender, byte age)
{
var result= base.Modify(targetNo, name, gender, age);
if (result!=null) Modified?.Invoke(this, result);
return result;
}

public override void Clear()


{
base.Clear();
Cleared?.Invoke(this, null);
}
public Person? this[int key] => _pers.FirstOrDefault(x => x.No == key);

E-Define the data storage as an object of PersonListX in the class Program:


public static PersonListX PersonList = new();
F(optional)-The application might load data from a data file, people.txt, with this contents:
Men Sotha/Male/23
Keo Dara/Male/39
Kung Neary/Female/45
Choup Thida/Female/28
Hang Saray/Female/26
Cheat Rithy/Male/21
Prak Mom/Female/33

And a static method in the class Program to load data and create people into the data storage:

private static void LoadPeople()


{
var allLines = File.ReadAllLines("people.txt");
foreach(var line in allLines)
{
var newPerson = Person.CreateInstance(line);
if (newPerson != null)
{
PersonList.Add(newPerson);
}
}
}

G-Code in the MainForm to handle events:

+button Refresh’s Click

+button Delete’s Click

+button DeleteAll’s Click

+button New’s Click

+Button Edit’s Click

H-Code in the CreateForm to handle events:

+button Clear’s Click

+button Create’s Click

I-Code in the EditForm to handler events:

+button Cancel’s Click

+button Update’s Click

You might also like