0
\$\begingroup\$

Inheritance Challenge 2 - Employees, Bosses and Trainees

Create a main class with a Main Method, then a base class Employee with the properties Name, FirstName, Salary and the methods Work() and Pause().

Create a deriving class boss with the propertie CompanyCar and the method Lead(). Create another deriving class of employees - trainees with the properties WorkingHours and SchoolHourse and a method Learn().

Override the methods Work() of the trainee class so that it indicates the working hours of the trainee.

Don’t forget to create the constructors.

Create an object of each of the three classes (with arbitrary values) and call the methods, Lead() of Boss and Work() of Trainee.

Just print out the respective text, what the respective employees do.

I feel like my biggest improvement here can be with access modifiers. Here are the scripts.

Program.cs

using System;

namespace Employees__Bosses_and_Trainees
{
    class Program
    {
        static void Main()
        {
            Employee employee = new Employee("Warlock", "Adam", 10.0);
            Console.WriteLine("Printing Employee.");
            Console.WriteLine("Last name: {0}.", employee.Name);
            Console.WriteLine("First name: {0}.", employee.FirstName);
            Console.WriteLine("Salary: ${0}.", employee.Salary);
            employee.Work();
            employee.Pause();

            Console.WriteLine("");

            Employee frank = new Employee
            {
                FirstName = "Frank",
                Name = "Smith",
                Salary = 10.0
            };

            Console.WriteLine("Printing Employee.");
            Console.WriteLine("Last name: {0}.", frank.Name);
            Console.WriteLine("First name: {0}.", frank.FirstName);
            Console.WriteLine("Salary: ${0}.", frank.Salary);
            frank.Work();
            frank.Pause();

            Console.WriteLine("");

            Boss boss = new Boss("Boop", "Betty", 20.0);
            Console.WriteLine("Printing Boss.");
            Console.WriteLine("Does the Boss have a car? {0}", boss.CompanyCar);
            Console.WriteLine("First Name: {0}.", boss.FirstName);
            Console.WriteLine("Last name: {0}.", boss.Name);
            Console.WriteLine("Salary: ${0}.", boss.Salary);
            boss.Work();
            boss.Lead();
            boss.Pause();

            Console.WriteLine("");

            Boss james = new Boss();
            Console.WriteLine("Printing Boss.");
            Console.WriteLine("Does the Boss have a car? {0}", james.CompanyCar);
            james.FirstName = "James";
            james.Name = "Storm";
            james.Salary = 20.0;
            Console.WriteLine("First Name: {0}.", james.FirstName);
            Console.WriteLine("Last name: {0}.", james.Name);
            Console.WriteLine("Salary: ${0}.", james.Salary);
            james.Work();
            james.Lead();
            james.Pause();

            Console.WriteLine("");

            Trainees trainee = new Trainees("Murphy", "Charlie", 15.0);
            Console.WriteLine("Printing Trainees.");
            Console.WriteLine("First Name: {0}.", trainee.FirstName);
            Console.WriteLine("Last name: {0}.", trainee.Name);
            Console.WriteLine("Salary: {0}.", trainee.Salary);
            Console.WriteLine("School hours: {0}.", trainee.SchoolHours);
            Console.WriteLine("Training hours {0}.", trainee.WorkingHours);
            trainee.Learn();
            trainee.Pause();
            trainee.Work();

            Console.WriteLine("");

            Trainees nully = new Trainees();
            Console.WriteLine("Printing Trainees.");
            Console.WriteLine("First Name: {0}.", nully.FirstName);
            Console.WriteLine("Last name: {0}.", nully.Name);
            Console.WriteLine("Salary: {0}.", nully.Salary);
            Console.WriteLine("School hours: {0}.", nully.SchoolHours);
            Console.WriteLine("Training hours {0}.", nully.WorkingHours);
            nully.Learn();
            nully.Pause();
            nully.Work();
        }
    }
}

Boss.cs

using System;

namespace Employees__Bosses_and_Trainees
{
    internal class Boss : Employee
    {
        //  Properties
        internal bool CompanyCar { get; set; }

        //  Constructor
        internal Boss()
        {
        }

        internal Boss(string name, string firstName, double salary) : base(name, firstName, salary)
        {
            Name = name;
            FirstName = firstName;
            Salary = salary;
            CompanyCar = true;
        }

        //  Methods
        internal void Lead()
        {
            if (CompanyCar)
                Console.WriteLine("{0} {1} earns ${2} an hour wage and has a company car.", FirstName, Name, Salary);
            else
                Console.WriteLine("{0} {1} earns ${2} an hour wage without a company car.", FirstName, Name, Salary);
        }
    }
}

Employee.cs

using System;

namespace Employees__Bosses_and_Trainees
{
    internal class Employee
    {
        //  Properties
        internal string Name { get; set; }
        internal string FirstName { get; set; }
        internal double Salary { get; set; }

        // Constructors
        internal Employee()
        {
        }

        internal Employee(string name = null, string firstName = null, double salary = 0.0)
        {
            Name = name;
            FirstName = firstName;
            Salary = salary;
        }

        //  Methods
        public virtual void Work() => Console.WriteLine("{0} {1} earns ${2} an hour wage.", FirstName, Name, Salary);

        internal void Pause() => Console.WriteLine("{0} is on break at work.", Name);
    }
}

Trainees.cs

using System;

namespace Employees__Bosses_and_Trainees
{
    internal class Trainees : Employee
    {
        //  Properties
        internal double WorkingHours { get; set; }
        internal double SchoolHours { get; set; }

        //  Constructors
        internal Trainees()
        {
        }

        internal Trainees(string name, string firstName, double salary, double workingHours = 8.0, double schoolHours = 4.0) : base(name, firstName, salary)
        {
            Name = name;
            FirstName = firstName;
            Salary = salary;
            WorkingHours = workingHours;
            SchoolHours = schoolHours;
        }

        //  Methods
        internal void Learn() => Console.WriteLine("{0} {1} is going to school {2} hours a week.", FirstName, Name, SchoolHours);

        //  Override
        public override void Work() => Console.WriteLine("{0} {1} is working {2} a week as a trainer.", FirstName, Name, WorkingHours);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.