Skip to content

Commit f503a37

Browse files
committed
2 parents b122b1b + c25ecb3 commit f503a37

File tree

8 files changed

+409
-0
lines changed

8 files changed

+409
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Classroom : IEnumerable
2+
{
3+
private List students = new List();
4+
5+
public void AddStudent(Student student)
6+
{
7+
students.Add(student);
8+
}
9+
10+
public void SortStudentsByAge()
11+
{
12+
students.Sort(); // Uses the IComparable implementation in Student
13+
}
14+
15+
public IEnumerator GetEnumerator()
16+
{
17+
return students.GetEnumerator();
18+
}
19+
20+
IEnumerator IEnumerable.GetEnumerator()
21+
{
22+
return GetEnumerator();
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public interface IPerson
2+
{
3+
string Name { get; set; }
4+
int Age { get; set; }
5+
void DisplayInfo();
6+
7+
// New property
8+
string Role { get; }
9+
10+
// Default method
11+
void Greet()
12+
{
13+
Console.WriteLine($"Hello, my name is {Name} and I am a {Role}.");
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class PersonUtilities
2+
{
3+
public static void PrintPersonDetails(IPerson person)
4+
{
5+
person.DisplayInfo();
6+
person.Greet();
7+
}
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Program
2+
{
3+
static void Main(string[] args)
4+
{
5+
// Create a teacher and students
6+
IPerson teacher = new Teacher { Name = "Helen Karu", Age = 35 };
7+
IPerson student1 = new Student { Name = "Eba Lencho", Age = 20 };
8+
IPerson student2 = new Student { Name = "Frederiek Eppink", Age = 22 };
9+
10+
// Use the utility class to print details
11+
PersonUtilities.PrintPersonDetails(teacher);
12+
PersonUtilities.PrintPersonDetails(student1);
13+
14+
// Create a classroom and add students
15+
Classroom classroom = new Classroom();
16+
classroom.AddStudent((Student)student1);
17+
classroom.AddStudent((Student)student2);
18+
19+
// Sort students by age
20+
classroom.SortStudentsByAge();
21+
22+
Console.WriteLine("\nSorted Students by Age:");
23+
foreach (var student in classroom)
24+
{
25+
student.DisplayInfo();
26+
}
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Student : IPerson, IComparable
2+
{
3+
public string Name { get; set; } = string.Empty;
4+
public int Age { get; set; } = 0;
5+
6+
public string Role => "Student";
7+
8+
public void DisplayInfo()
9+
{
10+
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
11+
}
12+
13+
public int CompareTo(Student? other)
14+
{
15+
if (other == null) return 1;
16+
return this.Age.CompareTo(other.Age);
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Teacher : IPerson
2+
{
3+
public string Name { get; set; } = string.Empty;
4+
public int Age { get; set; } = 0;
5+
6+
public string Role => "Teacher";
7+
8+
public void DisplayInfo()
9+
{
10+
Console.WriteLine($"Teacher Name: {Name}, Age: {Age}");
11+
}
12+
13+
public void Greet()
14+
{
15+
Console.WriteLine($"Hello, I am {Name}, a teacher with {Age} years of experience.");
16+
}
17+
}

0 commit comments

Comments
 (0)