Friday, May 9, 2014
Class with Date & Time (Lab)_Update
namespace Zau_Class_with_DateTime_Lab
{
public class Person
{
//private fields
private string _firstname;
private string _lastname;
private DateTime _dateofbirth;
//constructor
public Person (string firstname, string lastname, DateTime dateofbirth)
{
_firstname = firstname;
_lastname = lastname;
_dateofbirth = dateofbirth;
}
//properties
public string FirstName
{ get { return _firstname; } }
public string LastName
{ get { return _lastname; } }
public DateTime DateOfBirth
{ get { return _dateofbirth; } }
public int GetAge()
{
TimeSpan dt = DateTime.Now - DateOfBirth;
int years = dt.Days / 365;
return years;
}
public int GetDaysTillBirthDay()
{
DateTime today = DateTime.Today;
DateTime nextbirthday = new DateTime(today.Year, _dateofbirth.Month, _dateofbirth.Day);
if (nextbirthday < today)
nextbirthday = nextbirthday.AddYears(1);
int numDaysLeft = (nextbirthday - today).Days;
return numDaysLeft;
}
}
}
//============================================
namespace Zau_Class_with_DateTime_Lab
{
public partial class Form1 : Form
{
List<Person> personList = new List<Person>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Person
person = new Person("Zau", "Bawk", new DateTime(1984, 9, 23));
personList.Add(person);
person = new Person("David", "Lama", new DateTime(2011, 7, 2));
personList.Add(person);
person = new Person("Grace", "Lama", new DateTime(2003, 11, 25));
personList.Add(person);
person = new Person("Seng", "Lahtaw", new DateTime(1988, 09, 19));
personList.Add(person);
person = new Person("Obama", "Barack", new DateTime(1955, 8, 22));
personList.Add(person);
person = new Person("George", "Bush", new DateTime(2011, 3, 13));
personList.Add(person);
person = new Person("Bill", "Clinton", new DateTime(1930, 4, 25));
personList.Add(person);
}
private void btnDisplayNameList_Click(object sender, EventArgs e)
{
DisplayPerson();
}
private void DisplayPerson()
{
listView1.Items.Clear();
foreach (Person person in personList)
{
ListViewItem lvi = new ListViewItem(person.FirstName);
lvi.SubItems.Add(person.LastName);
lvi.SubItems.Add(person.DateOfBirth.ToShortDateString());
//add age to listview
lvi.SubItems.Add(person.GetAge().ToString());
//add days left to listview
lvi.SubItems.Add(person.GetDaysTillBirthDay().ToString());
listView1.Items.Add(lvi);
}
}
private void btnGetYoungestPerson_Click(object sender, EventArgs e)
{
//GetYoungestPerson();
Person person = GetYoungestPerson();
ListViewItem lvi = new ListViewItem(person.FirstName);
lvi.SubItems.Add(person.LastName);
lvi.SubItems.Add(person.DateOfBirth.ToShortDateString());
lvi.SubItems.Add(person.GetAge().ToString());
lvi.SubItems.Add(person.GetDaysTillBirthDay().ToString());
listView1.Items.Add(lvi);
}
private Person GetYoungestPerson()
{
listView1.Items.Clear();
Person youngest = personList[0];
foreach (Person person in personList)
{
if (youngest.DateOfBirth < person.DateOfBirth)
{
youngest = person;
}
}
return youngest;
}
private void btnGetOldestPerson_Click(object sender, EventArgs e)
{
Person person = GetOldestPerson();
ListViewItem lvi = new ListViewItem(person.FirstName);
lvi.SubItems.Add(person.LastName);
lvi.SubItems.Add(person.DateOfBirth.ToShortDateString());
lvi.SubItems.Add(person.GetAge().ToString());
lvi.SubItems.Add(person.GetDaysTillBirthDay().ToString());
listView1.Items.Add(lvi);
}
private Person GetOldestPerson()
{
listView1.Items.Clear();
Person oldest = personList[0];
foreach (Person person in personList)
{
if (oldest.DateOfBirth > person.DateOfBirth)
{
oldest = person;
}
}
return oldest;
}
}
}
//=================================
///Lab Assignment
///Create a class Person with _firstname,
///_lastname, _dateOfBirth (of type DateTime)
///Add constructor and properties
///
///in Form1: create a List of Person objects
///in Form1-load: add at least 10 people to the
///list
///Add a listview and a button
///Display the content of the list in a listview
///=================================================
///Lab Assignment: Add to the Person class
///Add method GetAge() to the Person class
///this method returns the numbers of years old
///
///Add method GetDaysTillBirthDay()
///this method should return the number of days
///left until the person's birthday
///
///in form1: re-adjust the listview by adding 2 more colums: "Age", and "Days" left to brithdate
///
///Display the List<Person> with the additional information.
///
///In form1 add a method GetYoungest() that returns
///a person (the youngest person)
///add method GetOldest() that returns the oldest person
///Add button to call these methods and display.
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment