我正在开发一个 Windows 窗体应用程序,其目的是计算和显示存储在文本文件中的工资统计信息。
我现在有一项任务有问题:计算当前平均工资最高的职业。
我已将每个薪水统计信息存储为 SalaryInformation 对象。我将向您展示 SalaryInformation 类的外观:
public sealed class SalaryInformation
{
private string profession;
private int yearOfEmployment;
private decimal startSalary;
private decimal currentSalary;
public string Profession
{
get { return profession; }
set { profession = value; }
}
public int YearOfEmployment
{
get { return yearOfEmployment; }
set { yearOfEmployment = value; }
}
public decimal StartSalary
{
get { return startSalary; }
set { startSalary = value; }
}
public decimal CurrentSalary
{
get { return currentSalary; }
set { currentSalary = value; }
}
public SalaryInformation()
{ }
public SalaryInformation(string p, int yoe, decimal startS, decimal currentS)
{
profession = p;
yearOfEmployment = yoe;
startSalary = startS;
currentSalary = currentS;
}
我想要做的是返回一个字符串。与最高平均 currentSalary 关联的 SalaryInformation 对象的职业属性。请记住,有几个 SalaryInformation 对象具有共同的职业值(例如,三个 SalaryInformation 对象在财产专业上具有值“医生”)。
我从这种方法开始,我被困在这里:
public string GetHighestPaidProfession()
{
string highestPaidProfession = "";
//Gets all the salaryInformation objects and stores them in a list
List<SalaryInformation> allSalaries = new List<SalaryInformation>();
allSalaries = data.GetSalaryInformation();
//Right here I don't know how to do the rest from here.
//I realize that I have to calculate the average currentsalary from every
//SalaryInformation I got in the list allSalaries. But then I have to
//to get the actual profession which has the highest average currentsalary
//among them. It's right there I get stuck.
return highestPaidProfession;
}
如果您需要更多代码和详细信息,请告诉我,我会将其添加到此线程中。