private Dictionary <string, DelimitedRow> LoadEmployeeDictionary(string fileName, Func <DelimitedRow, string> getKey) { Dictionary <string, DelimitedRow> result = new Dictionary <string, DelimitedRow>(); using (TextReader empText = new StreamReader(fileName)) { DelimitedReader empParser = new DelimitedReader(empText, ',', true); for (;;) { DelimitedRow empRow = empParser.CurrentValues; if (empRow == null) { break; } if (empRow.Count > 0) { result.Add(getKey(empRow), empRow); } empParser.NextLine(); } } return(result); }
private void btnCreateW2_Click(object sender, EventArgs e) { if (cboTaxYear.SelectedItem == null) { MessageBox.Show("Please select a tax year"); return; } Accumulators accum = new Accumulators(); Dictionary <string, DelimitedRow> employees = LoadEmployeeDictionary(txtEmployeeFile.Text, empRow => empRow.GetString("SSN")); Dictionary <string, DelimitedRow> supplemental = LoadEmployeeDictionary(txtEmployeeSupplemental.Text, empRow => empRow.GetString("SSN")); Dictionary <string, DelimitedRow> statewideQ1 = LoadEmployeeDictionary(txtStatewideTransitQ1.Text, empRow => empRow.GetInteger("SSN").ToString("000-00-0000")); Dictionary <string, DelimitedRow> statewideQ2 = LoadEmployeeDictionary(txtStatewideTransitQ2.Text, empRow => empRow.GetInteger("SSN").ToString("000-00-0000")); Dictionary <string, DelimitedRow> statewideQ3 = LoadEmployeeDictionary(txtStatewideTransitQ3.Text, empRow => empRow.GetInteger("SSN").ToString("000-00-0000")); Dictionary <string, DelimitedRow> statewideQ4 = LoadEmployeeDictionary(txtStatewideTransitQ4.Text, empRow => empRow.GetInteger("SSN").ToString("000-00-0000")); int paycheckCount = 0; using (TextWriter writer = new StreamWriter("W2Output.txt")) { OutputRARecord(writer); OutputRERecord(writer); using (TextReader payText = new StreamReader(txtPayrollFile.Text)) { DelimitedReader payParser = new DelimitedReader(payText, ',', true); for (;;) { if (!payParser.HasCurrent) { break; } DelimitedRow paySummary = payParser.ReadUntilChange("SSN"); string ssn = paySummary.GetString("SSN"); if (!employees.TryGetValue(ssn, out DelimitedRow empRow)) { MessageBox.Show("Could not find SSN in employee file: " + ssn); return; } if (!supplemental.TryGetValue(ssn, out DelimitedRow supRow)) { MessageBox.Show("Could not find SSN in supplemental file: " + ssn); return; } OutputRWRecord(writer, paySummary, empRow, accum); decimal statewideTransitTax = GetStatewideTaxAmount(ssn, statewideQ1) + GetStatewideTaxAmount(ssn, statewideQ2) + GetStatewideTaxAmount(ssn, statewideQ3) + GetStatewideTaxAmount(ssn, statewideQ4); OutputRSRecord(writer, paySummary, empRow, supRow, statewideTransitTax, accum); paycheckCount++; } } OutputRTRecord(writer, accum); // No "RU" record, because no "RO" records. OutputRVRecord(writer, accum); OutputRFRecord(writer, accum); } MessageBox.Show("Wrote W2Output.txt to working directory with " + paycheckCount + " employees (if number is too large, maybe input file was not sorted by SSN)."); MessageBox.Show( "Federal wages: " + accum.FederalWages.ToString("F2") + Environment.NewLine + "FITW: " + accum.FITW.ToString("F2") + Environment.NewLine + "State wages: " + accum.StateWages.ToString("F2") + Environment.NewLine + "SITW: " + accum.SITW.ToString("F2") + Environment.NewLine + "Transit wages: " + accum.StatewideTransitWages.ToString("F2") + Environment.NewLine + "Transit taxes: " + accum.StatewideTransitTax.ToString("F2") + Environment.NewLine + "W-2 count: " + accum.EmployeeRecordCount.ToString()); }
private void ReadTestFile1() { try { mFileName = "TestFile1.txt"; using (TextReader reader = new StreamReader(mFileName)) { DelimitedReader delReader = new DelimitedReader(reader, ',', true); DelimitedRow row = delReader.CurrentValues; DelimitedRow totals = delReader.ColumnTotals; if (row == null) { Failure("Initial row is null"); } if (row.Count != 6) { Failure("Initial row column wrong count"); } if (totals != null) { Failure("Initial totals must be null"); } if (!delReader.HasCurrent) { Failure("Initial has no current"); } if (row.GetIndex("Name") != 0) { Failure("Name index != 0"); } if (row.GetIndex("Weight") != 2) { Failure("Weight index != 2"); } if ((string)row.GetValue("Name") != "Dog") { Failure("Name!=Dog"); } if (!(row.GetValue("Weight") is decimal)) { Failure("Dog weight is not decimal"); } if ((decimal)row.GetValue("Weight") != 20.5M) { Failure("Dog weight is not 20.5M"); } if (row.GetDecimal("Weight") != 20.5M) { Failure("Dog weight is not 20.5M (2)"); } if (!(row.GetValue("Length") is int)) { Failure("Length is not integer"); } if (row.GetInteger("Length") != 26) { Failure("Dog Length is not 26"); } row = delReader.NextLine(); if (!delReader.HasCurrent) { Failure("Second has no current"); } if (row.Count != 6) { Failure("Second row column count wrong"); } if (row.GetString("Name") != "Big Fish") { Failure("Second name is not Fish"); } if (row.GetString("Color") != "Silver") { Failure("Second color is not Silver"); } if (row.GetDecimal("Weight") != 3.5M) { Failure("Second Weight should be 3.5M"); } if (row.GetInteger("Units") != 200) { Failure("Second units is not 200"); } totals = delReader.ReadUntilChange("Break"); row = delReader.CurrentValues; if (row.GetString("Name") != "Small Fish") { Failure("UntilChange Break wrong row"); } if (!(totals.GetValue("Name") is string)) { Failure("UntilChange Name should be string"); } if (!(totals.GetValue("Weight") is decimal)) { Failure("UntilChange Weight should be decimal"); } if (totals.GetDecimal("Weight") != 3.5M) { Failure("UntilChange Weight should be 3.5M"); } totals = delReader.ReadUntilChange("Break"); row = delReader.CurrentValues; if (row.GetString("Name") != "guppy") { Failure("UntilChange2 Break wrong row"); } if (totals.GetDecimal("Weight") != ((decimal)1 + 8.5M + 300.5M)) { Failure("UntilChange2 wrong Weight total"); } if (totals.GetInteger("Units") != (3000 + 5 + 1)) { Failure("UntilChange2 wrong Units total"); } if (!(row.GetValue("Units") is int)) { Failure("UntilChange2 wrong Units type"); } row = delReader.NextLine(); if (row.GetString("Name") != "") { Failure("Blank Name is not blank"); } if (row.GetString("Color") != "none") { Failure("Blank Color is not none"); } if (row.GetString("Weight") != "") { Failure("Blank Weight is not blank"); } if (row.GetString("Length") != "") { Failure("Blank Length is not blank"); } if (row.GetInteger("Units") != 100) { Failure("Blank Units is not 100"); } row = delReader.NextLine(); if (row.GetString("Name") != "tall grass") { Failure("tall grass wrong name"); } if (!delReader.HasCurrent) { Failure("tall grass has no current"); } delReader.NextLine(); if (delReader.HasCurrent) { Failure("should not have current"); } if (!(delReader.Add("a", "B") is string)) { Failure("Add string string is not string"); } if (!(delReader.Add("a", 1) is string)) { Failure("Add string int is not string"); } if (!(delReader.Add(2, "B") is string)) { Failure("Add int string is not string"); } if (!(delReader.Add(2.5M, "B") is string)) { Failure("Add decimal string is not string"); } if (!(delReader.Add(2, 1) is int)) { Failure("Add int int is not int"); } if (!(delReader.Add(2.5M, 3.5M) is decimal)) { Failure("Add decimal decimal is not decimal"); } if (!(delReader.Add(2.5M, 3) is decimal)) { Failure("Add decimal int is not decimal"); } if (!(delReader.Add(2, 3.5M) is decimal)) { Failure("Add int decimal is not decimal"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void ReadTestFile2() { try { mFileName = "TestFile2.txt"; using (TextReader reader = new StreamReader(mFileName)) { DelimitedReader delReader = new DelimitedReader(reader, '\t', false); DelimitedRow row = delReader.CurrentValues; DelimitedRow totals = delReader.ColumnTotals; if (row == null) { Failure("Initial row is null"); } if (totals != null) { Failure("Initial totals must be null"); } if (!delReader.HasCurrent) { Failure("Initial has no current"); } if (row.GetString("Color") != "Red") { Failure("Initial color must be Red"); } if (row.GetString("Group") != "Reds") { Failure("Initial group must be Reds"); } if (row.GetString("Depth") != "\"Deep\"") { Failure("Initial Depth is not Deep"); } if (row.GetString("Description") != "\"Very deep red\"") { Failure("Initial description wrong"); } if (!(row.GetValue("Level") is int)) { Failure("Initial Level must be int"); } if (row.GetInteger("Level") != 100) { Failure("Initial Level must be 100"); } totals = delReader.ReadUntilChange("Group"); row = delReader.CurrentValues; if (row != null) { Failure("UntilChanges did not read through end"); } if (totals.GetInteger("Level") != 150) { Failure("UntilChanges Level was not 150"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }