Using JSON file for employee pay data











up vote
2
down vote

favorite












I'm looking for some feedback on my code here. I want to eliminate duplicating the code through the different button_clicks. I'm thinking with a method but nothing I try works better than what I have.



using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EmployeePayDataWk4
{
public partial class Employee_Pay_Form : Form
{

public Employee_Pay_Form()
{
InitializeComponent();
}

private void Employee_Pay_Form_Load(object sender, EventArgs e)
{
EmployeeDataGridView.ColumnCount = 8;
EmployeeDataGridView.Columns[0].Name = "Employee Name";
EmployeeDataGridView.Columns[1].Name = "Zip Code";
EmployeeDataGridView.Columns[2].Name = "Age";
EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
EmployeeDataGridView.Columns[4].Name = "Department ID";
EmployeeDataGridView.Columns[5].Name = "Developer Type";
EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";

}

private void LoadAllButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();
//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//Display into DataGridView
foreach (Employee emp in employees)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}



private void FTEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for FT Employees
var FTEmp = from emp in employees
where emp.GetTaxForm == "W2"
select emp;

//Display into DataGridView
foreach (Employee emp in FTEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}

private void ContractEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for Contract Employees
var contractEmp = from emp in employees
where emp.GetTaxForm == "1099"
select emp;

//Display into DataGridView
foreach (Employee emp in contractEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}


//Method to determine developer type
string typeName;
public string SetDevType(int id)
{
if (id == 1)
{
typeName = "Object-Oriented";
}
else if (id == 2)
{
typeName = "Scripts";
}
else { typeName = "Unknown"; }
return typeName;
}

public double AnnualPay(double amount) => 12 * amount;
}


class Employee : IFilingStatus
{
public Employee() { }

public string Name { get; set; }
public string Zip { get; set; }
public int Age { get; set; }
public double Pay { get; set; }
public int DepartmentId { get; set; }
public string GetTaxForm { get; set; }

public double CalculateTax(double basis)
{
double monthlyTax;

if ((GetTaxForm == "W2") || (GetTaxForm == "w2"))
{
monthlyTax = .07 * basis;
}
else
{
monthlyTax = 0;
}
return 12 * monthlyTax;
}
public double AnnualPay(double amount) => 12 * amount;
}

public interface IFilingStatus
{
double CalculateTax(double basis);
}

}


Here's the JSON file



[

{
"Name": "Jeff",
"Zip": "55422",
"Age": 54,
"Pay": 9587.23,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Dave",
"Zip": "03456",
"Age": 41,
"Pay": 8547.55,
"DepartmentId": 1,
"GetTaxForm": "W2"
},

{
"Name": "Amber",
"Zip": "41908",
"Age": 35,
"Pay": 4878.1,
"DepartmentId": 2,
"GetTaxForm": "W2"
},

{
"Name": "Cassie",
"Zip": "91820",
"Age": 28,
"Pay": 4500,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Albert",
"Zip": "54321",
"Age": 39,
"Pay": 5874.09,
"DepartmentId": 2,
"GetTaxForm": "1099"
}
]









share|improve this question









New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you could show the Employee class and a sample of the json file it would help people give you better answers.
    – tinstaafl
    yesterday










  • @tinstaafl the Employee class in the main code at the bottom
    – Christopher Wood
    yesterday















up vote
2
down vote

favorite












I'm looking for some feedback on my code here. I want to eliminate duplicating the code through the different button_clicks. I'm thinking with a method but nothing I try works better than what I have.



using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EmployeePayDataWk4
{
public partial class Employee_Pay_Form : Form
{

public Employee_Pay_Form()
{
InitializeComponent();
}

private void Employee_Pay_Form_Load(object sender, EventArgs e)
{
EmployeeDataGridView.ColumnCount = 8;
EmployeeDataGridView.Columns[0].Name = "Employee Name";
EmployeeDataGridView.Columns[1].Name = "Zip Code";
EmployeeDataGridView.Columns[2].Name = "Age";
EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
EmployeeDataGridView.Columns[4].Name = "Department ID";
EmployeeDataGridView.Columns[5].Name = "Developer Type";
EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";

}

private void LoadAllButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();
//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//Display into DataGridView
foreach (Employee emp in employees)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}



private void FTEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for FT Employees
var FTEmp = from emp in employees
where emp.GetTaxForm == "W2"
select emp;

//Display into DataGridView
foreach (Employee emp in FTEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}

private void ContractEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for Contract Employees
var contractEmp = from emp in employees
where emp.GetTaxForm == "1099"
select emp;

//Display into DataGridView
foreach (Employee emp in contractEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}


//Method to determine developer type
string typeName;
public string SetDevType(int id)
{
if (id == 1)
{
typeName = "Object-Oriented";
}
else if (id == 2)
{
typeName = "Scripts";
}
else { typeName = "Unknown"; }
return typeName;
}

public double AnnualPay(double amount) => 12 * amount;
}


class Employee : IFilingStatus
{
public Employee() { }

public string Name { get; set; }
public string Zip { get; set; }
public int Age { get; set; }
public double Pay { get; set; }
public int DepartmentId { get; set; }
public string GetTaxForm { get; set; }

public double CalculateTax(double basis)
{
double monthlyTax;

if ((GetTaxForm == "W2") || (GetTaxForm == "w2"))
{
monthlyTax = .07 * basis;
}
else
{
monthlyTax = 0;
}
return 12 * monthlyTax;
}
public double AnnualPay(double amount) => 12 * amount;
}

public interface IFilingStatus
{
double CalculateTax(double basis);
}

}


Here's the JSON file



[

{
"Name": "Jeff",
"Zip": "55422",
"Age": 54,
"Pay": 9587.23,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Dave",
"Zip": "03456",
"Age": 41,
"Pay": 8547.55,
"DepartmentId": 1,
"GetTaxForm": "W2"
},

{
"Name": "Amber",
"Zip": "41908",
"Age": 35,
"Pay": 4878.1,
"DepartmentId": 2,
"GetTaxForm": "W2"
},

{
"Name": "Cassie",
"Zip": "91820",
"Age": 28,
"Pay": 4500,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Albert",
"Zip": "54321",
"Age": 39,
"Pay": 5874.09,
"DepartmentId": 2,
"GetTaxForm": "1099"
}
]









share|improve this question









New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you could show the Employee class and a sample of the json file it would help people give you better answers.
    – tinstaafl
    yesterday










  • @tinstaafl the Employee class in the main code at the bottom
    – Christopher Wood
    yesterday













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm looking for some feedback on my code here. I want to eliminate duplicating the code through the different button_clicks. I'm thinking with a method but nothing I try works better than what I have.



using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EmployeePayDataWk4
{
public partial class Employee_Pay_Form : Form
{

public Employee_Pay_Form()
{
InitializeComponent();
}

private void Employee_Pay_Form_Load(object sender, EventArgs e)
{
EmployeeDataGridView.ColumnCount = 8;
EmployeeDataGridView.Columns[0].Name = "Employee Name";
EmployeeDataGridView.Columns[1].Name = "Zip Code";
EmployeeDataGridView.Columns[2].Name = "Age";
EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
EmployeeDataGridView.Columns[4].Name = "Department ID";
EmployeeDataGridView.Columns[5].Name = "Developer Type";
EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";

}

private void LoadAllButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();
//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//Display into DataGridView
foreach (Employee emp in employees)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}



private void FTEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for FT Employees
var FTEmp = from emp in employees
where emp.GetTaxForm == "W2"
select emp;

//Display into DataGridView
foreach (Employee emp in FTEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}

private void ContractEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for Contract Employees
var contractEmp = from emp in employees
where emp.GetTaxForm == "1099"
select emp;

//Display into DataGridView
foreach (Employee emp in contractEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}


//Method to determine developer type
string typeName;
public string SetDevType(int id)
{
if (id == 1)
{
typeName = "Object-Oriented";
}
else if (id == 2)
{
typeName = "Scripts";
}
else { typeName = "Unknown"; }
return typeName;
}

public double AnnualPay(double amount) => 12 * amount;
}


