|
-
August 21st, 2010, 08:21 PM
#3
Re: array list
i have to use array list
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace EmployeePayrollClasses
{
public partial class GUI : Form
{
//private Employee[] employee = new Employee[3];
private double gross;
private int _i = 0;
public ArrayList empList = new ArrayList();
List<Employee> list = new List<Employee>();
Employee emp = new Employee( );
StreamReader employeeStreamReader;
public GUI()
{
InitializeComponent();
}
private void GUI_Load(object sender, EventArgs e)
{
openFile();
}
private void calcPay()//calculate method
{
double hours;//variables
double payrate;
double ot;
double otPay;
string th;
string tr;
th = txtHours.Text;
tr = txtRate.Text;
try//try catch for errors
{
txtHours.BackColor = (Color.White);
hours = Double.Parse(th);//get input
payrate = double.Parse(tr);
if (hours <= 40)//test for 40 and below hours
{
gross = hours * payrate;
txtGross.Text = gross.ToString("c2");
}
else
if (hours > 40)//test for overtime
{
ot = hours - 40;
otPay = ot * (payrate * 1.5);
gross = (hours * payrate) + otPay;
txtGross.Text = gross.ToString("c2");
}
}
catch (FormatException ex)
{
MessageBox.Show(" ENTER HOURS PLEASE");
txtHours.BackColor = (Color.Pink);
txtHours.Text = "";
}
}
private void displayRecord()//display the records
{
try
{
emp = /*(Employee)empList[_i]; */(empList[_i] as Employee);
txtFName.Text = emp.firstName;
txtLName.Text = emp.lastName;
txtRate.Text = emp.rate.ToString();
_i++;
}
catch (Exception ex)
{
txtFName.Text = string.Empty;
txtLName.Text = string.Empty;
txtRate.Text = string.Empty;
_i = 0;
MessageBox.Show("Pretty error message" + ex.ToString());
}
}
//try
//{
// foreach (Employee e in list)
// {
// Console.WriteLine(e.firstName.ToString());
// Console.WriteLine(e.lastName.ToString());
// }
// txtFName.Text = emp.firstName;
// txtLName.Text = emp.lastName;
// txtRate.Text = emp.rate.ToString();
// // _i++;
//}
//{
//if (_i < list.Count && _i == 0)//cycle through the elements
//{
// //txtFName.Text = emp.firstName;
// //txtLName.Text = emp.lastName;
// //txtRate.Text = emp.rate.ToString();
// txtFName.Text = list[_i].firstName;
// txtLName.Text = list[_i].lastName;
// txtRate.Text = list[_i].rate.ToString();
// _i++;
// }
//}
//else
//{
// _i = 0;//go back to element 0
// txtFName.Text = "";
// txtLName.Text = "";
// txtRate.Text = "";
//}
// }
//catch (Exception ex)
//{
// MessageBox.Show("hmm " + ex.ToString());
//}
//}
public void openFile()//open the files
{
DialogResult responseDialogResult;
openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
responseDialogResult = openFileDialog1.ShowDialog();
int x = 0;
try//place info into the structure
{
employeeStreamReader = File.OpenText(openFileDialog1.FileName);
while (employeeStreamReader.Peek() > -1)
{
//employee[x].firstName = employeeStreamReader.ReadLine();
//employee[x].lastName = employeeStreamReader.ReadLine();
//employee[x].rate = Convert.ToDouble(employeeStreamReader.ReadLine());
//x++;
emp.first = employeeStreamReader.ReadLine();
emp.last = employeeStreamReader.ReadLine();
emp.rate = Convert.ToDouble(employeeStreamReader.ReadLine());
empList.Add(emp);
//list.Add(emp);
x++;
}
}
catch (FormatException fe)
{
MessageBox.Show("ERROR " + fe.ToString());
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (_i == empList.Count) _i = 0;
displayRecord();//call this method
txtHours.BackColor = (Color.White);//change color back
txtHours.Text = "";//clear fields
txtGross.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
employeeStreamReader.Close();
}
private void btnCalc_Click(object sender, EventArgs e)
{
calcPay();
// txtGross.Text = gross.ToString();
}
}
}
I still get the same error -- it only shows me the last element in the file.
for instance file contains
tom
follow
10
tim
hast
20
john
sprat
12.5
it is only showing john sprat 12.5
3 times instead of each element
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|