In: Computer Science
Can someone take a look and tell me what I have wrong here. VS 2019 .net framework console app c##
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace dropbox10
{
class MainClass
{
public static void Main(string[] args)
{
//create a list of employees
List<Employee> allEmployees = new List<Employee>();
//create two full time employees
FullTimeEmployee fe1 = new FullTimeEmployee("111", "Alice",
67888.00m);
FullTimeEmployee fe2 = new FullTimeEmployee("222", "Bob",
67555.00m);
//create two part time employees
PartTimeEmployee pe1 = new PartTimeEmployee("333", "Chuck", 22.12m,
20m);
PartTimeEmployee pe2 = new PartTimeEmployee("444", "Dan", 23.33m,
18.45m);
//add employees to list
allEmployees.Add(fe1);
allEmployees.Add(fe2);
allEmployees.Add(pe1);
allEmployees.Add(pe2);
//display data
foreach (Employee emp in allEmployees)
{
Console.WriteLine(emp);
}
Console.ReadKey();
}
}
}
//the required employee class
class Employee
{
//fields
private string employeeId;
private string employeeName;
//properties
public string EmployeeId
{
get { return employeeId; }
set { employeeId = value; }
}
public string EmployeeName
{
get { return employeeName; }
set { employeeName = value; }
}
public Employee(string employeeId, string employeeName)
{
this.employeeId = employeeId;
this.employeeName = employeeName;
}
//required toString method
public override string ToString()
{
string str;
str = string.Format("ID: {0} Name: {1}", EmployeeId,
EmployeeName);
return str;
}
}
//the required class
class FullTimeEmployee : Employee
{
//field
private decimal annualSalary;
//property
public decimal AnnualSalary
{
get { return annualSalary; }
set { annualSalary = value; }
}
//consturtor
public FullTimeEmployee(string employeeId, string employeeName,
decimal annualSalary)
: base(employeeId, employeeName)
{
this.annualSalary = annualSalary;
}
//GetweeklyPaid() method
public decimal GetWeeklyPaid()
{
decimal payAmount;
payAmount = AnnualSalary / 52;
return payAmount;
}
//ToString() method
public override string ToString()
{
string str;
str = base.ToString() + string.Format(" Pay amount: {0:C}",
GetWeeklyPaid());
return str;
}
class PartTimeEmployee : Employee
{
//fields
private decimal hourlyWage;
private decimal hoursWorked;
//properties
public decimal HourlyWage
{
get { return hourlyWage; }
set { hourlyWage = value; }
}
public decimal HoursWorked
{
get { return hoursWorked; }
set { hoursWorked = value; }
}
//constuctor
public PartTimeEmployee(string employeeId, string
employeeName,
decimal hourlyWage, decimal hoursWorke)
: base(employeeId, employeeName)
{
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
//GetWeeklyPaid() method
public decimal GetWeeklyPaid()
{
decimal payAmount;
payAmount = HoursWorked * HourlyWage;
return payAmount;
}
//ToString() method
public override string ToString()
{
string str;
str = base.ToString() + string.Format(" Pay amount: {0:C}",
GetWeeklyPaid());
return str;
}
}
}
Your have done mistake here
//constuctor
public PartTimeEmployee(string employeeId, string
employeeName,
decimal hourlyWage, decimal hoursWorke)
: base(employeeId, employeeName)
{
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
hoursWorke here you have done typo error ists hoursWorked
----------------------------------------------------------------------------------------------------------
Updated code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dropbox10
{
class MainClass
{
static void Main(string[] args)
{
//create a list of employees
List<Employee> allEmployees = new List<Employee>();
//create two full time employees
FullTimeEmployee fe1 = new FullTimeEmployee("111", "Alice",
67888.00m);
FullTimeEmployee fe2 = new FullTimeEmployee("222", "Bob",
67555.00m);
//create two part time employees
PartTimeEmployee pe1 = new PartTimeEmployee("333", "Chuck", 22.12m,
20m);
PartTimeEmployee pe2 = new PartTimeEmployee("444", "Dan", 23.33m,
18.45m);
//add employees to list
allEmployees.Add(fe1);
allEmployees.Add(fe2);
allEmployees.Add(pe1);
allEmployees.Add(pe2);
//display data
foreach (Employee emp in allEmployees)
{
Console.WriteLine(emp);
}
Console.ReadKey();
}
}
//the required employee class
class Employee
{
//fields
private string employeeId;
private string employeeName;
//properties
public string EmployeeId
{
get { return employeeId; }
set { employeeId = value; }
}
public string EmployeeName
{
get { return employeeName; }
set { employeeName = value; }
}
public Employee(string employeeId, string employeeName)
{
this.employeeId = employeeId;
this.employeeName = employeeName;
}
//required toString method
public override string ToString()
{
string str;
str = string.Format("ID: {0} Name: {1}", EmployeeId,
EmployeeName);
return str;
}
}
class FullTimeEmployee : Employee
{
//field
private decimal annualSalary;
//property
public decimal AnnualSalary
{
get { return annualSalary; }
set { annualSalary = value; }
}
//consturtor
public FullTimeEmployee(string employeeId, string employeeName,
decimal annualSalary)
: base(employeeId, employeeName)
{
this.annualSalary = annualSalary;
}
//GetweeklyPaid() method
public decimal GetWeeklyPaid()
{
decimal payAmount;
payAmount = AnnualSalary / 52;
return payAmount;
}
//ToString() method
public override string ToString()
{
string str;
str = base.ToString() + string.Format(" Pay amount: {0:C}",
GetWeeklyPaid());
return str;
}
}
class PartTimeEmployee : Employee
{
//fields
private decimal hourlyWage;
private decimal hoursWorked;
//properties
public decimal HourlyWage
{
get { return hourlyWage; }
set { hourlyWage = value; }
}
public decimal HoursWorked
{
get { return hoursWorked; }
set { hoursWorked = value; }
}
//constuctor
public PartTimeEmployee(string employeeId, string
employeeName,
decimal hourlyWage, decimal hoursWorked)
: base(employeeId, employeeName)
{
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
//GetWeeklyPaid() method
public decimal GetWeeklyPaid()
{
decimal payAmount;
payAmount = HoursWorked * HourlyWage;
return payAmount;
}
//ToString() method
public override string ToString()
{
string str;
str = base.ToString() + string.Format(" Pay amount: {0:C}",
GetWeeklyPaid());
return str;
}
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.