class Employee : IFilingStatus
{
public Employee() { }

public string Name { get; set; }
public string Zip { get; set; }
public int Age { get; set; }
public double Pay { get; set; }
public int DepartmentId { get; set; }
public string GetTaxForm { get; set; }

public double CalculateTax(double basis)
{
double monthlyTax;

if ((GetTaxForm == "W2") || (GetTaxForm == "w2"))
{
monthlyTax = .07 * basis;
}
else
{
monthlyTax = 0;
}
return 12 * monthlyTax;
}
public double AnnualPay(double amount) => 12 * amount;
}

public interface IFilingStatus
{
double CalculateTax(double basis);
}

}


Here's the JSON file



[

{
"Name": "Jeff",
"Zip": "55422",
"Age": 54,
"Pay": 9587.23,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Dave",
"Zip": "03456",
"Age": 41,
"Pay": 8547.55,
"DepartmentId": 1,
"GetTaxForm": "W2"
},

{
"Name": "Amber",
"Zip": "41908",
"Age": 35,
"Pay": 4878.1,
"DepartmentId": 2,
"GetTaxForm": "W2"
},

{
"Name": "Cassie",
"Zip": "91820",
"Age": 28,
"Pay": 4500,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Albert",
"Zip": "54321",
"Age": 39,
"Pay": 5874.09,
"DepartmentId": 2,
"GetTaxForm": "1099"
}
]









share|improve this question









New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm looking for some feedback on my code here. I want to eliminate duplicating the code through the different button_clicks. I'm thinking with a method but nothing I try works better than what I have.



using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EmployeePayDataWk4
{
public partial class Employee_Pay_Form : Form
{

public Employee_Pay_Form()
{
InitializeComponent();
}

private void Employee_Pay_Form_Load(object sender, EventArgs e)
{
EmployeeDataGridView.ColumnCount = 8;
EmployeeDataGridView.Columns[0].Name = "Employee Name";
EmployeeDataGridView.Columns[1].Name = "Zip Code";
EmployeeDataGridView.Columns[2].Name = "Age";
EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
EmployeeDataGridView.Columns[4].Name = "Department ID";
EmployeeDataGridView.Columns[5].Name = "Developer Type";
EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";

}

private void LoadAllButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();
//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//Display into DataGridView
foreach (Employee emp in employees)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}



private void FTEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for FT Employees
var FTEmp = from emp in employees
where emp.GetTaxForm == "W2"
select emp;

//Display into DataGridView
foreach (Employee emp in FTEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}

private void ContractEmployeeButton_Click(object sender, EventArgs e)
{
EmployeeDataGridView.Rows.Clear();

//Read from JSON file
string JSONstring = File.ReadAllText("JSON.json");
List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);

//LINQ Query for Contract Employees
var contractEmp = from emp in employees
where emp.GetTaxForm == "1099"
select emp;

//Display into DataGridView
foreach (Employee emp in contractEmp)
{
string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
EmployeeDataGridView.Rows.Add(row);
}
}


//Method to determine developer type
string typeName;
public string SetDevType(int id)
{
if (id == 1)
{
typeName = "Object-Oriented";
}
else if (id == 2)
{
typeName = "Scripts";
}
else { typeName = "Unknown"; }
return typeName;
}

public double AnnualPay(double amount) => 12 * amount;
}


class Employee : IFilingStatus
{
public Employee() { }

public string Name { get; set; }
public string Zip { get; set; }
public int Age { get; set; }
public double Pay { get; set; }
public int DepartmentId { get; set; }
public string GetTaxForm { get; set; }

public double CalculateTax(double basis)
{
double monthlyTax;

if ((GetTaxForm == "W2") || (GetTaxForm == "w2"))
{
monthlyTax = .07 * basis;
}
else
{
monthlyTax = 0;
}
return 12 * monthlyTax;
}
public double AnnualPay(double amount) => 12 * amount;
}

public interface IFilingStatus
{
double CalculateTax(double basis);
}

}


Here's the JSON file



[

{
"Name": "Jeff",
"Zip": "55422",
"Age": 54,
"Pay": 9587.23,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Dave",
"Zip": "03456",
"Age": 41,
"Pay": 8547.55,
"DepartmentId": 1,
"GetTaxForm": "W2"
},

{
"Name": "Amber",
"Zip": "41908",
"Age": 35,
"Pay": 4878.1,
"DepartmentId": 2,
"GetTaxForm": "W2"
},

{
"Name": "Cassie",
"Zip": "91820",
"Age": 28,
"Pay": 4500,
"DepartmentId": 1,
"GetTaxForm": "1099"
},

{
"Name": "Albert",
"Zip": "54321",
"Age": 39,
"Pay": 5874.09,
"DepartmentId": 2,
"GetTaxForm": "1099"
}
]






c# json winforms






share|improve this question









New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 21 hours ago









t3chb0t

33.6k744108




33.6k744108






New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Christopher Wood

112




112




New contributor




Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Christopher Wood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • If you could show the Employee class and a sample of the json file it would help people give you better answers.
    – tinstaafl
    yesterday










  • @tinstaafl the Employee class in the main code at the bottom
    – Christopher Wood
    yesterday


















  • If you could show the Employee class and a sample of the json file it would help people give you better answers.
    – tinstaafl
    yesterday










  • @tinstaafl the Employee class in the main code at the bottom
    – Christopher Wood
    yesterday
















If you could show the Employee class and a sample of the json file it would help people give you better answers.
– tinstaafl
yesterday




If you could show the Employee class and a sample of the json file it would help people give you better answers.
– tinstaafl
yesterday












@tinstaafl the Employee class in the main code at the bottom
– Christopher Wood
yesterday




@tinstaafl the Employee class in the main code at the bottom
– Christopher Wood
yesterday










3 Answers
3






active

oldest

votes

















up vote
3
down vote













Your code is fairly clean and easy to read and follow. But you should definitely think about not repeating yourself. Even just a single or two line - when you feel encouraged to copy/paste - don't! Make a method and call that from where needed.





When you do something like this:




    EmployeeDataGridView.ColumnCount = 8;
EmployeeDataGridView.Columns[0].Name = "Employee Name";
EmployeeDataGridView.Columns[1].Name = "Zip Code";
EmployeeDataGridView.Columns[2].Name = "Age";
EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
EmployeeDataGridView.Columns[4].Name = "Department ID";
EmployeeDataGridView.Columns[5].Name = "Developer Type";
EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";



there is obviously better ways that is easier to maintain - an array and a loop for instance:



  DataGridView employeeDataGridView = EmployeeDataGridView;

string headers =
{
"Employee Name",
"Zip Code",
"Age",
"Monthly Gross Pay",
"Department ID",
"Developer Type",
"Annual Taxes",
"Annual Net Pay",
};

employeeDataGridView.ColumnCount = headers.Length;

for (int i = 0; i < headers.Length; i++)
{
employeeDataGridView.Columns[i].Name = headers[i];
}


