Skip to content

OOP Practica - Company - Complete (full course on one page)

Generated on: 2026-03-25


1.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
    

2 Encapsulation and constructor chaining

  1. If needed, retrieve the OOP Company project up to chapter 1 from DLO. It contains the Program and Person classes as you left them at the end of chapter 1.

  2. Change the Person class according to the class diagram below:

    Mermaid diagram Mermaid diagram

    a) Add the constant BonusThreshold.
    b) Make the four non-static attributes private.
    c) Add properties for these four attributes (properties are not shown in the class diagram).
    d) Apply constructor chaining to the three constructors.
    e) Add the HasRightToBonus() method. It returns true if the monthly salary is greater than or equal to BonusThreshold.

  3. Fix any errors that have arisen in your Main method (access the attributes via their properties).

  4. Remove the last two lines of code (both starting with Console.WriteLine) and add code so that your output looks like this:

    1
    2
    3
    4
    5
    6
    7
    8
    0
    1
    1
    2
    2
    4
    Mark earns 10000,00 and has right to a bonus.
    Caroline earns 4000,00 and has no right to a bonus.
    

3 Departments and association

  1. If needed, retrieve the OOP Company project up to chapter 2 from DLO. It contains the Program and Person classes as you left them at the end of chapter 2.

  2. In this assignment you will extend the company according to the following class diagram. A department can contain 0 or more persons. A person belongs to 1 department. A person knows which department they are in; a department does not know about its persons.

    Mermaid diagram Mermaid diagram

  3. Add a Department class to the Models folder.

    a) The default values for name and location are both "Unknown";
    b) Apply constructor chaining.
    c) Create properties for the two attributes.

  4. Modify the Person class:

    a) Add the department attribute of type Department.
    b) Adjust the relevant constructors. Use the default values of the Department class.
    c) Create a property for the department attribute.

  5. In your Main method:

    a) Remove or comment out any existing code.
    b) Create an array named departments with 4 departments:
    i) The department Operations in Hilversum;
    ii) The department Support in Amsterdam;
    iii) The department Management in Almere;
    iv) The department Documentation in Gouda.
    c) Create a person named boss (name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array you just created for the department.
    d) Create a person named employee (name: Caroline, city: Delft, monthly salary: 4000, department: Support);
    e) Create a person named assistant (name: Klaas, other values are defaults);
    f) Add code so that your output looks like this (the italic values in the output should come from the relevant objects):

    1
    2
    3
    4
    The number of people in the company is 3
    Mark works in Almere and lives in Den Haag
    Caroline works in department Support and earns 4000,00
    Klaas works in department Unknown and lives in Unknown
    

