using System.IO;
namespace HW10
{
struct Employee
{
public string LastName;
public string FirstName;
public string IDNumber;
public string Department;
public string Position;
public string PayType;
public double HoursWorked;
public double PayRate;
}
public partial class frmPayroll : Form
{
//Create a list to hold the data from the input file
private List<Employee> EmployeeList = new List<Employee>();
public frmPayroll()
{
InitializeComponent();
}
private void frmPayroll_Load(object sender, EventArgs e)
{
}
private void btnOpen_Click(object sender, EventArgs e)
{
//Declare local variables
DialogResult ButtonClicked;
string InputRecord;
string[] InputFields;
Employee worker;
//Show the OpenFileDialog box for user to select file
openFileDialog1.InitialDirectory = Environment.CurrentDirectory;
openFileDialog1.FileName = "Payroll.csv";
ButtonClicked = openFileDialog1.ShowDialog();
if(ButtonClicked == DialogResult.Cancel)
{
MessageBox.Show("You did NOT select a file.");
return;
}
//Declare the streamreader variable to access the file
StreamReader inputFile = File.OpenText(openFileDialog1.FileName);
//Set up a loop to read every record, skipping the first record
InputRecord = inputFile.ReadLine();
while(!inputFile.EndOfStream)
{
InputRecord = inputFile.ReadLine();
InputFields = InputRecord.Split(',');
worker.LastName = InputFields[0];
worker.FirstName = InputFields[1];
worker.IDNumber = InputFields[2];
worker.Department = InputFields[3];
worker.Position = InputFields[4];
worker.PayType = InputFields[5];
worker.HoursWorked = double.Parse(InputFields[6]);
worker.PayRate = double.Parse(InputFields[7]);
//Add this worker struc to the list declared above
EmployeeList.Add(worker);
//Display the name in the Listbox on the form
listPayroll.Items.Add(worker.LastName + "," + worker.FirstName);
}
//Close the input file
inputFile.Close();
btnOpen.Enabled = false;
}
private void btnCompute_Click(object sender, EventArgs e)
{
listPayroll.Items.Clear();
string DisplayHeader = String.Format("{0,-18}{1,14}{2,11}{3,13}",
"Employee", "HoursWorked", "PayRate", "Gross Pay");
listPayroll.Items.Add(DisplayHeader);
DisplayHeader = String.Format("{0,-18}{1,14}{2,11}{3,13}",
"Employee", "HoursWorked", "PayRate", "Gross Pay");
listPayroll.Items.Add(DisplayHeader);
foreach (Employee emp in EmployeeList)
{
double HoursWorked = 0.0;
double PayRate = 0.0;
double GrossPay = 0.0;
// Check for Salary or Hourly
if(emp.PayType == "Hourly")
{
if(emp.HoursWorked > 40.0)
{
PayRate = emp.PayRate * (emp.HoursWorked - 40.0);
HoursWorked = emp.PayRate * 40.0;
}
else
{
PayRate = 0.0;
HoursWorked = emp.PayRate * emp.HoursWorked;
}
}
else //Salary paytype. No ot. Just striaght pay for 40 hrs
{
PayRate = 0.0;
HoursWorked = emp.PayRate * 40.0;
}
GrossPay = HoursWorked + PayRate;
//Display this data in the listbox
string DisplayPayroll = String.Format("{0,-18}{1,14:c}{2,11:C{3,13:c}",
emp.FirstName + " " + emp.LastName,
HoursWorked, PayRate, GrossPay);
listPayroll.Items.Add(DisplayPayroll);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
// This method closes the application
this.Close();
У меня проблемы при попытке вычислить данные из файла .csv в список. Это вызывает необработанную ошибку исключения в блоке кода «Показать эти данные в списке». Я поместил системный ввод-вывод и все строки / двойной, которые мне нужно показать в списке. Я уверен, что это очень простое решение, но я новичок в кодировании. Любая помощь будет принята с благодарностью.