OOP Practica - Company - Complete (full course on one page)
Generated on: 2026-03-25
1.1 Person class and constructors
-
Start a new project in Visual Studio and name it
Company. -
Create a
Modelsfolder. -
Create a class named
Personinside it.The class diagram for the
Personclass looks like this:Note that the attribute
NumberOfPeopleis static. This attribute is used in this assignment to keep track of how many people have been added (explained below). -
Add the following line to the class:
1public static int NumberOfPeople = 0;Remember that a static attribute exists in the class and is available outside instances. The static attribute
NumberOfPeopleis set to 0 when thePersonclass is loaded. -
Add the remaining attributes according to the class diagram.
-
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
EmployeeNumberattribute is assigned dynamically. First the static attributeNumberOfPeopleis incremented by 1 (the plus signs precede the attribute), and the new value is then assigned toEmployeeNumber. The next steps will demonstrate this. -
Create a new
Programclass in the project root (or use the existingProgram.cs). -
Add the following code to your
Mainmethod: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
NumberOfPeopleis 0. - When instantiating
boss,NumberOfPeopleis incremented by 1 (and thus equals 1). - This value is assigned to the
EmployeeNumberattribute ofboss. - When instantiating
employee,NumberOfPeopleis incremented by 1 (and thus equals 2). - This value is assigned to the
EmployeeNumberattribute ofemployee.
- Initially
-
Run your program and check the output.
-
Create the second constructor
Person(string name).a) Use the default values "Unknown" for
cityand 0 formonthlySalary.
b) Assign theEmployeeNumberattribute as done above. -
Create the default constructor
Person().a) Use the default value "Unknown" for
nameand the default values forcityandmonthlySalaryas described above.
b) Assign theEmployeeNumberattribute as done above. -
Add the
CalculateYearlyIncomemethod.a) The yearly income is 12 times the monthly salary.
-
Add the following code to your
Mainmethod (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}"); -
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
-
If needed, retrieve the OOP Company project up to chapter 1 from DLO. It contains the
ProgramandPersonclasses as you left them at the end of chapter 1. -
Change the
Personclass according to the class diagram below: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 theHasRightToBonus()method. It returnstrueif the monthly salary is greater than or equal toBonusThreshold. -
Fix any errors that have arisen in your
Mainmethod (access the attributes via their properties). -
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
-
If needed, retrieve the OOP Company project up to chapter 2 from DLO. It contains the
ProgramandPersonclasses as you left them at the end of chapter 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.
-
Add a
Departmentclass to theModelsfolder.a) The default values for
nameandlocationare both "Unknown";
b) Apply constructor chaining.
c) Create properties for the two attributes. -
Modify the
Personclass:a) Add the
departmentattribute of typeDepartment.
b) Adjust the relevant constructors. Use the default values of theDepartmentclass.
c) Create a property for thedepartmentattribute. -
In your
Mainmethod:a) Remove or comment out any existing code.
b) Create an array nameddepartmentswith 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 namedboss(name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array you just created for the department.
d) Create a person namedemployee(name: Caroline, city: Delft, monthly salary: 4000, department: Support);
e) Create a person namedassistant(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
-
If needed, retrieve the OOP Company project up to chapter 3 from DLO. It contains the
Program,Person, andDepartmentclasses as you left them at the end of chapter 3. -
The company recognises two types of persons: employees and freelancers. In this exercise these become subclasses of the
Personclass. ThePersonclass must also be adapted. -
Add the
ToString()method to theDepartmentclass.- The desired result is "department name at location" with the attribute values in place of the italics.
-
Modify the
Personclass according to the class diagram:- Pay attention to visibility modifiers.
- Use a constant for the default value of the
nameattribute. - Adjust the constructors and keep applying constructor chaining.
- The
CalculateYearlyIncome()method always returns 0. - Remove properties of removed attributes.
- Add the
ToString()method.- The desired result is "name lives in city and works at department name at location".
- Use the
ToString()method of theDepartmentclass!
-
Create the
Employeesubclass according to the class diagram:- Create constructors and apply constructor chaining. Use the known default values.
- Use the constructors of the
Personclass. - Create the properties.
- Yearly income is 12 times monthly salary plus any bonus. The bonus equals monthly salary.
- Add the
ToString()method.- 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".
- Use the
ToString()method of thePersonclass! - The "with/without" text is determined by the
HasRightToBonus()method.
-
Create the
Freelancersubclass according to the class diagram:- Use the constructors of the
Personclass. The default value forhoursWorkedis 0. - Create the properties.
- Yearly income is hourly rate times hours worked.
- Hours worked is increased when the freelancer is hired via the
Hire()method. - Add the
ToString()method.- 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".
- Use the
ToString()method of thePersonclass!
- Use the constructors of the
-
Adjust your
Mainmethod as follows:- Create an array named
departmentswith 4 departments:- The department Operations in Hilversum;
- The department Support in Amsterdam;
- The department Management in Almere;
- The department Documentation in Gouda.
- Create an employee named
boss(name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array for the department. - Create an employee named
employee(name: Caroline, city: Delft, monthly salary: 4000, department: Support); - Create a freelancer named
assistant(name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation); - Hire Klaas for 160 hours.
- 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- 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 - Create an array named
5 Abstract Person and polymorphism
-
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.
-
Make the
Personclass and theCalculateYearlyIncome()method abstract according to the class diagram. -
Adjust your
Mainmethod as follows:- Create an array named
departmentswith four departments:- The department Operations in Hilversum;
- The department Support in Amsterdam;
- The department Management in Almere;
- The department Documentation in Gouda.
- Create four persons:
- An employee named
boss(name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array you just created. - An employee named
employee(name: Caroline, city: Delft, monthly salary: 4000, department: Support); - A freelancer named
assistant(name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation); - A freelancer named
projectLeader(name: Ronald, city: Zaandam, hourly rate: 80,00, department: Operations);
- An employee named
- Hire Klaas for 160 hours.
- Hire Ronald for 320 hours.
- Create an array of type
Personwith four objects. - Add the employees
bossandemployeeand the freelancersassistantandprojectLeaderto the array. - Add a method below your
Mainmethod with the signatureShowYearlyIncome(Person person)that displays the text "Name earns yearlyIncome per year" on the screen. - 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 - Create an array named
6 Lists and type checking
-
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.
-
Keep the array with four departments as in your
Mainmethod. -
Create a
List<Person>namedpeople. -
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)); -
Use a for-loop with
isand casting to hire all freelancers in the list for 320 hours. -
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
Volunteerclass is added. A volunteer can also be hired, but at zero rate. - The
Hire(int hours)method is only needed forVolunteerandFreelancerand is placed in an interface. -
Persons are sorted using the
IComparableinterface. -
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
Personclass:a) Add the
IComparable<Person>interface (already done in 5 Abstract Person and polymorphism).
b) TheCompareTo()method sorts by the name of a person (already done). -
Create an interface named
IHireablein theModelsfolder with the methodHire(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
Freelancerclass and override theHire()method. -
Add the
Volunteerclass to theModelsfolder according to the class diagram:a) The
Hire()method does the same as in theFreelancerclass.
b) TheCalculateYearlyIncome()method always returns 0.
c) TheToString()method returns "name lives in city and works at department name at location and is a volunteer". -
In the
Mainmethod 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
-
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.
-
Modify the
MonthlySalaryproperty setter in theEmployeeclass so that anArgumentExceptionis thrown if the monthly salary is less than or equal to 0. The message is: "The monthly salary cannot be negative". -
Adjust the constructors so they call the
MonthlySalaryproperty setter. -
Remove or comment out all code in your
Mainmethod. -
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
-
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.
-
Create a
Datafolder in the same folder as your src folder. -
Download the files
Departments.txtandPeople.csvfrom DLO and place them in theDatafolder. -
Read the departments from the text file
Departments.txtand add them to a list nameddepartments. Note: the name and location of the department are on two different lines in the text file. -
Read the various persons from the file
People.csvand add them to a list namedpeople. A line consists of: person type, name, city, index of the departments list, monthly salary / hourly rate / 0. -
Sort the list of persons and print them.
-
Add code to create an output file
PeoplePerDepartment.txtthat 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
-
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.
-
A file
CreateInsertScriptCompany.sqlis 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. -
Add the MySQL connector to your project (e.g.
MySql.Datavia NuGet):
a) In Visual Studio: right-click the project → Manage NuGet Packages → search for MySql.Data.
b) Install the package. -
Create a new
Databasefolder in your project. -
A generic
DatabaseAccessclass is available on DLO. Download it and place it in the folder you just created. It will then be visible in your project. -
Create a new
DepartmentDAOclass with the methodSaveDepartment(Department department), which saves a (new) department to the database. -
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 theDepartmentDAOclass. -
Create a new method
GetDepartmentsByLocation(string location)that returns a list of departments for the given location. -
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 -
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
-
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.
-
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. -
Make
DepartmentDAO,PersonDAOandEmployeeDAOsubclasses ofAbstractDAO. -
Rewrite the methods in the three specific DAOs so they use the
AbstractDAO. -
Test your DAOs by running the chapter 10 code again. You must remove the HR row from the
departmenttable in Workbench, otherwise you will get an error. You will see that you can add the employee Lodewijk to your database twice without problems.