4 Inheritance with Employee and Freelancer

  1. If needed, retrieve the OOP Company project up to chapter 3 from DLO. It contains the Program, Person, and Department classes as you left them at the end of chapter 3.

  2. The company recognises two types of persons: employees and freelancers. In this exercise these become subclasses of the Person class. The Person class must also be adapted.

    Mermaid diagram Mermaid diagram

  3. Add the ToString() method to the Department class.

    1. The desired result is "department name at location" with the attribute values in place of the italics.
  4. Modify the Person class according to the class diagram:

    1. Pay attention to visibility modifiers.
    2. Use a constant for the default value of the name attribute.
    3. Adjust the constructors and keep applying constructor chaining.
    4. The CalculateYearlyIncome() method always returns 0.
    5. Remove properties of removed attributes.
    6. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location".
      2. Use the ToString() method of the Department class!
  5. Create the Employee subclass according to the class diagram:

    1. Create constructors and apply constructor chaining. Use the known default values.
    2. Use the constructors of the Person class.
    3. Create the properties.
    4. Yearly income is 12 times monthly salary plus any bonus. The bonus equals monthly salary.
    5. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location and is an employee with/without right to a bonus".
      2. Use the ToString() method of the Person class!
      3. The "with/without" text is determined by the HasRightToBonus() method.
  6. Create the Freelancer subclass according to the class diagram:

    1. Use the constructors of the Person class. The default value for hoursWorked is 0.
    2. Create the properties.
    3. Yearly income is hourly rate times hours worked.
    4. Hours worked is increased when the freelancer is hired via the Hire() method.
    5. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location and is a freelancer with an hourly rate of hourlyRate".
      2. Use the ToString() method of the Person class!
  7. Adjust your Main method as follows:

    1. Create an array named departments with 4 departments:
      1. The department Operations in Hilversum;
      2. The department Support in Amsterdam;
      3. The department Management in Almere;
      4. The department Documentation in Gouda.
    2. Create an employee named boss (name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array for the department.
    3. Create an employee named employee (name: Caroline, city: Delft, monthly salary: 4000, department: Support);
    4. Create a freelancer named assistant (name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation);
    5. Hire Klaas for 160 hours.
    6. Add code so that your output looks like this. Use the ToString() method (implicitly):
    1
    2
    3
    4
    The number of people in the company is 3
    Mark lives in Den Haag and works at department Management at Almere and is an employee with right to a bonus
    Caroline lives in Delft and works at department Support at Amsterdam and is an employee without right to a bonus
    Klaas lives in Diemen and works at department Documentation at Gouda and is a freelancer with an hourly rate of 50.0
    
    1. Add code so that the following appears below the output above:
    1
    2
    3
    Mark earns 130000,00 per year
    Caroline earns 48000,00 per year
    Klaas earns 8000,00 per year
    

5 Abstract Person and polymorphism

  1. If needed, retrieve the OOP Company project up to chapter 4 from DLO. It contains the classes as you left them at the end of chapter 4.

  2. Make the Person class and the CalculateYearlyIncome() method abstract according to the class diagram.

    Mermaid diagram Mermaid diagram

  3. Adjust your Main method as follows:

    1. Create an array named departments with four departments:
      1. The department Operations in Hilversum;
      2. The department Support in Amsterdam;
      3. The department Management in Almere;
      4. The department Documentation in Gouda.
    2. Create four persons:
      1. An employee named boss (name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array you just created.
      2. An employee named employee (name: Caroline, city: Delft, monthly salary: 4000, department: Support);
      3. A freelancer named assistant (name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation);
      4. A freelancer named projectLeader (name: Ronald, city: Zaandam, hourly rate: 80,00, department: Operations);
    3. Hire Klaas for 160 hours.
    4. Hire Ronald for 320 hours.
    5. Create an array of type Person with four objects.
    6. Add the employees boss and employee and the freelancers assistant and projectLeader to the array.
    7. Add a method below your Main method with the signature ShowYearlyIncome(Person person) that displays the text "Name earns yearlyIncome per year" on the screen.
    8. Call this method for each of the four items in your array using a for-loop, so that the output looks like:
    1
    2
    3
    4
    Mark earns 130000,00 per year
    Caroline earns 48000,00 per year
    Klaas earns 8000,00 per year
    Ronald earns 25600,00 per year
    

6 Lists and type checking

  1. If needed, retrieve the OOP Company project up to chapter 5 from DLO. It contains the classes as you left them at the end of chapter 5.

  2. Keep the array with four departments as in your Main method.

  3. Create a List<Person> named people.

  4. Fill the list as follows:

    1
    2
    3
    4
    5
    6
    7
    people.Add(new Employee("Mark", "Den Haag", departments[2], 10000));
    people.Add(new Employee("Angelique", "Rotterdam", departments[2], 5000));
    people.Add(new Employee("Caroline", "Delft", departments[1], 4000));
    people.Add(new Freelancer("Klaas", "Diemen", departments[3], 50.00));
    people.Add(new Freelancer("Ronald", "Zaandam", departments[0], 80.00));
    people.Add(new Freelancer("Jannie", "Utrecht", departments[0], 60.00));
    people.Add(new Freelancer("Anne", "Zwolle", departments[0], 40.00));
    
  5. Use a for-loop with is and casting to hire all freelancers in the list for 320 hours.

  6. Use a for-loop and the existing ShowYearlyIncome() method to get the following output:

    1
    2
    3
    4
    5
    6
    7
    Mark earns 130000,00 per year
    Angelique earns 65000,00 per year
    Caroline earns 48000,00 per year
    Klaas earns 16000,00 per year
    Ronald earns 25600,00 per year
    Jannie earns 19200,00 per year
    Anne earns 12800,00 per year
    

7 Interfaces and volunteers

In this assignment you will modify the class diagram as follows:

  • A Volunteer class is added. A volunteer can also be hired, but at zero rate.
  • The Hire(int hours) method is only needed for Volunteer and Freelancer and is placed in an interface.
  • Persons are sorted using the IComparable interface.

    Mermaid diagram Mermaid diagram

  • If needed, retrieve the OOP Company project up to chapter 6 from DLO. It contains the classes as you left them at the end of chapter 6.

  • For the Person class:

    a) Add the IComparable<Person> interface (already done in 5 Abstract Person and polymorphism).
    b) The CompareTo() method sorts by the name of a person (already done).

  • Create an interface named IHireable in the Models folder with the method Hire(int hours). The code for the interface is:

    1
    2
    3
    4
    5
    6
    7
    namespace Company.Models
    {
        public interface IHireable
        {
            void Hire(int hours);
        }
    }
    
  • Add the interface to the Freelancer class and override the Hire() method.

  • Add the Volunteer class to the Models folder according to the class diagram:

    a) The Hire() method does the same as in the Freelancer class.
    b) The CalculateYearlyIncome() method always returns 0.
    c) The ToString() method returns "name lives in city and works at department name at location and is a volunteer".

  • In the Main method of the launcher:

    a) Use the existing code and add the following four volunteers to the list:

    1
    2
    3
    4
    people.Add(new Volunteer("Ambi", "Amsterdam", departments[0]));
    people.Add(new Volunteer("Naledi", "Gaborone", departments[1]));
    people.Add(new Volunteer("Ceren", "Istanbul", departments[2]));
    people.Add(new Volunteer("Haining", "Shaoxing", departments[3]));
    

    b) Hire all volunteers for 160 hours.
    c) Sort the list alphabetically.
    d) Add code to get the following output (alphabetically sorted by name):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    Ambi lives in Amsterdam and works at department Operations at Hilversum and is a volunteer
    Ambi earns € 0,00 per year
    Angelique lives in Rotterdam and works at department Management at Almere and is an employee with right to a bonus
    Angelique earns € 65.000,00 per year
    Anne lives in Zwolle and works at department Operations at Hilversum and is a freelancer with an hourly rate of 40
    Anne earns € 12.800,00 per year
    Caroline lives in Delft and works at department Support at Amsterdam and is an employee without right to a bonus
    Caroline earns € 48.000,00 per year
    Ceren lives in Istanbul and works at department Management at Almere and is a volunteer
    Ceren earns € 0,00 per year
    Haining lives in Shaoxing and works at department Documentation at Gouda and is a volunteer
    Haining earns € 0,00 per year
    Jannie lives in Utrecht and works at department Operations at Hilversum and is a freelancer with an hourly rate of 60
    Jannie earns € 19.200,00 per year
    Klaas lives in Diemen and works at department Documentation at Gouda and is a freelancer with an hourly rate of 50
    Klaas earns € 16.000,00 per year
    Mark lives in Den Haag and works at department Management at Almere and is an employee with right to a bonus
    Mark earns € 130.000,00 per year
    Naledi lives in Gaborone and works at department Support at Amsterdam and is a volunteer
    Naledi earns € 0,00 per year
    Ronald lives in Zaandam and works at department Operations at Hilversum and is a freelancer with an hourly rate of 80
    Ronald earns € 25.600,00 per year
    

