Question

In: Computer Science

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

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

Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived 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 also defines the CalcCurSalary() function that calculates the current year salary by using initial salary with 3% yearly increment. Create a Manager class that is derived from Worker class. In Manager class, it includes one field: yearPromo and constructor(s). Itincludes a CalcCurSalary function that calculate the current year salary by overriding the base class function using initial salary with 5% yearly increment plus 10% bonus. The manager’s salary calculates in two parts. It calculates as a worker before the year promoted and as a manager after the promotion. Write an application that reads the workers and managers information from files (“worker.txt” and “manager.txt”) and then creates the dynamic arrays of objects. Prompt the user for current year and display the workers’ and managers’ current information in separate groups: first and last name, ID, the year he/she has been working, and current salary.

Worker.txt

5
Hector
Alcoser
A001231
1999
24000
Anna
Alaniz
A001232
2001
34000
Lydia
Bean
A001233
2002
30000
Jorge
Botello
A001234
2005
40000
Pablo
Gonzalez
A001235
2007
35000

Manager.txt

3
Sam
Reza
M000411
1995
51000
2005
Jose
Perez
M000412
1998
55000
2002
Rachel
Pena
M000413
2000
48000
2010

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<Worker> employees = new List<Worker>();
using (StreamReader sr = new StreamReader(new FileStream("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<Manager> managers = new List<Manager>();
using (StreamReader sr = new StreamReader(new FileStream("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 of bounds of the array with the source being LINE 21 in the EmployeeMain Class.

Solutions

Expert Solution

There were numerous errors in your previous code, which are summarised below.

  1. The main error was in the code of reading the worker.txt and manager.txt file placed in the Main.cs file. Note that each data field of the employees are given on a new line in the text files. Hence, you need to read each line by line and parse them accordingly based on the data type. In previous code you were reading a single line and trying to split it into data values, which was wrong. Since each field is placed in a single line in the text file, hence you need to read the file line by line and not to do any splitting process.
  2. The wokID is a String field and not an integer field, hence, proper data type must be used in employee class for workID data member.

The corrected class files are given below within their respective tables.

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 string workId;

        public string WorkId
        {
            get { return workId; }
            set { workId = 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 = "";
            workId = "";
            yearStartedWked = 0;
            initSalary = 0;
        }

        public Employee(string fN, string lN, string id, int yearStarted, double salary)
        {
            firstName = fN;
            lastName = lN;
            workId = 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, string 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, string 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<Worker> employees = new List<Worker>();
            int numWorkers = 0;
            using (StreamReader sr = new StreamReader(new FileStream("worker.txt", FileMode.Open)))
            {
                
                string firstLine;
                firstLine = sr.ReadLine();
                int.TryParse(firstLine, out numWorkers);
                int i = 0;
                while (i < numWorkers)
                {
                    // read 5 lines at a time simultaneously and store in array of words as data for individual worker.
                    string[] words = { sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine() };
                    employees.Add(new Worker(words[0], words[1], words[2], Convert.ToInt32(words[3]), Convert.ToDouble(words[4])));
                    i++;
                }
            }
            List<Manager> managers = new List<Manager>();
            int numManagers = 0;
            using (StreamReader sr = new StreamReader(new FileStream("manager.txt", FileMode.Open)))
            {
                string firstLine;
                firstLine = sr.ReadLine();
                int.TryParse(firstLine, out numManagers);
                int i = 0;
                while (i < numManagers)
                {
                    // read 6 lines at a time simultaneously and store in array of words as data for individual manager.
                    string[] words = { sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine() };
                    managers.Add(new Manager(words[0], words[1], words[2], Convert.ToInt32(words[3]), Convert.ToDouble(words[4]), Convert.ToInt32(words[5])));
                    i++;
                }
            }
            Console.Write("Enter current year: ");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            for (int i = 0; i < numWorkers; i++)
            {
                employees[i].CalcYearWorked(year);
                employees[i].CalcCurSalary(year);
                Console.WriteLine("Worker: {0}, {1}'s salary is ${2:#.##}  and worked a total of {3} years",
                    employees[i].LastName, employees[i].FirstName,employees[i].CurSalary,employees[i].NYearWked);
            }
            Console.WriteLine();
            for (int i = 0; i < numManagers; i++)
            {
                managers[i].CalcYearWorked(year);
                managers[i].CalcCurSalary(year);
                Console.WriteLine("Manager: {0}, {1}'s salary is ${2:#.##}  and worked a total of {3} years",
                    managers[i].LastName, managers[i].FirstName, managers[i].CurSalary, managers[i].NYearWked);
            }
            Console.ReadKey();
        }
    }
}

Console Output

NOTE: Please put the worker.txt and manager.txt files in your projects > Bin > Debug folder. Please mention your doubts(if any) in the comments section and please give a thumbs up if you liked the solution.


Related Solutions

This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Projectile motion with air resistance code in c language
Projectile motion with air resistance code in c language
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1),...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1), 3 ) out2 <- rep( c(1,0,1), 3 ) counts <- c(18,17,15,20,10,20,25,13,12) This is a variation on the data in the first example on the “glm” help page in R. The counts variable is our response variable and will be modeled as a Poisson variable, the out1 predictor variable will measure the difference between outcomes 2 (baseline) and 3, and out2 will measure the difference...
Making a program in Python with the following code types. I'm very confused and would love...
Making a program in Python with the following code types. I'm very confused and would love the actual code that creates this prompt and a brief explanation. Thanks! A comment on the top line of your program containing your name. A comment on the second line containing your section number. A comment on the third line containing the date. A comment on the fourth line containing your email address. A comment with the lab number and purpose of this lab....
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only)....
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only). The function code should follow the conventions for MIPS function calls including passing parameters and returning results. Your function code must be written with the minimum number of machine instructions to be executed and without any use of MIPS pseudo-instructions. Myfunction(unsigned int a, unsigned int b, unsigned int c) { int i=0; while (a > c) { a /= b; i++; } return i;...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT