Skip to content

OOP Session Two Task #17

@Josephsam12

Description

@Josephsam12

using System;

namespace OOP_Task
{
// Part 1: Encapsulation
public class Employee
{
// Private fields
private int id;
private string name;
private double salary;

    // Properties
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public double Salary
    {
        get { return salary; }
        set
        {
            if (value >= 0)
                salary = value;
            else
                Console.WriteLine("Salary cannot be negative!");
        }
    }

    // Method
    public void DisplayInfo()
    {
        Console.WriteLine($"ID: {id}, Name: {name}, Salary: {salary}");
    }
}

// Part 2: Inheritance

// Manager Class
public class Manager : Employee
{
    private double bonus;

    public double Bonus
    {
        get { return bonus; }
        set
        {
            if (value >= 0)
                bonus = value;
            else
                Console.WriteLine("Bonus cannot be negative!");
        }
    }

    public double CalculateTotalSalary()
    {
        return Salary + bonus;
    }
}

// Developer Class
public class Developer : Employee
{
    private string programmingLanguage;

    public string ProgrammingLanguage
    {
        get { return programmingLanguage; }
        set { programmingLanguage = value; }
    }

    public void DisplayDeveloperInfo()
    {
        Console.WriteLine($"Language: {programmingLanguage}");
    }
}

// Part 3: Main Program
class Program
{
    static void Main(string[] args)
    {
        // Manager object
        Manager manager = new Manager();
        manager.Id = 1;
        manager.Name = "Ahmed";
        manager.Salary = 8000;
        manager.Bonus = 2000;

        Console.WriteLine("Manager:");
        manager.DisplayInfo();
        Console.WriteLine($"Bonus: {manager.Bonus}");
        Console.WriteLine($"Total Salary: {manager.CalculateTotalSalary()}");

        Console.WriteLine();

        // Developer object
        Developer developer = new Developer();
        developer.Id = 2;
        developer.Name = "Sara";
        developer.Salary = 6000;
        developer.ProgrammingLanguage = "C#";

        Console.WriteLine("Developer:");
        developer.DisplayInfo();
        developer.DisplayDeveloperInfo();
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions