尝试使用 C# 开发 HR 入职控制台应用程序
Attempt on an HR onboarding console application using C#
我不太确定如何完成这个,因为这是我第一次尝试使用 C# 读写文本文件
我想用我的控制台应用程序做的是:
I) 从 HR 的文本文件中读取新员工记录列表,masterlist.txt。这
HRMasterlist.txt 文件包含新员工记录的完整详细信息。
II) 生成txt文件,存储不同部门所需的新员工记录列表。
每个部门需要的新员工信息不一样:
公司行政部门:全名、职务、部门
采购部门:称呼、全名、手机号、职务、部门
IT 部门:Nric、全名、开始日期、部门、手机号
这是我目前所做的:
Employee.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
namespace SectionA
{
public class Employee
{
public Employee(string nric, string fullname, string salutation, DateTime startdate, string designation, string department, string mobileno, string hiretype, double salary)
{
this.Nric = nric;
this.FullName = fullname;
this.Salutation = salutation;
this.StartDate = startdate;
this.Designation = designation;
this.Department = department;
this.MobileNo = mobileno;
this.HireType = hiretype;
this.Salary = salary;
this.MonthlyPayout = 0.0;
}
public string Nric { get; set; }
public string FullName { get; set; }
public string Salutation { get; set; }
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
public string HireType { get; set; }
public double Salary {get; set;}
public double MonthlyPayout {get; set;}
public string forCorpAdmin(Employee that)
{
return //add code;
}
public string forProcurement()
{
return //add code;
}
public string forITDept()
{
return //add code;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace SectionA
{
public delegate void GenerateTxtFile();
public class Program
{
static void Main(string[] args)
{
List<Employee> emps= new List<Employee>()
emps = readHRMasterList();
generateInfoForCorpAdmin(emps);
generateInfoForProcurement(emps);
enerateInfoForITDepartment(emps)
//when above can work, do delegate
}
public static List<Employee> readHRMasterList(List<Employee> emps)
{
string path = @"../HRMasterlist.txt";
//add code to read the HRMasterlist.txt
// use each row of data to create a employee object
//store each object into the employee before you return to main()
return emps;
}
public static void generateInfoForCorpAdmin(List<Employee> emps)
{
//• CorporateAdmin: FullName, Designation, Department
//open a new file "CorporateAdmin.txt" and store the above format into the file
//how to get the data frome the employee list pass in
}
public static void generateInfoForProcurement(List<Employee> emps)
{
//• Procurement: Salutation, FullName, MobileNo, Designation, Department
//add code
}
public static void generateInfoForITDepartment()
{
//• ITDepartment: Nric, FullName, StartDate, Department, MobileNo
//add code
}
}
}
最后,HRMasterList.txt
S1234567A|Jan Lee|Ms|05/10/1990|Software Architect|IT Department|98785432|PartTime|3500
S1234567B|Feb Tan|Mr|10/12/1991|Corporate Recruiter|HR Corporate Admin|98766432|PartTime|1500
S1234567C|Mark Lim|Mr|15/07/1992|Benefit Specialist|HR Corporate Admin|98265432|PartTime|2900
S1234567D|Apr Tan|Ms|20/01/1996|Payroll Administrator|HR Corporate Admin|91765432|FullTime|1600
S1234567E|May Ng|Ms|25/05/1994|Training Coordinator|HR Corporate Admin|98767432|Hourly|1200
S1234567F|Juno Chua|Mr|30/09/1997|Compensation Manager|HR Corporate Admin|98760432|FullTime|4000
S1234567G|Jul Wang|Mr|02/03/1996|Technical Recruiter|HR Corporate Admin|98765932|PartTime|1200
S1234567H|Aug Lau|Mr|04/11/1997|HR Consultant|HR Corporate Admin|98765032|PartTime|2000
S1234567I|Sep Yeo|Mr|06/08/1998|Software Engineer|IT Department|98765434|FullTime|3000
S1234567J|Octo Chua|Mr|08/04/1999|Technical Lead|IT Department|92765432|FullTime|4000
S1234567K|Nova Ng|Ms|10/03/1980|Team Lead|IT Department|90760432|FullTime|3000
S1234567L|Dessy Chow|Ms|05/01/1998|Project Lead|IT Department|95760432|FullTime|3500
S1234567M|Jack Mar|Mr|14/06/1999|Project Manager|IT Department|98766632|FullTime|4500
S1234567N|Anne Chan|Ms|16/04/1983|Procurement Assistant|Procurement Department|90065432|Hourly|1000
S1234567O|Claire Tan|Ms|17/08/1984|Procurement Officer|Procurement Department|98760032|FullTime|1000
S1234567P|Jade Lau|Ms|18/10/1996|Purchasing Associate|Procurement Department|91711432|FullTime|1100
S1234567Q|Wyne Yeo|Ms|19/08/1986|Purchasing Clerk|Procurement Department|98733433|Hourly|1200
S1234567R|Craig Ng|Mr|23/11/1987|Facilities Manager|Procurement Department|90088432|PartTime|2000
S1234567S|Hugh Chan|Mr|25/05/1988|Operations Manager|Procurement Department|88765432|FullTime|2500
S1234567T|Kent Teo|Mr|27/11/1989|Store Manager|Procurement Department|80765432|FullTime|2000
S1234567U|Sam Goh|Mr|29/03/1990|Production Planner|Procurement Department|98765321|Hourly|1500
S1234567V|Zack Aw|Mr|13/12/1991|Warehouse Clerk|Procurement Department|98321432|Hourly|1000
S1234567W|Ava Lim|Ms|07/11/1992|Procurement Specialist|Procurement Department|91235432|PartTime|2800
S1234567X|Eva Chow|Ms|09/12/1997|Materials Director|Procurement Department|92225432|FullTime|6500
S1234567Y|Lea Law|Ms|11/07/1994|Corporate Recruiter|HR Corporate Admin|94445432|PartTime|1600
S1234567Z|Ian Neo|Ms|21/09/1995|HR Coordinator|HR Corporate Admin|90005432|Hourly|1200
class Program
{
static void Main(string[] args)
{
//read all employees from files
var employees = ReadEmployes(@"filepathhere");
//store for the different departments
var coprateAdmins = new List<string>();
var procurementDept = new List<string>();
var itDept = new List<string>();
foreach (var employee in employees)
{
//classify employee here with switch case on department
switch (employee.Department)
{
case "IT Department":
itDept.Add(ToString(employee));
break;
case "HR Corporate Admin":
coprateAdmins.Add(ToString(employee));
break;
case "Procurement Department":
procurementDept.Add(ToString(employee));
break;
default:
break;
}
}
//writing files stage feel free to rename as accordingly
WriteFile("IT.txt", itDept);
WriteFile("procurement.txt", procurementDept);
WriteFile("coporateAdmins.txt", coprateAdmins);
}
/// <summary>
/// helper function to write files given names
/// </summary>
/// <param name="name"></param>
/// <param name="employees"></param>
private static void WriteFile(string name, List<string> employees)
{
File.WriteAllLines(name, employees);
}
/// <summary>
/// helper function to read employees from a file
/// </summary>
/// <param name="path">path of the text file</param>
/// <returns></returns>
private static List<Employee> ReadEmployes(string path)
{
var employees = new List<Employee>();
//the employees will be read separated by new lines
var content = File.ReadAllText(path);
//split by new line into an array of employee information in text
var lines = content.Split('\n');
//map each employee with their properies assumes all records will have the same order in columns
foreach (var line in lines)
{
var properties = line.Split('|');
employees.Add(new Employee
{
Nric = properties[0],
FullName = properties[1],
Salutation = properties[2],
StartDate = DateTime.ParseExact(properties[3], "dd/MM/yyyy", CultureInfo.InvariantCulture),
Designation = properties[4],
Department = properties[5],
MobileNo = properties[6],
HireType = properties[7],
Salary = double.Parse(properties[8])
});
}
return employees;
}
/// <summary>
/// helper function that uses c# reflection to get an employees properties then map to a string format similar to your input file
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
public static string ToString<T>(T instance)
{
var builder = new StringBuilder();
foreach (PropertyInfo pi in instance.GetType().GetProperties())
{
if (pi.GetValue(instance) != null)
{
builder.Append(pi.GetValue(instance));
builder.Append('|');
}
}
return builder.ToString();
}
}
/// <summary>
/// employee details
/// </summary>
public class Employee
{
public string Nric { get; set; }
public string FullName { get; set; }
public string Salutation { get; set; }
public DateTime StartDate { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
public string HireType { get; set; }
public double Salary { get; set; }
public double MonthlyPayout { get; set; }
}
/// <summary>
/// coroporate admin model
/// </summary>
public class CorporateAdmin
{
public string FullName { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
}
/// <summary>
/// procurement model
/// </summary>
public class ProcurementDepartment
{
public string Salutation { get; set; }
public string FullName { get; set; }
public string MobileNo { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
}
/// <summary>
/// it deparment model
/// </summary>
public class ITDepartment
{
public string Nric { get; set; }
public string FullName { get; set; }
public DateTime StartDate { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
}
}
我不太确定如何完成这个,因为这是我第一次尝试使用 C# 读写文本文件
我想用我的控制台应用程序做的是:
I) 从 HR 的文本文件中读取新员工记录列表,masterlist.txt。这 HRMasterlist.txt 文件包含新员工记录的完整详细信息。
II) 生成txt文件,存储不同部门所需的新员工记录列表。
每个部门需要的新员工信息不一样:
公司行政部门:全名、职务、部门
采购部门:称呼、全名、手机号、职务、部门
IT 部门:Nric、全名、开始日期、部门、手机号
这是我目前所做的:
Employee.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
namespace SectionA
{
public class Employee
{
public Employee(string nric, string fullname, string salutation, DateTime startdate, string designation, string department, string mobileno, string hiretype, double salary)
{
this.Nric = nric;
this.FullName = fullname;
this.Salutation = salutation;
this.StartDate = startdate;
this.Designation = designation;
this.Department = department;
this.MobileNo = mobileno;
this.HireType = hiretype;
this.Salary = salary;
this.MonthlyPayout = 0.0;
}
public string Nric { get; set; }
public string FullName { get; set; }
public string Salutation { get; set; }
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
public string HireType { get; set; }
public double Salary {get; set;}
public double MonthlyPayout {get; set;}
public string forCorpAdmin(Employee that)
{
return //add code;
}
public string forProcurement()
{
return //add code;
}
public string forITDept()
{
return //add code;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace SectionA
{
public delegate void GenerateTxtFile();
public class Program
{
static void Main(string[] args)
{
List<Employee> emps= new List<Employee>()
emps = readHRMasterList();
generateInfoForCorpAdmin(emps);
generateInfoForProcurement(emps);
enerateInfoForITDepartment(emps)
//when above can work, do delegate
}
public static List<Employee> readHRMasterList(List<Employee> emps)
{
string path = @"../HRMasterlist.txt";
//add code to read the HRMasterlist.txt
// use each row of data to create a employee object
//store each object into the employee before you return to main()
return emps;
}
public static void generateInfoForCorpAdmin(List<Employee> emps)
{
//• CorporateAdmin: FullName, Designation, Department
//open a new file "CorporateAdmin.txt" and store the above format into the file
//how to get the data frome the employee list pass in
}
public static void generateInfoForProcurement(List<Employee> emps)
{
//• Procurement: Salutation, FullName, MobileNo, Designation, Department
//add code
}
public static void generateInfoForITDepartment()
{
//• ITDepartment: Nric, FullName, StartDate, Department, MobileNo
//add code
}
}
}
最后,HRMasterList.txt
S1234567A|Jan Lee|Ms|05/10/1990|Software Architect|IT Department|98785432|PartTime|3500
S1234567B|Feb Tan|Mr|10/12/1991|Corporate Recruiter|HR Corporate Admin|98766432|PartTime|1500
S1234567C|Mark Lim|Mr|15/07/1992|Benefit Specialist|HR Corporate Admin|98265432|PartTime|2900
S1234567D|Apr Tan|Ms|20/01/1996|Payroll Administrator|HR Corporate Admin|91765432|FullTime|1600
S1234567E|May Ng|Ms|25/05/1994|Training Coordinator|HR Corporate Admin|98767432|Hourly|1200
S1234567F|Juno Chua|Mr|30/09/1997|Compensation Manager|HR Corporate Admin|98760432|FullTime|4000
S1234567G|Jul Wang|Mr|02/03/1996|Technical Recruiter|HR Corporate Admin|98765932|PartTime|1200
S1234567H|Aug Lau|Mr|04/11/1997|HR Consultant|HR Corporate Admin|98765032|PartTime|2000
S1234567I|Sep Yeo|Mr|06/08/1998|Software Engineer|IT Department|98765434|FullTime|3000
S1234567J|Octo Chua|Mr|08/04/1999|Technical Lead|IT Department|92765432|FullTime|4000
S1234567K|Nova Ng|Ms|10/03/1980|Team Lead|IT Department|90760432|FullTime|3000
S1234567L|Dessy Chow|Ms|05/01/1998|Project Lead|IT Department|95760432|FullTime|3500
S1234567M|Jack Mar|Mr|14/06/1999|Project Manager|IT Department|98766632|FullTime|4500
S1234567N|Anne Chan|Ms|16/04/1983|Procurement Assistant|Procurement Department|90065432|Hourly|1000
S1234567O|Claire Tan|Ms|17/08/1984|Procurement Officer|Procurement Department|98760032|FullTime|1000
S1234567P|Jade Lau|Ms|18/10/1996|Purchasing Associate|Procurement Department|91711432|FullTime|1100
S1234567Q|Wyne Yeo|Ms|19/08/1986|Purchasing Clerk|Procurement Department|98733433|Hourly|1200
S1234567R|Craig Ng|Mr|23/11/1987|Facilities Manager|Procurement Department|90088432|PartTime|2000
S1234567S|Hugh Chan|Mr|25/05/1988|Operations Manager|Procurement Department|88765432|FullTime|2500
S1234567T|Kent Teo|Mr|27/11/1989|Store Manager|Procurement Department|80765432|FullTime|2000
S1234567U|Sam Goh|Mr|29/03/1990|Production Planner|Procurement Department|98765321|Hourly|1500
S1234567V|Zack Aw|Mr|13/12/1991|Warehouse Clerk|Procurement Department|98321432|Hourly|1000
S1234567W|Ava Lim|Ms|07/11/1992|Procurement Specialist|Procurement Department|91235432|PartTime|2800
S1234567X|Eva Chow|Ms|09/12/1997|Materials Director|Procurement Department|92225432|FullTime|6500
S1234567Y|Lea Law|Ms|11/07/1994|Corporate Recruiter|HR Corporate Admin|94445432|PartTime|1600
S1234567Z|Ian Neo|Ms|21/09/1995|HR Coordinator|HR Corporate Admin|90005432|Hourly|1200
class Program
{
static void Main(string[] args)
{
//read all employees from files
var employees = ReadEmployes(@"filepathhere");
//store for the different departments
var coprateAdmins = new List<string>();
var procurementDept = new List<string>();
var itDept = new List<string>();
foreach (var employee in employees)
{
//classify employee here with switch case on department
switch (employee.Department)
{
case "IT Department":
itDept.Add(ToString(employee));
break;
case "HR Corporate Admin":
coprateAdmins.Add(ToString(employee));
break;
case "Procurement Department":
procurementDept.Add(ToString(employee));
break;
default:
break;
}
}
//writing files stage feel free to rename as accordingly
WriteFile("IT.txt", itDept);
WriteFile("procurement.txt", procurementDept);
WriteFile("coporateAdmins.txt", coprateAdmins);
}
/// <summary>
/// helper function to write files given names
/// </summary>
/// <param name="name"></param>
/// <param name="employees"></param>
private static void WriteFile(string name, List<string> employees)
{
File.WriteAllLines(name, employees);
}
/// <summary>
/// helper function to read employees from a file
/// </summary>
/// <param name="path">path of the text file</param>
/// <returns></returns>
private static List<Employee> ReadEmployes(string path)
{
var employees = new List<Employee>();
//the employees will be read separated by new lines
var content = File.ReadAllText(path);
//split by new line into an array of employee information in text
var lines = content.Split('\n');
//map each employee with their properies assumes all records will have the same order in columns
foreach (var line in lines)
{
var properties = line.Split('|');
employees.Add(new Employee
{
Nric = properties[0],
FullName = properties[1],
Salutation = properties[2],
StartDate = DateTime.ParseExact(properties[3], "dd/MM/yyyy", CultureInfo.InvariantCulture),
Designation = properties[4],
Department = properties[5],
MobileNo = properties[6],
HireType = properties[7],
Salary = double.Parse(properties[8])
});
}
return employees;
}
/// <summary>
/// helper function that uses c# reflection to get an employees properties then map to a string format similar to your input file
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
public static string ToString<T>(T instance)
{
var builder = new StringBuilder();
foreach (PropertyInfo pi in instance.GetType().GetProperties())
{
if (pi.GetValue(instance) != null)
{
builder.Append(pi.GetValue(instance));
builder.Append('|');
}
}
return builder.ToString();
}
}
/// <summary>
/// employee details
/// </summary>
public class Employee
{
public string Nric { get; set; }
public string FullName { get; set; }
public string Salutation { get; set; }
public DateTime StartDate { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
public string HireType { get; set; }
public double Salary { get; set; }
public double MonthlyPayout { get; set; }
}
/// <summary>
/// coroporate admin model
/// </summary>
public class CorporateAdmin
{
public string FullName { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
}
/// <summary>
/// procurement model
/// </summary>
public class ProcurementDepartment
{
public string Salutation { get; set; }
public string FullName { get; set; }
public string MobileNo { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
}
/// <summary>
/// it deparment model
/// </summary>
public class ITDepartment
{
public string Nric { get; set; }
public string FullName { get; set; }
public DateTime StartDate { get; set; }
public string Department { get; set; }
public string MobileNo { get; set; }
}
}