oop Concept in C#

Manimaddu
Posted by Manimaddu under C# category on | Points: 40 | Views : 708
Hi..
Here i will cover maximum oop concepts..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOPsOnHuman
{
class Person
{
protected string firstName;
protected string lastName;

public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}

public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}

public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
}
}
}

class Student : Person
{
private int grade;

public Student(string fName, string lName, int grade)
: base(fName, lName)
{
this.grade = grade;
}

public int Grade
{
get
{
return this.grade;
}
set
{
this.grade = value;
}
}

public override string ToString()
{
string result = string.Format("Student Name: {0} {1}", this.firstName, this.lastName) + Environment.NewLine;
result = result + string.Format(" Grade: {0}", this.grade);
return result;
}
}

class Worker : Person
{
private decimal weekSalary;
private decimal workHoursPerDay;

public Worker(string fName, string lName, decimal salary, decimal workHours)
: base(fName, lName)
{
this.weekSalary = salary;
this.workHoursPerDay = workHours;
}

public decimal WeekSalary
{
get
{
return this.weekSalary;
}
set
{
this.weekSalary = value;
}
}

public decimal WorkHoursPerDay
{
get
{
return this.workHoursPerDay;
}
set
{
this.workHoursPerDay = value;
}
}

public decimal MoneyPerHour()
{
decimal result = (weekSalary) / (workHoursPerDay * 5);
return result;
}

public override string ToString()
{
string result = string.Format("Student Name: {0} {1}", this.firstName, this.lastName) + Environment.NewLine;
result = result + string.Format(" Salary Per Hour: {0:F2}", this.MoneyPerHour());
return result;
}
}
class WorkerAndStudentTest
{

static void Main(string[] args)
{

int studentsCount = 10;
List<Student> studentGroup = new List<Student>(studentsCount);
Student firstStudent = new Student("Mani", "Kumar", 5);
studentGroup.Add(firstStudent);
Student secondStudent = new Student("Sathsh", "Kumar", 6);
studentGroup.Add(secondStudent);
Student thirdStudent = new Student("Banesh", "Chandra", 4);
studentGroup.Add(thirdStudent);
Student fourthStudent = new Student("Sandeep", "Krishna", 6);
studentGroup.Add(fourthStudent);
Student fifthStudent = new Student("Gopi", "Reddy", 3);
studentGroup.Add(fifthStudent);
Student sixthStudent = new Student("Adi", "Krishna", 6);
studentGroup.Add(sixthStudent);
Student seventhStudent = new Student("Bhanu", " Prakash", 2);
studentGroup.Add(seventhStudent);
Student eightStudent = new Student("Vamsi", "Manikanta", 6);
studentGroup.Add(eightStudent);
Student ninthStudent = new Student("Narendra", " Babu", 4);
studentGroup.Add(ninthStudent);
Student tenthStudent = new Student("Durga", "Prasad", 5);
studentGroup.Add(tenthStudent);
var orderedStudents = studentGroup.OrderBy(x => x.Grade);
Console.WriteLine("Ordered students:");
foreach (Student student in orderedStudents)
{
Console.WriteLine(student);
}
Console.WriteLine();
int workerCount = 10;
List<Worker> workerGroup = new List<Worker>(workerCount);
Worker firstWorker = new Worker("Ram", "Prasad", 500, 8);
workerGroup.Add(firstWorker);
Worker secondWorker = new Worker("mani", "kumar", 333, 6);
workerGroup.Add(secondWorker);
Worker thirdWorker = new Worker("Ram", "Mohan", 444, 7);
workerGroup.Add(thirdWorker);
Worker fourthWorker = new Worker("Satish", "Kumar", 600, 8);
workerGroup.Add(fourthWorker);
Worker fifthWorker = new Worker("Vamsi", " manikanta", 300, 4);
workerGroup.Add(fifthWorker);
Worker sixthWorker = new Worker("lakshmi", "Kumari", 200, 6);
workerGroup.Add(sixthWorker);
Worker seventhWorker = new Worker("Durga", " Prasad", 200, 8);
workerGroup.Add(seventhWorker);
Worker eightWorker = new Worker("Narendra", "Babu", 649, 6);
workerGroup.Add(eightWorker);
Worker ninthWorker = new Worker("Gopi", " Reddy", 110, 4);
workerGroup.Add(ninthWorker);
Worker tenthWorker = new Worker("Satya", "Prasad", 500, 8);
workerGroup.Add(tenthWorker);
var orderedWorkers = workerGroup.OrderByDescending(x => x.MoneyPerHour());
Console.WriteLine("Ordered workers:");
foreach (Worker worker in orderedWorkers)
{
Console.WriteLine(worker);
}
Console.Read();
}
}
}


Thank You..

Comments or Responses

Login to post response