private void Form1_Load(object sender, EventArgs e) { listView1.View = View.Details; listView1.Columns.Add("FirstName"); listView1.Columns.Add("LastName"); listView1.Columns.Add("Address"); listView1.Columns.Add("Age"); listView1.Columns.Add("GrossMoPay"); listView1.Columns.Add("DeptID"); listView1.Columns.Add("DevType"); listView1.Columns.Add("TaxType"); listView1.Columns.Add("AnnaulTax"); listView1.Columns.Add("AnnualNetPay"); // List<T> object for holding the JSON file information. List <Employee> empList = new List <Employee>(); using (StreamReader sr = new StreamReader("data.json")) { string jsonFile = sr.ReadToEnd(); empList = JsonConvert.DeserializeObject <List <Employee> >(jsonFile); } // 'match' for seacrhing and placing the right block of employee information // within the proper class. First names are used for filtering. Employee match = empList.Find(em => em.FirstName == "John"); var emp1 = new W2 { FirstName = match.FirstName, LastName = match.LastName, Address = match.Address, Age = match.Age, GrossMoPay = match.GrossMoPay, DeptID = match.DeptID, DevType = match.DevType, TaxType = match.TaxType }; emp1.AnnualPay = Convert.ToDouble(emp1.GrossMoPay); emp1.TaxCalc(); string[] item = emp1.GetInfo(); var im = new ListViewItem(item); listView1.Items.Add(im); match = empList.Find(em => em.FirstName == "Mary"); var emp2 = new T1099 { FirstName = match.FirstName, LastName = match.LastName, Address = match.Address, Age = match.Age, GrossMoPay = match.GrossMoPay, DeptID = match.DeptID, DevType = match.DevType, TaxType = match.TaxType }; emp2.AnnualPay = Convert.ToDouble(emp2.GrossMoPay); emp2.TaxCalc(); item = emp2.GetInfo(); im = new ListViewItem(item); listView1.Items.Add(im); match = empList.Find(em => em.FirstName == "Angela"); var emp3 = new T1099 { FirstName = match.FirstName, LastName = match.LastName, Address = match.Address, Age = match.Age, GrossMoPay = match.GrossMoPay, DeptID = match.DeptID, DevType = match.DevType, TaxType = match.TaxType }; emp3.AnnualPay = Convert.ToDouble(emp3.GrossMoPay); emp3.TaxCalc(); item = emp3.GetInfo(); im = new ListViewItem(item); listView1.Items.Add(im); match = empList.Find(em => em.FirstName == "Michael"); var emp4 = new W2 { FirstName = match.FirstName, LastName = match.LastName, Address = match.Address, Age = match.Age, GrossMoPay = match.GrossMoPay, DeptID = match.DeptID, DevType = match.DevType, TaxType = match.TaxType }; emp4.AnnualPay = Convert.ToDouble(emp4.GrossMoPay); emp4.TaxCalc(); item = emp4.GetInfo(); im = new ListViewItem(item); listView1.Items.Add(im); listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); }
static public void ReadFile() { int num = 0; // For switch control. using (StreamReader sr = new StreamReader("data.txt")) { string f, l, ad, ag, gr, id, d, t; String line; while ((line = sr.ReadLine()) != null) { ++num; // 'info' array for holding the 'data.txt' information, before it is copied // to its respective array. string[] info = line.Split(','); f = info[0]; l = info[1]; ad = info[2]; ag = info[3]; gr = info[4]; id = info[5]; d = info[6]; t = info[7]; // Switch for populating the different arrays. // Once the 'em#' arrays are created, they are used // to construct their respective classes. switch (num) { case 1: for (int i = 0; i < info.Length; i++) { header.Add(info[i]); } header.Add("AnnualTax"); header.Add("AnnualNetPay"); break; case 2: for (int i = 0; i < info.Length; i++) { em1.Add(info[i]); } Emp1 = new W2(em1[0], em1[1], em1[2], em1[3], em1[4], em1[5], em1[6], em1[7]); break; case 3: for (int i = 0; i < info.Length; i++) { em2.Add(info[i]); } Emp2 = new T1099(em2[0], em2[1], em2[2], em2[3], em2[4], em2[5], em2[6], em2[7]); break; case 4: for (int i = 0; i < info.Length; i++) { em3.Add(info[i]); } Emp3 = new T1099(em3[0], em3[1], em3[2], em3[3], em3[4], em3[5], em3[6], em3[7]); break; case 5: for (int i = 0; i < info.Length; i++) { em4.Add(info[i]); } Emp4 = new W2(em4[0], em4[1], em4[2], em4[3], em4[4], em4[5], em4[6], em4[7]); break; } } } }
private void Form1_Load(object sender, EventArgs e) { listView1.View = View.Details; listView1.Columns.Add("FirstName"); listView1.Columns.Add("LastName"); listView1.Columns.Add("Address"); listView1.Columns.Add("Age"); listView1.Columns.Add("GrossMoPay"); listView1.Columns.Add("DeptID"); listView1.Columns.Add("DevType"); listView1.Columns.Add("TaxType"); listView1.Columns.Add("AnnaulTax"); listView1.Columns.Add("AnnualNetPay"); // LINQ query to separate the W2 employees into own file. var getW2 = from line in File.ReadLines("data.txt") where (line.Contains("W2")) select line; File.WriteAllLines(@"W2.txt", getW2); // LINQ query to separate the 1099 employees into own file. var get1099 = from line in File.ReadLines("data.txt") where (line.Contains("1099")) select line; File.WriteAllLines(@"1099.txt", get1099); // Reads the newly created W2 file to create and calculate W2 class using (StreamReader sr = new StreamReader("W2.txt")) { string line; while ((line = sr.ReadLine()) != null) { string[] info = line.Split(','); var w2 = new W2 { First = info[0], Last = info[1], Address = info[2], Age = info[3], Gross = info[4], ID = info[5], DevType = info[6], TaxType = info[7] }; w2.AnnualPay = Convert.ToDouble(w2.Gross); w2.TaxCalc(); string[] w2row = { w2.First, w2.Last, w2.Address, w2.Age, FormatString(w2.Gross), w2.ID, w2.DevType, w2.TaxType,w2.Tax, w2.NetPay }; var w2Item = new ListViewItem(w2row); listView1.Items.Add(w2Item); } } // Reads the newly created 1099 file to create and calculate 1099 class using (StreamReader sr = new StreamReader("1099.txt")) { string line; while ((line = sr.ReadLine()) != null) { string[] info = line.Split(','); var t99 = new T1099 { First = info[0], Last = info[1], Address = info[2], Age = info[3], Gross = info[4], ID = info[5], DevType = info[6], TaxType = info[7] }; t99.AnnualPay = Convert.ToDouble(t99.Gross); t99.TaxCalc(); string[] t99row = { t99.First, t99.Last, t99.Address, t99.Age, FormatString(t99.Gross), t99.ID, t99.DevType, t99.TaxType,t99.Tax, t99.NetPay }; var t99Item = new ListViewItem(t99row); listView1.Items.Add(t99Item); } } listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); }