0% found this document useful (0 votes)
43 views3 pages

Compositiontest: "Bob" "Jones" "Testing Class Employee"

This document contains code that demonstrates class composition in C#. It defines three classes - CompositionTest, Employee, and Date. CompositionTest creates an Employee object and displays its details. The Employee class contains first name, last name, birth date, and hire date properties. It constructs a Date object for the birth and hire dates. The Date class represents a date with day, month, and year properties, validating the values.

Uploaded by

Tram Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Compositiontest: "Bob" "Jones" "Testing Class Employee"

This document contains code that demonstrates class composition in C#. It defines three classes - CompositionTest, Employee, and Date. CompositionTest creates an Employee object and displays its details. The Employee class contains first name, last name, birth date, and hire date properties. It constructs a Date object for the birth and hire dates. The Date class represents a date with day, month, and year properties, validating the values.

Uploaded by

Tram Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//CompositionTest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Employee
{
public class CompositionTest
{
public void Main()
{
Employee e =
new Employee("Bob", "Jones", 2, 2, 1989, 3, 12, 2016);

MessageBox.Show(e.ToEmployeeString(), "Testing Class Employee");

}
}
}

//Employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Employee
{
public class Employee
{
public string firstName;
public string lastName;
public Date birthDate;
public Date hireDate;

public Employee(string first, string last, int birthDay, int birthMonth, int
birthYear, int hireDay, int hireMonth, int hireYear)
{
firstName = first;
lastName = last;
birthDate = new Date(birthMonth, birthDay, birthYear);
hireDate = new Date(hireMonth, hireDay, hireYear);

public string ToEmployeeString()


{
return lastName + " , " + firstName + " Hire: " + hireDate.ToDateString() + "
Birthday: " + birthDate.ToDateString();
}
}
}

//Date
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Employee
{
public class Date
{
private int day;
private int month;
private int year;

public Date(int theMonth, int theDay, int theYear)


{
if (theMonth > 0 && theMonth < 12)
month = theMonth;
else
{
month = 1;
Console.WriteLine("Thang {0} khong hop le. Tam thoi dua ve 1", theMonth);
}
year = theYear;
day = CheckDay(theDay);

public int CheckDay(int testDay)


{
int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year
% 100 != 0)))
return testDay;
Console.WriteLine("Ngay {0} khong hop le tam thoi dua ve 1", testDay);
return 1;

public string ToDateString()


{
return month + "/" + day + "/" + year;
}
}
}
//Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Employee
{
public partial class Form1 : Form
{
public Form1()
{
Employee e =
new Employee("Bob", "Jones", 2, 2, 1989, 3, 12, 2016);

MessageBox.Show(e.ToEmployeeString(), "Testing Class Employee");


}
}
}

You might also like