Skip to content

1 Person class and constructors

  1. Start a new project in Visual Studio and name it Company.

  2. Create a Models folder.

  3. Create a class named Person inside it.

    The class diagram for the Person class looks like this:

    Mermaid diagram Mermaid diagram

    Note that the attribute NumberOfPeople is static. This attribute is used in this assignment to keep track of how many people have been added (explained below).

  4. Add the following line to the class:

    1
    public static int NumberOfPeople = 0;
    

    Remember that a static attribute exists in the class and is available outside instances. The static attribute NumberOfPeople is set to 0 when the Person class is loaded.

  5. Add the remaining attributes according to the class diagram.

  6. Add the all-args constructor as follows:

    1
    2
    3
    4
    5
    6
    7
    public Person(string name, string city, double monthlySalary)
    {
        this.Name = name;
        this.City = city;
        this.MonthlySalary = monthlySalary;
        this.EmployeeNumber = ++NumberOfPeople;
    }
    

    The EmployeeNumber attribute is assigned dynamically. First the static attribute NumberOfPeople is incremented by 1 (the plus signs precede the attribute), and the new value is then assigned to EmployeeNumber. The next steps will demonstrate this.

  7. Create a new Program class in the project root (or use the existing Program.cs).

  8. Add the following code to your Main method:

    1
    2
    3
    4
    5
    6
    7
    Console.WriteLine(Person.NumberOfPeople);
    Person boss = new Person("Mark", "Den Haag", 10000);
    Console.WriteLine(Person.NumberOfPeople);
    Console.WriteLine(boss.EmployeeNumber);
    Person employee = new Person("Caroline", "Delft", 4000);
    Console.WriteLine(Person.NumberOfPeople);
    Console.WriteLine(employee.EmployeeNumber);
    
    • Initially NumberOfPeople is 0.
    • When instantiating boss, NumberOfPeople is incremented by 1 (and thus equals 1).
    • This value is assigned to the EmployeeNumber attribute of boss.
    • When instantiating employee, NumberOfPeople is incremented by 1 (and thus equals 2).
    • This value is assigned to the EmployeeNumber attribute of employee.
  9. Run your program and check the output.

  10. Create the second constructor Person(string name).

    a) Use the default values "Unknown" for city and 0 for monthlySalary.
    b) Assign the EmployeeNumber attribute as done above.

  11. Create the default constructor Person().

    a) Use the default value "Unknown" for name and the default values for city and monthlySalary as described above.
    b) Assign the EmployeeNumber attribute as done above.

  12. Add the CalculateYearlyIncome method.

    a) The yearly income is 12 times the monthly salary.

  13. Add the following code to your Main method (below the existing code):

    1
    2
    3
    4
    5
    Person assistant = new Person("Klaas");
    Person manager = new Person();
    Console.WriteLine(Person.NumberOfPeople);
    Console.WriteLine($"{boss.Name} earns {boss.CalculateYearlyIncome():C} per year");
    Console.WriteLine($"{assistant.Name} lives in {assistant.City}");
    
  14. Run your program. The output should look like:

    1
    2
    3
    4
    5
    6
    7
    8
    0
    1
    1
    2
    2
    4
    Mark earns € 120.000,00 per year
    Klaas lives in Unknown