8 Exceptions and validation

  1. If needed, retrieve the OOP Company project up to chapter 7 from DLO. It contains the classes as you left them at the end of chapter 7.

  2. Modify the MonthlySalary property setter in the Employee class so that an ArgumentException is thrown if the monthly salary is less than or equal to 0. The message is: "The monthly salary cannot be negative".

  3. Adjust the constructors so they call the MonthlySalary property setter.

  4. Remove or comment out all code in your Main method.

  5. Add code that asks the user for data of a new employee as in the example below. Use the existing constructors and ToString() method. Your output should look like:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    Enter the name: Antoinette
    Enter the city: Leeuwarden
    Enter the department name: IT
    Enter the department location: Groningen
    Enter the monthly salary: -2300
    The monthly salary cannot be negative
    Your input has been processed.
    Enter the monthly salary: 2300
    Antoinette lives in Leeuwarden and works at department IT at Groningen and is an employee without right to a bonus
    Your input has been processed.
    

9 File IO and grouping

  1. If needed, retrieve the OOP Company project up to chapter 8 from DLO. It contains the classes as you left them at the end of chapter 8.

  2. Create a Data folder in the same folder as your src folder.

  3. Download the files Departments.txt and People.csv from DLO and place them in the Data folder.

  4. Read the departments from the text file Departments.txt and add them to a list named departments. Note: the name and location of the department are on two different lines in the text file.

  5. Read the various persons from the file People.csv and add them to a list named people. A line consists of: person type, name, city, index of the departments list, monthly salary / hourly rate / 0.

  6. Sort the list of persons and print them.

  7. Add code to create an output file PeoplePerDepartment.txt that looks like this (grouped by department):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Department: Operations
    -- Ambi lives in Amsterdam and works at department Operations at Hilversum and is a volunteer
    -- Anne lives in Zwolle and works at department Operations at Hilversum and is a freelancer with an hourly rate of 40.0
    -- Jannie lives in Utrecht and works at department Operations at Hilversum and is a freelancer with an hourly rate of 60.0
    -- Ronald lives in Zaandam and works at department Operations at Hilversum and is a freelancer with an hourly rate of 80.0
    
    Department: Support
    -- Caroline lives in Delft and works at department Support at Amsterdam and is an employee without right to a bonus
    -- Naledi lives in Gaborone and works at department Support at Amsterdam and is a volunteer
    
    Department: Management
    -- Angelique lives in Rotterdam and works at department Management at Almere and is an employee with right to a bonus
    -- Ceren lives in Istanboel and works at department Management at Almere and is a volunteer
    -- Mark lives in Den Haag and works at department Management at Almere and is an employee with right to a bonus
    
    Department: Documentation
    -- Haining lives in Shaoxing and works at department Documentation at Gouda and is a volunteer
    -- Klaas lives in Diemen and works at department Documentation at Gouda and is a freelancer with an hourly rate of 50.0
    

