Object Oriented Programing (OOP) : Creating a Project and Task Class and its Members

By   Tewodros   Date Posted: Oct. 12, 2021  Hits: 980   Category:  ASP.NET C#   Total Comment: 0             A+ A-


side

We recommend the introductory articles on OOP before reading this article. 
 

  1. BASIC PROGRAMING TUTORIAL
  2. C# OBJECT ORIENTED PROGRAMING (OOP) : CREATING A PERSON CLASS AND ITS MEMBERS
  3. INHERITANCE AND POLYMORPHISM

In this OOP exercise we will create two classes Project and Task. A project can be any real world project like Construction, Business, Software, Humanitarian etc. A project has a list of tasks that needs to be done to complete the project. A project has a start date and end date as well as the name of the project. 

A given task has duration of the task, and which project it belongs to as well as the name of the given task.

As you guessed, there is a One-Many relationship between a task and a project. For this we have to use a collection data structure (List ) to create the relationship.

   private IList<Task> Tasks = new List<Task>();

IList collection class has built methods to add or remove items to the list. 

Tasks.Add(task);

Let's see everything put together now.

 

public class Project {

   public string ProjectName { get; set; }

   public DateTime StratDate { get; set; }

   public DateTime EndDate { get; set; }

 

   private IList<Task> Tasks = new List<Task>();

   public Project(string ProjectName, DateTime stratDate, DateTime endDate)   {

    this.ProjectName = ProjectName;

    this.StratDate = stratDate;

    this.EndDate = endDate;

   }  

   public void AddTask(Task task)  {

    Tasks.Add(task);

   }

   public void RemoveTask(Task task) {

    Tasks.Remove(task);

   }

   public double CalculateTotalCost() {

    double TotalCost = 0;

    foreach (Task t in Tasks)  {

     TotalCost += t.CalculateCost();

    }

    return TotalCost;

   }

}

 

Task can be defined as an abstract class as we can have other concreate class depending on which task we want to implement. So the abstract class delegate the job of calculating the cost to the sub classed which inherit from this class.

 

public abstract class Task {

   public string Name { get; set; }

   public int Duration { get; set; }

 

   private Project Project;    

 

   public Task(string name, int duration, Project project) {

    this.Name = name;

    this.Duration = duration;

    this.Project = project;

    project.AddTask(this);

   }

    public abstract double CalculateCost();

}

For example, Building the foundation is one task where as Painting Job is another task. Each task has a way of calculating its total cost which a different way from other subclasses of Task. For example for BuildingTask class it may be a matter of adding Labor and material cost but for others sub classes there may be other parameters we need to consider.

 

 

 

 

  public class BuildingTask : Task   {

       public double LaborCost { get; set; }

       public double MaterialCost { get; set; }

       public BuildingTask(string TaskName, int duration, Project project, double LaborCost, double MaterialCost) : 

       base(TaskName, duration, project)    {

         this.MaterialCost = MaterialCost;

         this.LaborCost = LaborCost;

       }

      public override double CalculateCost()   {    

           return LaborCost + MaterialCost;

    }

}

}

 

 

 

public class Tesing {

 static void Main(string[] args) {

    Project ConstructionProj = new Project("ABC Building", new DateTime(1/1/2000), new DateTime(10/10/2030));

    Task TFoundation =  new BuildingTask("ABC Building Foundation ", 10, ConstructionProj, 2000, 5000);

Console.WriteLine( TFoundation.Name  +  " will take " + TFoundation.Duration  +  " Month and costs " +               

      TFoundation.CalculateCost());

    Task TPaint = new BuildingTask("ABC Painting", 20, ConstructionProj, 5000, 5000);

    Console.WriteLine(TPaint.Name +   " will take " + TPaint.Duration + " Month " +  TPaint.CalculateCost());

    Console.WriteLine("Total Construction and Paint cost is for ABC Building:" +   ConstructionProj.CalculateTotalCost());

    Console.ReadLine();

   }

 

Here is the output we get from the above code. 

  1. ABC Building Foundation will take 10 Month and costs 7000
  2. ABC Painting will take 20 Month 10000
  3. Total Construction and Paint cost is for ABC Building:17000

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* nkjsuu

Back to Top