This is easier to maintain. A new column is just inserted in the headers list, and reordering can be done there too - in one place.






//Method to determine developer type
string typeName;
public string SetDevType(int id)
{
if (id == 1)
{
typeName = "Object-Oriented";
}
else if (id == 2)
{
typeName = "Scripts";
}
else { typeName = "Unknown"; }
return typeName;
}



Here the typeName field is placed outside the method. Why that? And you could use an switch-case statement instead of the if's:



public string SetDevType(int id)
{
switch (id)
{
case 1:
return "Object-Oriented";
case 2:
return "Scripts";
default:
return "Unknown";
}
}




user2156791 shows a good way to refactor the initialization of the grid, but it can be done even "tighter":



private IEnumerable<Employee> LoadEmployees(string filePath)
{
//Read from JSON file
string JSONstring = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<IEnumerable<Employee>>(JSONstring);
}

private void InitializeGrid(Func<IEnumerable<Employee>, IEnumerable<Employee>> employeeSelector)
{
try
{
EmployeeDataGridView.Rows.Clear();

IEnumerable<Employee> employees = LoadEmployees(@"JSON.json");
if (employees == null)
throw new NullReferenceException("Unable to read from the data source file");

foreach (Employee employee in employeeSelector(employees))
{
string row =
{
employee.Name,
employee.Zip,
employee.Age.ToString(),
string.Format("{0:C}", employee.Pay),
employee.DepartmentId.ToString(),
SetDevType(employee.DepartmentId),
string.Format("{0:C}",
employee.CalculateTax(employee.Pay)),
string.Format("{0:C}", AnnualPay(employee.Pay) - employee.CalculateTax(employee.Pay))
};

EmployeeDataGridView.Rows.Add(row);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void LoadAllButton_Click(object sender, EventArgs e)
{
InitializeGrid(employees => employees);
}

private void FTEmployeeButton_Click(object sender, EventArgs e)
{
InitializeGrid(employees => from emp in employees
where emp.GetTaxForm == "W2"
select emp);
}

private void ContractEmployeeButton_Click(object sender, EventArgs e)
{
InitializeGrid(employees => from emp in employees
where emp.GetTaxForm == "1099"
select emp);
}


Here everything is only done in one place, and it's easy to maintain and extent or change. Because the data source is always the same a selector delegate is provided to InitializeGrid() instead of the entire source.



Futher: when interacting with the user through event handlers you should care about handling exceptions and errors and display appropriate messages to the user. A try-catch around everything is a place to start.






share|improve this answer






























    up vote
    2
    down vote













    Let's first check what your code is doing repeatedly




    • Read the json file and fill a List<Employee>

    • Filter this list by checking the GetTaxForm property of the employee which by the way is a bad name for a property, just TaxForm would be better or return all employees

    • Display the resulting List<Employee> in a DataGridView


    Now let us check what your code isn't doing




    • It doesn't change the json file




    Improvements




    • I suggest reading the json-file only once and fill a List<Employee> which you filter if needed by the desired property.


    • Having a method DisplayEmployees() or like @user2156791 stated in his/her answer FillEmployeeDataGrid() (but I would pass an IEnumerable<Employee> as the method argument).





    This




    //Method to determine developer type
    string typeName;
    public string SetDevType(int id)
    {
    if (id == 1)
    {
    typeName = "Object-Oriented";
    }
    else if (id == 2)
    {
    typeName = "Scripts";
    }
    else { typeName = "Unknown"; }
    return typeName;
    }



    looks strange in many ways. The method is called SetXX() but is getting a value. The class level field typeName is only used in this method so why is it a class level field ?



    Why do you have public double AnnualPay(double amount) => 12 * amount; inside the Employee_Pay_Form class ? Why don't you use the ´AnnualPay()from theEmployee` class ?





    Implementing the mentioned points will lead to



    private static List<Employee> LoadEmployees(string fileName)
    {
    if (string.IsNullOrWhiteSpace(fileName))
    {
    return new List<Employee>();
    }
    string content = File.ReadAllText("JSON.json");
    return JsonConvert.DeserializeObject<List<Employee>>(content );
    }


    which is called once at startup and stored in a class-level field List<Employee> eployees.



    private void DisplayEmployees(IEnumerable<Employee> avaibleEmployees)
    {
    EmployeeDataGridView.Rows.Clear();

    foreach (var employee in avaibleEmployees)
    {
    string row =
    {
    employee.Name,
    employee.Zip,
    employee.Age.ToString(),
    string.Format("{0:C}", employee.Pay),
    employee.DepartmentId.ToString(),
    employee.FetchDevType(employee.DepartmentId),
    string.Format("{0:C}", employee.CalculateTax(emp.Pay)),
    string.Format("{0:C}", employee.AnnualPay(emp.Pay) - employee.CalculateTax(emp.Pay))
    };
    EmployeeDataGridView.Rows.Add(row);
    }
    }


    where FetchDevType() looks like so



    public string FetchDevType(int departmentId)
    {
    switch (departmentId)
    {
    case 1:
    return "Object-Oriented";
    case 2:
    return "Scripts";
    default:
    return "Unknown";
    }
    }


    and should be placed inside the Employee class.



    private IEnumerable<Employee> FilterByTaxForm(string desiredTaxForm)
    {
    return from employee in employees
    where employoee.TaxForm == desiredTaxForm
    select employee;
    }


    which is called where you need to filter the eployees like e.g so



    private void ContractEmployeeButton_Click(object sender, EventArgs e)
    {
    DisplayEmployees(FilterByTaxForm("1099"));
    }





    share|improve this answer




























      up vote
      -2
      down vote













      Please check below refactoring code.



      I have check the code and refactoring code with common method. So, if you have change common method then effect to all place. No need to change every place where you write same code or same logic.



      private void LoadAllButton_Click(object sender, EventArgs e)
      {
      //Read from JSON file
      List<Employee> employees = ReadJsonData();

      FillEmployeeDataGrid(employees);
      }

      private List<Employee> ReadJsonData()
      {
      string JSONstring = File.ReadAllText("JSON.json");
      List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);
      return employees;
      }

      private void FillEmployeeDataGrid(List<Employee> employees)
      {
      EmployeeDataGridView.Rows.Clear();
      //Display into DataGridView
      foreach (Employee emp in employees)
      {
      string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
      emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
      string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
      string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
      EmployeeDataGridView.Rows.Add(row);
      }
      }

      private void FTEmployeeButton_Click(object sender, EventArgs e)
      {
      List<Employee> employees = ReadJsonData();

      //LINQ Query for FT Employees
      var FTEmp = from emp in employees
      where emp.GetTaxForm == "W2"
      select emp;

      //Display into DataGridView
      FillEmployeeDataGrid(FTEmp.ToList());
      }

      private void ContractEmployeeButton_Click(object sender, EventArgs e)
      {
      //Read from JSON file
      List<Employee> employees = ReadJsonData();

      //LINQ Query for Contract Employees
      var contractEmp = from emp in employees
      where emp.GetTaxForm == "1099"
      select emp;
      //Display into DataGridView
      FillEmployeeDataGrid(contractEmp.ToList());
      }

      //Method to determine developer type
      string typeName;
      public string SetDevType(int id)
      {
      if (id == 1)
      {
      typeName = "Object-Oriented";
      }
      else if (id == 2)
      {
      typeName = "Scripts";
      }
      else { typeName = "Unknown"; }
      return typeName;
      }

      public double AnnualPay(double amount) => 12 * amount;





      share|improve this answer










      New contributor




      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.














      • 1




        Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
        – t3chb0t
        21 hours ago













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "196"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Christopher Wood is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208027%2fusing-json-file-for-employee-pay-data%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Your code is fairly clean and easy to read and follow. But you should definitely think about not repeating yourself. Even just a single or two line - when you feel encouraged to copy/paste - don't! Make a method and call that from where needed.





      When you do something like this:




          EmployeeDataGridView.ColumnCount = 8;
      EmployeeDataGridView.Columns[0].Name = "Employee Name";
      EmployeeDataGridView.Columns[1].Name = "Zip Code";
      EmployeeDataGridView.Columns[2].Name = "Age";
      EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
      EmployeeDataGridView.Columns[4].Name = "Department ID";
      EmployeeDataGridView.Columns[5].Name = "Developer Type";
      EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
      EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";



      there is obviously better ways that is easier to maintain - an array and a loop for instance:



        DataGridView employeeDataGridView = EmployeeDataGridView;

      string headers =
      {
      "Employee Name",
      "Zip Code",
      "Age",
      "Monthly Gross Pay",
      "Department ID",
      "Developer Type",
      "Annual Taxes",
      "Annual Net Pay",
      };

      employeeDataGridView.ColumnCount = headers.Length;

      for (int i = 0; i < headers.Length; i++)
      {
      employeeDataGridView.Columns[i].Name = headers[i];
      }


      This is easier to maintain. A new column is just inserted in the headers list, and reordering can be done there too - in one place.






      //Method to determine developer type
      string typeName;
      public string SetDevType(int id)
      {
      if (id == 1)
      {
      typeName = "Object-Oriented";
      }
      else if (id == 2)
      {
      typeName = "Scripts";
      }
      else { typeName = "Unknown"; }
      return typeName;
      }



      Here the typeName field is placed outside the method. Why that? And you could use an switch-case statement instead of the if's:



      public string SetDevType(int id)
      {
      switch (id)
      {
      case 1:
      return "Object-Oriented";
      case 2:
      return "Scripts";
      default:
      return "Unknown";
      }
      }




      user2156791 shows a good way to refactor the initialization of the grid, but it can be done even "tighter":



      private IEnumerable<Employee> LoadEmployees(string filePath)
      {
      //Read from JSON file
      string JSONstring = File.ReadAllText(filePath);
      return JsonConvert.DeserializeObject<IEnumerable<Employee>>(JSONstring);
      }

      private void InitializeGrid(Func<IEnumerable<Employee>, IEnumerable<Employee>> employeeSelector)
      {
      try
      {
      EmployeeDataGridView.Rows.Clear();

      IEnumerable<Employee> employees = LoadEmployees(@"JSON.json");
      if (employees == null)
      throw new NullReferenceException("Unable to read from the data source file");

      foreach (Employee employee in employeeSelector(employees))
      {
      string row =
      {
      employee.Name,
      employee.Zip,
      employee.Age.ToString(),
      string.Format("{0:C}", employee.Pay),
      employee.DepartmentId.ToString(),
      SetDevType(employee.DepartmentId),
      string.Format("{0:C}",
      employee.CalculateTax(employee.Pay)),
      string.Format("{0:C}", AnnualPay(employee.Pay) - employee.CalculateTax(employee.Pay))
      };

      EmployeeDataGridView.Rows.Add(row);
      }
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }

      private void LoadAllButton_Click(object sender, EventArgs e)
      {
      InitializeGrid(employees => employees);
      }

      private void FTEmployeeButton_Click(object sender, EventArgs e)
      {
      InitializeGrid(employees => from emp in employees
      where emp.GetTaxForm == "W2"
      select emp);
      }

      private void ContractEmployeeButton_Click(object sender, EventArgs e)
      {
      InitializeGrid(employees => from emp in employees
      where emp.GetTaxForm == "1099"
      select emp);
      }


      Here everything is only done in one place, and it's easy to maintain and extent or change. Because the data source is always the same a selector delegate is provided to InitializeGrid() instead of the entire source.



      Futher: when interacting with the user through event handlers you should care about handling exceptions and errors and display appropriate messages to the user. A try-catch around everything is a place to start.






      share|improve this answer



























        up vote
        3
        down vote













        Your code is fairly clean and easy to read and follow. But you should definitely think about not repeating yourself. Even just a single or two line - when you feel encouraged to copy/paste - don't! Make a method and call that from where needed.





        When you do something like this:




            EmployeeDataGridView.ColumnCount = 8;
        EmployeeDataGridView.Columns[0].Name = "Employee Name";
        EmployeeDataGridView.Columns[1].Name = "Zip Code";
        EmployeeDataGridView.Columns[2].Name = "Age";
        EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
        EmployeeDataGridView.Columns[4].Name = "Department ID";
        EmployeeDataGridView.Columns[5].Name = "Developer Type";
        EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
        EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";



        there is obviously better ways that is easier to maintain - an array and a loop for instance:



          DataGridView employeeDataGridView = EmployeeDataGridView;

        string headers =
        {
        "Employee Name",
        "Zip Code",
        "Age",
        "Monthly Gross Pay",
        "Department ID",
        "Developer Type",
        "Annual Taxes",
        "Annual Net Pay",
        };

        employeeDataGridView.ColumnCount = headers.Length;

        for (int i = 0; i < headers.Length; i++)
        {
        employeeDataGridView.Columns[i].Name = headers[i];
        }


        This is easier to maintain. A new column is just inserted in the headers list, and reordering can be done there too - in one place.






        //Method to determine developer type
        string typeName;
        public string SetDevType(int id)
        {
        if (id == 1)
        {
        typeName = "Object-Oriented";
        }
        else if (id == 2)
        {
        typeName = "Scripts";
        }
        else { typeName = "Unknown"; }
        return typeName;
        }



        Here the typeName field is placed outside the method. Why that? And you could use an switch-case statement instead of the if's:



        public string SetDevType(int id)
        {
        switch (id)
        {
        case 1:
        return "Object-Oriented";
        case 2:
        return "Scripts";
        default:
        return "Unknown";
        }
        }




        user2156791 shows a good way to refactor the initialization of the grid, but it can be done even "tighter":



        private IEnumerable<Employee> LoadEmployees(string filePath)
        {
        //Read from JSON file
        string JSONstring = File.ReadAllText(filePath);
        return JsonConvert.DeserializeObject<IEnumerable<Employee>>(JSONstring);
        }

        private void InitializeGrid(Func<IEnumerable<Employee>, IEnumerable<Employee>> employeeSelector)
        {
        try
        {
        EmployeeDataGridView.Rows.Clear();

        IEnumerable<Employee> employees = LoadEmployees(@"JSON.json");
        if (employees == null)
        throw new NullReferenceException("Unable to read from the data source file");

        foreach (Employee employee in employeeSelector(employees))
        {
        string row =
        {
        employee.Name,
        employee.Zip,
        employee.Age.ToString(),
        string.Format("{0:C}", employee.Pay),
        employee.DepartmentId.ToString(),
        SetDevType(employee.DepartmentId),
        string.Format("{0:C}",
        employee.CalculateTax(employee.Pay)),
        string.Format("{0:C}", AnnualPay(employee.Pay) - employee.CalculateTax(employee.Pay))
        };

        EmployeeDataGridView.Rows.Add(row);
        }
        }
        catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
        }

        private void LoadAllButton_Click(object sender, EventArgs e)
        {
        InitializeGrid(employees => employees);
        }

        private void FTEmployeeButton_Click(object sender, EventArgs e)
        {
        InitializeGrid(employees => from emp in employees
        where emp.GetTaxForm == "W2"
        select emp);
        }

        private void ContractEmployeeButton_Click(object sender, EventArgs e)
        {
        InitializeGrid(employees => from emp in employees
        where emp.GetTaxForm == "1099"
        select emp);
        }


        Here everything is only done in one place, and it's easy to maintain and extent or change. Because the data source is always the same a selector delegate is provided to InitializeGrid() instead of the entire source.



        Futher: when interacting with the user through event handlers you should care about handling exceptions and errors and display appropriate messages to the user. A try-catch around everything is a place to start.






        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote









          Your code is fairly clean and easy to read and follow. But you should definitely think about not repeating yourself. Even just a single or two line - when you feel encouraged to copy/paste - don't! Make a method and call that from where needed.





          When you do something like this:




              EmployeeDataGridView.ColumnCount = 8;
          EmployeeDataGridView.Columns[0].Name = "Employee Name";
          EmployeeDataGridView.Columns[1].Name = "Zip Code";
          EmployeeDataGridView.Columns[2].Name = "Age";
          EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
          EmployeeDataGridView.Columns[4].Name = "Department ID";
          EmployeeDataGridView.Columns[5].Name = "Developer Type";
          EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
          EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";



          there is obviously better ways that is easier to maintain - an array and a loop for instance:



            DataGridView employeeDataGridView = EmployeeDataGridView;

          string headers =
          {
          "Employee Name",
          "Zip Code",
          "Age",
          "Monthly Gross Pay",
          "Department ID",
          "Developer Type",
          "Annual Taxes",
          "Annual Net Pay",
          };

          employeeDataGridView.ColumnCount = headers.Length;

          for (int i = 0; i < headers.Length; i++)
          {
          employeeDataGridView.Columns[i].Name = headers[i];
          }


          This is easier to maintain. A new column is just inserted in the headers list, and reordering can be done there too - in one place.






          //Method to determine developer type
          string typeName;
          public string SetDevType(int id)
          {
          if (id == 1)
          {
          typeName = "Object-Oriented";
          }
          else if (id == 2)
          {
          typeName = "Scripts";
          }
          else { typeName = "Unknown"; }
          return typeName;
          }



          Here the typeName field is placed outside the method. Why that? And you could use an switch-case statement instead of the if's:



          public string SetDevType(int id)
          {
          switch (id)
          {
          case 1:
          return "Object-Oriented";
          case 2:
          return "Scripts";
          default:
          return "Unknown";
          }
          }




          user2156791 shows a good way to refactor the initialization of the grid, but it can be done even "tighter":



          private IEnumerable<Employee> LoadEmployees(string filePath)
          {
          //Read from JSON file
          string JSONstring = File.ReadAllText(filePath);
          return JsonConvert.DeserializeObject<IEnumerable<Employee>>(JSONstring);
          }

          private void InitializeGrid(Func<IEnumerable<Employee>, IEnumerable<Employee>> employeeSelector)
          {
          try
          {
          EmployeeDataGridView.Rows.Clear();

          IEnumerable<Employee> employees = LoadEmployees(@"JSON.json");
          if (employees == null)
          throw new NullReferenceException("Unable to read from the data source file");

          foreach (Employee employee in employeeSelector(employees))
          {
          string row =
          {
          employee.Name,
          employee.Zip,
          employee.Age.ToString(),
          string.Format("{0:C}", employee.Pay),
          employee.DepartmentId.ToString(),
          SetDevType(employee.DepartmentId),
          string.Format("{0:C}",
          employee.CalculateTax(employee.Pay)),
          string.Format("{0:C}", AnnualPay(employee.Pay) - employee.CalculateTax(employee.Pay))
          };

          EmployeeDataGridView.Rows.Add(row);
          }
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }

          private void LoadAllButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => employees);
          }

          private void FTEmployeeButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => from emp in employees
          where emp.GetTaxForm == "W2"
          select emp);
          }

          private void ContractEmployeeButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => from emp in employees
          where emp.GetTaxForm == "1099"
          select emp);
          }


          Here everything is only done in one place, and it's easy to maintain and extent or change. Because the data source is always the same a selector delegate is provided to InitializeGrid() instead of the entire source.



          Futher: when interacting with the user through event handlers you should care about handling exceptions and errors and display appropriate messages to the user. A try-catch around everything is a place to start.






          share|improve this answer














          Your code is fairly clean and easy to read and follow. But you should definitely think about not repeating yourself. Even just a single or two line - when you feel encouraged to copy/paste - don't! Make a method and call that from where needed.





          When you do something like this:




              EmployeeDataGridView.ColumnCount = 8;
          EmployeeDataGridView.Columns[0].Name = "Employee Name";
          EmployeeDataGridView.Columns[1].Name = "Zip Code";
          EmployeeDataGridView.Columns[2].Name = "Age";
          EmployeeDataGridView.Columns[3].Name = "Monthly Gross Pay";
          EmployeeDataGridView.Columns[4].Name = "Department ID";
          EmployeeDataGridView.Columns[5].Name = "Developer Type";
          EmployeeDataGridView.Columns[6].Name = "Annual Taxes";
          EmployeeDataGridView.Columns[7].Name = "Annual Net Pay";



          there is obviously better ways that is easier to maintain - an array and a loop for instance:



            DataGridView employeeDataGridView = EmployeeDataGridView;

          string headers =
          {
          "Employee Name",
          "Zip Code",
          "Age",
          "Monthly Gross Pay",
          "Department ID",
          "Developer Type",
          "Annual Taxes",
          "Annual Net Pay",
          };

          employeeDataGridView.ColumnCount = headers.Length;

          for (int i = 0; i < headers.Length; i++)
          {
          employeeDataGridView.Columns[i].Name = headers[i];
          }


          This is easier to maintain. A new column is just inserted in the headers list, and reordering can be done there too - in one place.






          //Method to determine developer type
          string typeName;
          public string SetDevType(int id)
          {
          if (id == 1)
          {
          typeName = "Object-Oriented";
          }
          else if (id == 2)
          {
          typeName = "Scripts";
          }
          else { typeName = "Unknown"; }
          return typeName;
          }



          Here the typeName field is placed outside the method. Why that? And you could use an switch-case statement instead of the if's:



          public string SetDevType(int id)
          {
          switch (id)
          {
          case 1:
          return "Object-Oriented";
          case 2:
          return "Scripts";
          default:
          return "Unknown";
          }
          }




          user2156791 shows a good way to refactor the initialization of the grid, but it can be done even "tighter":



          private IEnumerable<Employee> LoadEmployees(string filePath)
          {
          //Read from JSON file
          string JSONstring = File.ReadAllText(filePath);
          return JsonConvert.DeserializeObject<IEnumerable<Employee>>(JSONstring);
          }

          private void InitializeGrid(Func<IEnumerable<Employee>, IEnumerable<Employee>> employeeSelector)
          {
          try
          {
          EmployeeDataGridView.Rows.Clear();

          IEnumerable<Employee> employees = LoadEmployees(@"JSON.json");
          if (employees == null)
          throw new NullReferenceException("Unable to read from the data source file");

          foreach (Employee employee in employeeSelector(employees))
          {
          string row =
          {
          employee.Name,
          employee.Zip,
          employee.Age.ToString(),
          string.Format("{0:C}", employee.Pay),
          employee.DepartmentId.ToString(),
          SetDevType(employee.DepartmentId),
          string.Format("{0:C}",
          employee.CalculateTax(employee.Pay)),
          string.Format("{0:C}", AnnualPay(employee.Pay) - employee.CalculateTax(employee.Pay))
          };

          EmployeeDataGridView.Rows.Add(row);
          }
          }
          catch (Exception ex)
          {
          MessageBox.Show(ex.Message);
          }
          }

          private void LoadAllButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => employees);
          }

          private void FTEmployeeButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => from emp in employees
          where emp.GetTaxForm == "W2"
          select emp);
          }

          private void ContractEmployeeButton_Click(object sender, EventArgs e)
          {
          InitializeGrid(employees => from emp in employees
          where emp.GetTaxForm == "1099"
          select emp);
          }


          Here everything is only done in one place, and it's easy to maintain and extent or change. Because the data source is always the same a selector delegate is provided to InitializeGrid() instead of the entire source.



          Futher: when interacting with the user through event handlers you should care about handling exceptions and errors and display appropriate messages to the user. A try-catch around everything is a place to start.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 21 hours ago

























          answered 21 hours ago









          Henrik Hansen

          6,0831722




          6,0831722
























              up vote
              2
              down vote













              Let's first check what your code is doing repeatedly




              • Read the json file and fill a List<Employee>

              • Filter this list by checking the GetTaxForm property of the employee which by the way is a bad name for a property, just TaxForm would be better or return all employees

              • Display the resulting List<Employee> in a DataGridView


              Now let us check what your code isn't doing




              • It doesn't change the json file




              Improvements




              • I suggest reading the json-file only once and fill a List<Employee> which you filter if needed by the desired property.


              • Having a method DisplayEmployees() or like @user2156791 stated in his/her answer FillEmployeeDataGrid() (but I would pass an IEnumerable<Employee> as the method argument).





              This




              //Method to determine developer type
              string typeName;
              public string SetDevType(int id)
              {
              if (id == 1)
              {
              typeName = "Object-Oriented";
              }
              else if (id == 2)
              {
              typeName = "Scripts";
              }
              else { typeName = "Unknown"; }
              return typeName;
              }



              looks strange in many ways. The method is called SetXX() but is getting a value. The class level field typeName is only used in this method so why is it a class level field ?



              Why do you have public double AnnualPay(double amount) => 12 * amount; inside the Employee_Pay_Form class ? Why don't you use the ´AnnualPay()from theEmployee` class ?





              Implementing the mentioned points will lead to



              private static List<Employee> LoadEmployees(string fileName)
              {
              if (string.IsNullOrWhiteSpace(fileName))
              {
              return new List<Employee>();
              }
              string content = File.ReadAllText("JSON.json");
              return JsonConvert.DeserializeObject<List<Employee>>(content );
              }


              which is called once at startup and stored in a class-level field List<Employee> eployees.



              private void DisplayEmployees(IEnumerable<Employee> avaibleEmployees)
              {
              EmployeeDataGridView.Rows.Clear();

              foreach (var employee in avaibleEmployees)
              {
              string row =
              {
              employee.Name,
              employee.Zip,
              employee.Age.ToString(),
              string.Format("{0:C}", employee.Pay),
              employee.DepartmentId.ToString(),
              employee.FetchDevType(employee.DepartmentId),
              string.Format("{0:C}", employee.CalculateTax(emp.Pay)),
              string.Format("{0:C}", employee.AnnualPay(emp.Pay) - employee.CalculateTax(emp.Pay))
              };
              EmployeeDataGridView.Rows.Add(row);
              }
              }


              where FetchDevType() looks like so



              public string FetchDevType(int departmentId)
              {
              switch (departmentId)
              {
              case 1:
              return "Object-Oriented";
              case 2:
              return "Scripts";
              default:
              return "Unknown";
              }
              }


              and should be placed inside the Employee class.



              private IEnumerable<Employee> FilterByTaxForm(string desiredTaxForm)
              {
              return from employee in employees
              where employoee.TaxForm == desiredTaxForm
              select employee;
              }


              which is called where you need to filter the eployees like e.g so



              private void ContractEmployeeButton_Click(object sender, EventArgs e)
              {
              DisplayEmployees(FilterByTaxForm("1099"));
              }





              share|improve this answer

























                up vote
                2
                down vote













                Let's first check what your code is doing repeatedly




                • Read the json file and fill a List<Employee>

                • Filter this list by checking the GetTaxForm property of the employee which by the way is a bad name for a property, just TaxForm would be better or return all employees

                • Display the resulting List<Employee> in a DataGridView


                Now let us check what your code isn't doing




                • It doesn't change the json file




                Improvements




                • I suggest reading the json-file only once and fill a List<Employee> which you filter if needed by the desired property.


                • Having a method DisplayEmployees() or like @user2156791 stated in his/her answer FillEmployeeDataGrid() (but I would pass an IEnumerable<Employee> as the method argument).





                This




                //Method to determine developer type
                string typeName;
                public string SetDevType(int id)
                {
                if (id == 1)
                {
                typeName = "Object-Oriented";
                }
                else if (id == 2)
                {
                typeName = "Scripts";
                }
                else { typeName = "Unknown"; }
                return typeName;
                }



                looks strange in many ways. The method is called SetXX() but is getting a value. The class level field typeName is only used in this method so why is it a class level field ?



                Why do you have public double AnnualPay(double amount) => 12 * amount; inside the Employee_Pay_Form class ? Why don't you use the ´AnnualPay()from theEmployee` class ?





                Implementing the mentioned points will lead to



                private static List<Employee> LoadEmployees(string fileName)
                {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                return new List<Employee>();
                }
                string content = File.ReadAllText("JSON.json");
                return JsonConvert.DeserializeObject<List<Employee>>(content );
                }


                which is called once at startup and stored in a class-level field List<Employee> eployees.



                private void DisplayEmployees(IEnumerable<Employee> avaibleEmployees)
                {
                EmployeeDataGridView.Rows.Clear();

                foreach (var employee in avaibleEmployees)
                {
                string row =
                {
                employee.Name,
                employee.Zip,
                employee.Age.ToString(),
                string.Format("{0:C}", employee.Pay),
                employee.DepartmentId.ToString(),
                employee.FetchDevType(employee.DepartmentId),
                string.Format("{0:C}", employee.CalculateTax(emp.Pay)),
                string.Format("{0:C}", employee.AnnualPay(emp.Pay) - employee.CalculateTax(emp.Pay))
                };
                EmployeeDataGridView.Rows.Add(row);
                }
                }


                where FetchDevType() looks like so



                public string FetchDevType(int departmentId)
                {
                switch (departmentId)
                {
                case 1:
                return "Object-Oriented";
                case 2:
                return "Scripts";
                default:
                return "Unknown";
                }
                }


                and should be placed inside the Employee class.



                private IEnumerable<Employee> FilterByTaxForm(string desiredTaxForm)
                {
                return from employee in employees
                where employoee.TaxForm == desiredTaxForm
                select employee;
                }


                which is called where you need to filter the eployees like e.g so



                private void ContractEmployeeButton_Click(object sender, EventArgs e)
                {
                DisplayEmployees(FilterByTaxForm("1099"));
                }





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Let's first check what your code is doing repeatedly




                  • Read the json file and fill a List<Employee>

                  • Filter this list by checking the GetTaxForm property of the employee which by the way is a bad name for a property, just TaxForm would be better or return all employees

                  • Display the resulting List<Employee> in a DataGridView


                  Now let us check what your code isn't doing




                  • It doesn't change the json file




                  Improvements




                  • I suggest reading the json-file only once and fill a List<Employee> which you filter if needed by the desired property.


                  • Having a method DisplayEmployees() or like @user2156791 stated in his/her answer FillEmployeeDataGrid() (but I would pass an IEnumerable<Employee> as the method argument).





                  This




                  //Method to determine developer type
                  string typeName;
                  public string SetDevType(int id)
                  {
                  if (id == 1)
                  {
                  typeName = "Object-Oriented";
                  }
                  else if (id == 2)
                  {
                  typeName = "Scripts";
                  }
                  else { typeName = "Unknown"; }
                  return typeName;
                  }



                  looks strange in many ways. The method is called SetXX() but is getting a value. The class level field typeName is only used in this method so why is it a class level field ?



                  Why do you have public double AnnualPay(double amount) => 12 * amount; inside the Employee_Pay_Form class ? Why don't you use the ´AnnualPay()from theEmployee` class ?





                  Implementing the mentioned points will lead to



                  private static List<Employee> LoadEmployees(string fileName)
                  {
                  if (string.IsNullOrWhiteSpace(fileName))
                  {
                  return new List<Employee>();
                  }
                  string content = File.ReadAllText("JSON.json");
                  return JsonConvert.DeserializeObject<List<Employee>>(content );
                  }


                  which is called once at startup and stored in a class-level field List<Employee> eployees.



                  private void DisplayEmployees(IEnumerable<Employee> avaibleEmployees)
                  {
                  EmployeeDataGridView.Rows.Clear();

                  foreach (var employee in avaibleEmployees)
                  {
                  string row =
                  {
                  employee.Name,
                  employee.Zip,
                  employee.Age.ToString(),
                  string.Format("{0:C}", employee.Pay),
                  employee.DepartmentId.ToString(),
                  employee.FetchDevType(employee.DepartmentId),
                  string.Format("{0:C}", employee.CalculateTax(emp.Pay)),
                  string.Format("{0:C}", employee.AnnualPay(emp.Pay) - employee.CalculateTax(emp.Pay))
                  };
                  EmployeeDataGridView.Rows.Add(row);
                  }
                  }


                  where FetchDevType() looks like so



                  public string FetchDevType(int departmentId)
                  {
                  switch (departmentId)
                  {
                  case 1:
                  return "Object-Oriented";
                  case 2:
                  return "Scripts";
                  default:
                  return "Unknown";
                  }
                  }


                  and should be placed inside the Employee class.



                  private IEnumerable<Employee> FilterByTaxForm(string desiredTaxForm)
                  {
                  return from employee in employees
                  where employoee.TaxForm == desiredTaxForm
                  select employee;
                  }


                  which is called where you need to filter the eployees like e.g so



                  private void ContractEmployeeButton_Click(object sender, EventArgs e)
                  {
                  DisplayEmployees(FilterByTaxForm("1099"));
                  }





                  share|improve this answer












                  Let's first check what your code is doing repeatedly




                  • Read the json file and fill a List<Employee>

                  • Filter this list by checking the GetTaxForm property of the employee which by the way is a bad name for a property, just TaxForm would be better or return all employees

                  • Display the resulting List<Employee> in a DataGridView


                  Now let us check what your code isn't doing




                  • It doesn't change the json file




                  Improvements




                  • I suggest reading the json-file only once and fill a List<Employee> which you filter if needed by the desired property.


                  • Having a method DisplayEmployees() or like @user2156791 stated in his/her answer FillEmployeeDataGrid() (but I would pass an IEnumerable<Employee> as the method argument).





                  This




                  //Method to determine developer type
                  string typeName;
                  public string SetDevType(int id)
                  {
                  if (id == 1)
                  {
                  typeName = "Object-Oriented";
                  }
                  else if (id == 2)
                  {
                  typeName = "Scripts";
                  }
                  else { typeName = "Unknown"; }
                  return typeName;
                  }



                  looks strange in many ways. The method is called SetXX() but is getting a value. The class level field typeName is only used in this method so why is it a class level field ?



                  Why do you have public double AnnualPay(double amount) => 12 * amount; inside the Employee_Pay_Form class ? Why don't you use the ´AnnualPay()from theEmployee` class ?





                  Implementing the mentioned points will lead to



                  private static List<Employee> LoadEmployees(string fileName)
                  {
                  if (string.IsNullOrWhiteSpace(fileName))
                  {
                  return new List<Employee>();
                  }
                  string content = File.ReadAllText("JSON.json");
                  return JsonConvert.DeserializeObject<List<Employee>>(content );
                  }


                  which is called once at startup and stored in a class-level field List<Employee> eployees.



                  private void DisplayEmployees(IEnumerable<Employee> avaibleEmployees)
                  {
                  EmployeeDataGridView.Rows.Clear();

                  foreach (var employee in avaibleEmployees)
                  {
                  string row =
                  {
                  employee.Name,
                  employee.Zip,
                  employee.Age.ToString(),
                  string.Format("{0:C}", employee.Pay),
                  employee.DepartmentId.ToString(),
                  employee.FetchDevType(employee.DepartmentId),
                  string.Format("{0:C}", employee.CalculateTax(emp.Pay)),
                  string.Format("{0:C}", employee.AnnualPay(emp.Pay) - employee.CalculateTax(emp.Pay))
                  };
                  EmployeeDataGridView.Rows.Add(row);
                  }
                  }


                  where FetchDevType() looks like so



                  public string FetchDevType(int departmentId)
                  {
                  switch (departmentId)
                  {
                  case 1:
                  return "Object-Oriented";
                  case 2:
                  return "Scripts";
                  default:
                  return "Unknown";
                  }
                  }


                  and should be placed inside the Employee class.



                  private IEnumerable<Employee> FilterByTaxForm(string desiredTaxForm)
                  {
                  return from employee in employees
                  where employoee.TaxForm == desiredTaxForm
                  select employee;
                  }


                  which is called where you need to filter the eployees like e.g so



                  private void ContractEmployeeButton_Click(object sender, EventArgs e)
                  {
                  DisplayEmployees(FilterByTaxForm("1099"));
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 21 hours ago









                  Heslacher

                  44.5k460154




                  44.5k460154






















                      up vote
                      -2
                      down vote













                      Please check below refactoring code.



                      I have check the code and refactoring code with common method. So, if you have change common method then effect to all place. No need to change every place where you write same code or same logic.



                      private void LoadAllButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      FillEmployeeDataGrid(employees);
                      }

                      private List<Employee> ReadJsonData()
                      {
                      string JSONstring = File.ReadAllText("JSON.json");
                      List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);
                      return employees;
                      }

                      private void FillEmployeeDataGrid(List<Employee> employees)
                      {
                      EmployeeDataGridView.Rows.Clear();
                      //Display into DataGridView
                      foreach (Employee emp in employees)
                      {
                      string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
                      emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
                      string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
                      string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
                      EmployeeDataGridView.Rows.Add(row);
                      }
                      }

                      private void FTEmployeeButton_Click(object sender, EventArgs e)
                      {
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for FT Employees
                      var FTEmp = from emp in employees
                      where emp.GetTaxForm == "W2"
                      select emp;

                      //Display into DataGridView
                      FillEmployeeDataGrid(FTEmp.ToList());
                      }

                      private void ContractEmployeeButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for Contract Employees
                      var contractEmp = from emp in employees
                      where emp.GetTaxForm == "1099"
                      select emp;
                      //Display into DataGridView
                      FillEmployeeDataGrid(contractEmp.ToList());
                      }

                      //Method to determine developer type
                      string typeName;
                      public string SetDevType(int id)
                      {
                      if (id == 1)
                      {
                      typeName = "Object-Oriented";
                      }
                      else if (id == 2)
                      {
                      typeName = "Scripts";
                      }
                      else { typeName = "Unknown"; }
                      return typeName;
                      }

                      public double AnnualPay(double amount) => 12 * amount;





                      share|improve this answer










                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.














                      • 1




                        Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                        – t3chb0t
                        21 hours ago

















                      up vote
                      -2
                      down vote













                      Please check below refactoring code.



                      I have check the code and refactoring code with common method. So, if you have change common method then effect to all place. No need to change every place where you write same code or same logic.



                      private void LoadAllButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      FillEmployeeDataGrid(employees);
                      }

                      private List<Employee> ReadJsonData()
                      {
                      string JSONstring = File.ReadAllText("JSON.json");
                      List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);
                      return employees;
                      }

                      private void FillEmployeeDataGrid(List<Employee> employees)
                      {
                      EmployeeDataGridView.Rows.Clear();
                      //Display into DataGridView
                      foreach (Employee emp in employees)
                      {
                      string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
                      emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
                      string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
                      string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
                      EmployeeDataGridView.Rows.Add(row);
                      }
                      }

                      private void FTEmployeeButton_Click(object sender, EventArgs e)
                      {
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for FT Employees
                      var FTEmp = from emp in employees
                      where emp.GetTaxForm == "W2"
                      select emp;

                      //Display into DataGridView
                      FillEmployeeDataGrid(FTEmp.ToList());
                      }

                      private void ContractEmployeeButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for Contract Employees
                      var contractEmp = from emp in employees
                      where emp.GetTaxForm == "1099"
                      select emp;
                      //Display into DataGridView
                      FillEmployeeDataGrid(contractEmp.ToList());
                      }

                      //Method to determine developer type
                      string typeName;
                      public string SetDevType(int id)
                      {
                      if (id == 1)
                      {
                      typeName = "Object-Oriented";
                      }
                      else if (id == 2)
                      {
                      typeName = "Scripts";
                      }
                      else { typeName = "Unknown"; }
                      return typeName;
                      }

                      public double AnnualPay(double amount) => 12 * amount;





                      share|improve this answer










                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.














                      • 1




                        Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                        – t3chb0t
                        21 hours ago















                      up vote
                      -2
                      down vote










                      up vote
                      -2
                      down vote









                      Please check below refactoring code.



                      I have check the code and refactoring code with common method. So, if you have change common method then effect to all place. No need to change every place where you write same code or same logic.



                      private void LoadAllButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      FillEmployeeDataGrid(employees);
                      }

                      private List<Employee> ReadJsonData()
                      {
                      string JSONstring = File.ReadAllText("JSON.json");
                      List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);
                      return employees;
                      }

                      private void FillEmployeeDataGrid(List<Employee> employees)
                      {
                      EmployeeDataGridView.Rows.Clear();
                      //Display into DataGridView
                      foreach (Employee emp in employees)
                      {
                      string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
                      emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
                      string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
                      string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
                      EmployeeDataGridView.Rows.Add(row);
                      }
                      }

                      private void FTEmployeeButton_Click(object sender, EventArgs e)
                      {
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for FT Employees
                      var FTEmp = from emp in employees
                      where emp.GetTaxForm == "W2"
                      select emp;

                      //Display into DataGridView
                      FillEmployeeDataGrid(FTEmp.ToList());
                      }

                      private void ContractEmployeeButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for Contract Employees
                      var contractEmp = from emp in employees
                      where emp.GetTaxForm == "1099"
                      select emp;
                      //Display into DataGridView
                      FillEmployeeDataGrid(contractEmp.ToList());
                      }

                      //Method to determine developer type
                      string typeName;
                      public string SetDevType(int id)
                      {
                      if (id == 1)
                      {
                      typeName = "Object-Oriented";
                      }
                      else if (id == 2)
                      {
                      typeName = "Scripts";
                      }
                      else { typeName = "Unknown"; }
                      return typeName;
                      }

                      public double AnnualPay(double amount) => 12 * amount;





                      share|improve this answer










                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      Please check below refactoring code.



                      I have check the code and refactoring code with common method. So, if you have change common method then effect to all place. No need to change every place where you write same code or same logic.



                      private void LoadAllButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      FillEmployeeDataGrid(employees);
                      }

                      private List<Employee> ReadJsonData()
                      {
                      string JSONstring = File.ReadAllText("JSON.json");
                      List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);
                      return employees;
                      }

                      private void FillEmployeeDataGrid(List<Employee> employees)
                      {
                      EmployeeDataGridView.Rows.Clear();
                      //Display into DataGridView
                      foreach (Employee emp in employees)
                      {
                      string row = { emp.Name, emp.Zip, emp.Age.ToString(), string.Format("{0:C}", emp.Pay),
                      emp.DepartmentId.ToString(), SetDevType(emp.DepartmentId),
                      string.Format("{0:C}", emp.CalculateTax(emp.Pay)),
                      string.Format("{0:C}", AnnualPay(emp.Pay) - emp.CalculateTax(emp.Pay))};
                      EmployeeDataGridView.Rows.Add(row);
                      }
                      }

                      private void FTEmployeeButton_Click(object sender, EventArgs e)
                      {
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for FT Employees
                      var FTEmp = from emp in employees
                      where emp.GetTaxForm == "W2"
                      select emp;

                      //Display into DataGridView
                      FillEmployeeDataGrid(FTEmp.ToList());
                      }

                      private void ContractEmployeeButton_Click(object sender, EventArgs e)
                      {
                      //Read from JSON file
                      List<Employee> employees = ReadJsonData();

                      //LINQ Query for Contract Employees
                      var contractEmp = from emp in employees
                      where emp.GetTaxForm == "1099"
                      select emp;
                      //Display into DataGridView
                      FillEmployeeDataGrid(contractEmp.ToList());
                      }

                      //Method to determine developer type
                      string typeName;
                      public string SetDevType(int id)
                      {
                      if (id == 1)
                      {
                      typeName = "Object-Oriented";
                      }
                      else if (id == 2)
                      {
                      typeName = "Scripts";
                      }
                      else { typeName = "Unknown"; }
                      return typeName;
                      }

                      public double AnnualPay(double amount) => 12 * amount;






                      share|improve this answer










                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer








                      edited 22 hours ago





















                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered 23 hours ago









                      user2156791

                      11




                      11




                      New contributor




                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      user2156791 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.








                      • 1




                        Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                        – t3chb0t
                        21 hours ago
















                      • 1




                        Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                        – t3chb0t
                        21 hours ago










                      1




                      1




                      Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                      – t3chb0t
                      21 hours ago






                      Please take a look at the other reviews to see how to write them. Simply posting changed code isn't enough. You need to explain the changes you've made, or point things that can be improved in OP's code etc.
                      – t3chb0t
                      21 hours ago












                      Christopher Wood is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Christopher Wood is a new contributor. Be nice, and check out our Code of Conduct.













                      Christopher Wood is a new contributor. Be nice, and check out our Code of Conduct.












                      Christopher Wood is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208027%2fusing-json-file-for-employee-pay-data%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin