Language is C# (i've got some code but it seems to not run correctly, would love...

60.1K

Verified Solution

Question

Programming

Language is C# (i've got some code but it seems to not runcorrectly, would love a new take)

Create an Employee class with five fields: first name, lastname, workID, yearStartedWked, and initSalary. It includesconstructor(s) and properties to initialize values for all fields.Create an interface, SalaryCalculate, class that includes twofunctions: first,CalcYearWorked() function, it takes one parameter(currentyear) and calculates the number of year the worker has beenworking. The second function, CalcCurSalary() function thatcalculates the current year salary. Create a Worker classes that isderived from Employee and SalaryCalculate class. In Worker class,it includes two field, nYearWked and curSalary, and constructor(s).It defines the CalcYearWorked() function using (current year –yearStartedWked) and save it in the nYearWked variable. It alsodefines the CalcCurSalary() function that calculates the currentyear salary by using initial salary with 3% yearly increment.Create a Manager class that is derived from Worker class. InManager class, it includes one field: yearPromo and constructor(s).Itincludes a CalcCurSalary function that calculate the current yearsalary by overriding the base class function using initial salarywith 5% yearly increment plus 10% bonus. The manager’s salarycalculates in two parts. It calculates as a worker before the yearpromoted and as a manager after the promotion. Write an applicationthat reads the workers and managers information from files(“worker.txt” and “manager.txt”) and then creates the dynamicarrays of objects. Prompt the user for current year and display theworkers’ and managers’ current information in separate groups:first and last name, ID, the year he/she has been working, andcurrent salary.

Worker.txt

5HectorAlcoserA001231199924000AnnaAlanizA001232200134000LydiaBeanA001233200230000JorgeBotelloA001234200540000PabloGonzalezA001235200735000

Manager.txt

3SamRezaM0004111995510002005JosePerezM0004121998550002002RachelPenaM0004132000480002010

What i Have:

/// Employee.cs ///

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

namespace MyNamespace
{
public class Employee
{
private string firstName;

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;

public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private int wordId;

public int WordId
{
get { return wordId; }
set { wordId = value; }
}
private int yearStartedWked;

public int YearStartedWked
{
get { return yearStartedWked; }
set { yearStartedWked = value; }
}
private double initSalary;

public double InitSalary
{
get { return initSalary; }
set { initSalary = value; }
}

public Employee()
{
firstName = \"\";
lastName = \"\";
wordId = 0;
yearStartedWked = 0;
initSalary = 0;
}

public Employee(string fN, string lN, int id, int yearStarted,double salary)
{
firstName = fN;
lastName = lN;
wordId = id;
yearStartedWked = yearStarted;
initSalary = salary;
}

}
}

/// SalaryCalculate.cs ///

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

namespace MyNamespace
{
public interface SalaryCalculate
{
void CalcYearWorked(int currentYear);
void CalcCurSalary(int currentYear);
}
}

/// Worker.cs ///

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

namespace MyNamespace
{
public class Worker : Employee, SalaryCalculate
{
private int nYearWked;

public int NYearWked
{
get { return nYearWked; }
set { nYearWked = value; }
}
private double curSalary;

public double CurSalary
{
get { return curSalary; }
set { curSalary = value; }
}

public Worker(string fN, string lN, int id, int yearStarted,double salary) : base(fN, lN, id, yearStarted, salary)
{
  
}

public void CalcYearWorked(int currentYear)
{
nYearWked = currentYear - YearStartedWked;
}

public void CalcCurSalary(int currentYear)
{
double salary = InitSalary;
for (int i = YearStartedWked; i <= currentYear; ++i)
{
salary *= 1.03;
}
CurSalary = salary;
}

}
}

/// Manager.cs ///

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

namespace MyNamespace
{
public class Manager : Worker
{
private int yearPromo;

public int YearPromo
{
get { return yearPromo; }
set { yearPromo = value; }
}

public Manager(string fN, string lN, int id, int yearStarted,double salary, int yearPromo)
: base(fN, lN, id, yearStarted, salary)
{
this.yearPromo = yearPromo;
}

public void CalcCurSalary(int currentYear)
{
double salary = InitSalary;
for (int i = YearStartedWked; i <= currentYear; ++i)
{
if (i < yearPromo)
salary *= 1.03;
else
salary *= 1.05;
}
CurSalary = salary * 1.2; // 20% bonus
}

}
}

/// Main.cs ///

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace MyNamespace
{
class EmployeeMain
{
public static void Main()
{
List employees = new List();
using (StreamReader sr = new StreamReader(newFileStream(\"worker.txt\", FileMode.Open)))
{
string line;



while((line = sr.ReadLine()) != null)
{
string[] words = line.Split(' ');
employees.Add(new Worker(words[0], words[1],Convert.ToInt32(words[2]), Convert.ToInt32(words[3]),Convert.ToDouble(words[4])));
}
}
List managers = new List();
using (StreamReader sr = new StreamReader(newFileStream(\"manager.txt\", FileMode.Open)))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] words = line.Split(' ');
managers.Add(new Manager(words[0], words[1],Convert.ToInt32(words[2]), Convert.ToInt32(words[3]),Convert.ToDouble(words[4]), Convert.ToInt32(words[5])));
}
}
Console.Write(\"Enter current year: \");
int year = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < employees.Count; ++i)
{
employees[i].CalcYearWorked(year);
employees[i].CalcCurSalary(year);
Console.WriteLine(\"Worker: \" + employees[i].LastName + \", \" +employees[i].FirstName + \"'s salary is: \" + employees[i].CurSalary+ \" and worked a total of \" + employees[i].NYearWked + \"years.\");
}
for (int i = 0; i < managers.Count; ++i)
{
managers[i].CalcYearWorked(year);
managers[i].CalcCurSalary(year);
Console.WriteLine(\"Manager: \" + managers[i].LastName + \", \" +managers[i].FirstName + \"'s salary is: \" + managers[i].CurSalary +\" and worked a total of \" + managers[i].NYearWked + \"years.\");
}
}
}
}

HELP:

The error that is constantly showing up is the index is out ofbounds of the array with the source being LINE 21 in theEmployeeMain Class.

Answer & Explanation Solved by verified expert
3.8 Ratings (682 Votes)
There were numerous errors in your previous code whichare summarised belowThe main error was in the code of reading the workertxt andmanagertxt file placed in the Maincs file Note that each datafield of the employees are given on a new line in the text filesHence you need to read each line by line and parse themaccordingly based on the data type In previous code you werereading a single line and trying to split it into data valueswhich was wrong Since each field is placed in a single line in thetext file hence you    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students