这是家庭作业,所以如果您不能帮助我,我可以理解。

一切都按预期工作。但是我觉得我可以更好地实现事情或编写更简洁的代码。我希望该按钮仅调用读取JSON文件,运行查询并显示正确显示的方法,而无需像我一样复制代码。

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);
}

}

最佳答案

我会说这一行:

List<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);


应该

IList<Employee> employees = JsonConvert.DeserializeObject<List<Employee>>(JSONstring);


对于this reason

根据StyleCop的说法,局部变量,私有字段和参数应为小写驼峰式:

string jsonString = File.ReadAllText("JSON.json");


另一件事是不要重复自己(DRY)。可以将以下几行重构为单独的方法:

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;


SetDevType中,您可以完全使用switch / case,它的性能更好。

string typeName; // why do we need this?
private string SetDevType(int id)
{
    string typeName = string.Empty;
    switch(id){
        case 1: typeName = "Something"; break;
        default: typeName = "Unknown";
    }
    return typeName;
}


班上的某些成员不需要公开,例如

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




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


并且Employee类(Model类/ POCO)中也以某种方式存在此方法。 POCO类通常不包含非属性(尽管有时包括ToString和一个空的构造函数)。

关于c# - C#更好地实现JSON文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53382148/

10-12 22:20