10 Database access and DepartmentDAO

  1. If needed, retrieve the OOP Company project up to chapter 9 from DLO. It contains the classes as you left them at the end of chapter 9.

  2. A file CreateInsertScriptCompany.sql is available on DLO. Download it and run it in MySQL Workbench. This creates a database with departments, persons and employees. A user with a password is also created.

  3. Add the MySQL connector to your project (e.g. MySql.Data via NuGet):
    a) In Visual Studio: right-click the project → Manage NuGet Packages → search for MySql.Data.
    b) Install the package.

  4. Create a new Database folder in your project.

  5. A generic DatabaseAccess class is available on DLO. Download it and place it in the folder you just created. It will then be visible in your project.

  6. Create a new DepartmentDAO class with the method SaveDepartment(Department department), which saves a (new) department to the database.

  7. Test your code by creating a department named HR in Hilversum in the database from your launcher.
    a) Do not forget to set up database access first with the correct database, user and password. These can be found in the SQL script.
    b) Do not forget to create an instance of the DepartmentDAO class.

  8. Create a new method GetDepartmentsByLocation(string location) that returns a list of departments for the given location.

  9. Test your code by displaying all departments that are in Hilversum. Your output should look like:

    1
    2
    department HR at Hilversum
    department Operations at Hilversum
    
  10. Finally, add a new employee to the database. This employee is named Lodewijk, lives in Zaandam, works in the Support department in Amsterdam and earns € 2500 per month.
    a) Because of subtyping you must update two tables (Person and Employee). You therefore need two DAOs.
    b) The primary key for the Person table is auto increment. You need the generated key to update the Employee table.

11 Generic DAOs and refactoring

  1. If needed, retrieve the OOP Company project up to chapter 10 from DLO. It contains the classes as you left them at the end of chapter 10.

  2. In this assignment you will refactor the DAOs so they use the generic AbstractDAO. Copy or download this file and add it to your project. Both DBaccess and AbstractDAO are now generic classes that you can use everywhere.

  3. Make DepartmentDAO, PersonDAO and EmployeeDAO subclasses of AbstractDAO.

  4. Rewrite the methods in the three specific DAOs so they use the AbstractDAO.

  5. Test your DAOs by running the chapter 10 code again. You must remove the HR row from the department table in Workbench, otherwise you will get an error. You will see that you can add the employee Lodewijk to your database twice without problems.