如果对你有启发,可以点点采纳,谢谢
保存文件如图:
namespace ConsoleApp1
{
internal class Program
{
static List<Staff> StaffList = new List<Staff>();
static void Main(string[] args)
{
ImportStaffs();
while (true)
{
switch (MainMenu())
{
case 1:
CalculateSalary();
break;
case 2:
DisplayStaffInformation();
break;
case 3:
FindStaff();
break;
case 4:
SaveToFile();
break;
case 5:
{
Console.Clear();
Console.WriteLine("(5) 退出");
Console.ReadKey();
return;
}
default:
break;
}
}
}
private static void FindStaff()
{
Console.Clear();
Console.WriteLine("(3) 输入一个员工编号能查找员工信息");
Console.Write("输入一个员工编号:");
if (int.TryParse(Console.ReadLine(), out var staffID))
{
Console.WriteLine(StaffList.Find(s => s.ID == staffID));
}
Console.ReadKey();
}
private static void DisplayStaffInformation()
{
Console.Clear();
Console.WriteLine("(2) 显示员工信息");
foreach (var staff in StaffList)
{
Console.WriteLine(staff);
}
Console.ReadKey();
}
private static void SaveToFile()
{
Console.Clear();
Console.WriteLine("(4) 保存信息到.txt文档");
var staffInfoFileNew = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StaffInfoNew.txt");
if (File.Exists(staffInfoFileNew))
{
File.Delete(staffInfoFileNew);
}
using var stream = new StreamWriter(staffInfoFileNew, true);
foreach (var staff in StaffList)
{
stream.Write(staff);
stream.Write(Environment.NewLine);
}
Console.WriteLine("保存成功");
Console.ReadKey();
}
private static void ImportStaffs()
{
var staffInfoFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StaffInfo.txt");
if (File.Exists(staffInfoFile))
{
foreach (var staffLine in File.ReadAllLines(staffInfoFile).Skip(1))
{
var staffInfo = staffLine.Split(',');
var id = int.Parse(staffInfo[0]);
var firstName = staffInfo[1];
var lastName = staffInfo[2];
var annualSalary = double.Parse(staffInfo[3].Trim('%'));
var pensionRatio = double.Parse(staffInfo[4].Trim('%')) / 100;
StaffList.Add(Staff.EnrollNew(id, firstName, lastName, annualSalary, pensionRatio));
}
}
}
private static void CalculateSalary()
{
Console.Clear();
Console.WriteLine("(1) 计算每两周工资");
Console.WriteLine("每两周工资计算完成");
Console.ReadKey();
}
private static int MainMenu()
{
Console.Title = "员工工资计算";
while (true)
{
Console.Clear();
Console.WriteLine("(1) 计算每两周工资");
Console.WriteLine("(2) 显示员工信息");
Console.WriteLine("(3) 输入一个员工编号能查找员工信息");
Console.WriteLine("(4) 保存信息到.txt文档");
Console.WriteLine("(5) 退出");
Console.WriteLine();
if (int.TryParse(Console.ReadLine(), out var functionID))
{
if (functionID >= 1 && functionID <= 5)
{
return functionID;
}
}
}
}
}
}
namespace ConsoleApp1
{
internal class Staff
{
private double PersonalIncomeTax
{
get
{
double personalIncomeTax = default;
if (AnnualSalary < 48000)
{
personalIncomeTax = TwoWeeksSalary * 17.5 / 100;
}
else if (AnnualSalary >= 48000 && AnnualSalary < 70000)
{
personalIncomeTax = TwoWeeksSalary * 30 / 100;
}
else if (AnnualSalary >= 70000)
{
personalIncomeTax = TwoWeeksSalary * 33 / 100;
}
return personalIncomeTax;
}
}
private double Pension => TwoWeeksSalary * PensionRatio;
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double AnnualSalary { get; set; }
public double HourlySalary => AnnualSalary / 52 / 40;
public double PaidSalary => TwoWeeksSalary - Pension - PersonalIncomeTax;
public double PensionRatio { get; set; }
public double TwoWeeksSalary => HourlySalary * 40 * 2;
private Staff() { }
public static Staff EnrollNew(int id, string firstName, string lastName, double annualSalary, double pensionRatio)
{
var newStaff = new Staff
{
ID = id,
FirstName = firstName,
LastName = lastName,
AnnualSalary = annualSalary,
PensionRatio = pensionRatio
};
return newStaff;
}
public override string ToString()
{
return $"ID: {ID}{Environment.NewLine}" +
$"FirstName: {FirstName}{Environment.NewLine}" +
$"LastName: {LastName}{Environment.NewLine}" +
$"AnnualSalary: {AnnualSalary}{Environment.NewLine}" +
$"HourlySalary: {HourlySalary}{Environment.NewLine}" +
$"PaidSalary: {PaidSalary}{Environment.NewLine}" +
$"PensionRatio: {PensionRatio}{Environment.NewLine}" +
$"TwoWeeksSalary: {TwoWeeksSalary}{Environment.NewLine}" +
$"PersonalIncomeTax: {PersonalIncomeTax}{Environment.NewLine}" +
$"Pension: {Pension}{Environment.NewLine}";
}
}
}