今天上午進行了公司的C# C level考核,最后一道編程題是關於員工信息系統的,題目的要求大概是這樣的:1、要可以保存員工信息(XXXXX),並且要用正則表達式對輸入信息進行驗證;2、要可以對員工信息進行查詢(根據員工號和部門兩種方式)。
我是通過窗體程序實現的,窗體設計如下,一共三個,分別是主窗體界面、添加員工信息窗體和查找員工信息窗體:
程序如下:
主窗體——
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; namespace WorkmatesInfoSystem { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { AddInfo addInfoForm = new AddInfo(); addInfoForm.ShowDialog(); } private void button2_Click(object sender, EventArgs e) { SearchInfo searchInfoForm = new SearchInfo(); searchInfoForm.ShowDialog(); } } }
添加員工信息窗體:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Xml; using System.Xml.Linq; using System.IO; namespace WorkmatesInfoSystem { public partial class AddInfo : Form { public AddInfo() { InitializeComponent(); } private void textBox1_Click(object sender, EventArgs e) { if(WorkerNumber.Text == "The worker's nubmer must be a number(not a string or etc).") { WorkerNumber.Text = ""; } } private void textBox2_Click(object sender, EventArgs e) { if (WorkerName.Text == "Herry(For example)") { WorkerName.Text = ""; } } private void textBox3_Click(object sender, EventArgs e) { if (WorkerEmail.Text == "The format should like \"tylan@Avpoint.com\"") { WorkerEmail.Text = ""; } } private void textBox4_Click(object sender, EventArgs e) { if (WorkerDepartment.Text == "CC_CIQA(For example)") { WorkerDepartment.Text = ""; } } private void button1_Click(object sender, EventArgs e) { //Check the format of the worker's number. if (Regex.IsMatch(WorkerNumber.Text, "^[0-9]*$") == false) { MessageBox.Show("Worker's number is in a wrong format, please type in again."); } //Check the Email's format of the worker. else if (Regex.IsMatch(WorkerEmail.Text,@"^\w+@\w+$") == false) { MessageBox.Show("Worker's Email is in a wrong format, please type in again."); } //Check the end format of the Email Address. else if (Regex.IsMatch(EmailEndFormat.Text, "^(com|net)$") == false) { MessageBox.Show("The end format of the Email is wrong, please type in again. You can type in 'com' or 'net'."); } //Add the worker's info into the xml. else { saveToXml(SavePath.Text.ToString()); MessageBox.Show("Save the worker's info successfully."); } } //Save to XML method. private void saveToXml(string xmlPath) { string filePath = xmlPath + "WorkersInfo.xml"; FileInfo file = new FileInfo(@filePath); if (file.Exists == false) { File.Create(@filePath).Close(); StreamWriter sw = new StreamWriter(@filePath); string content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<System xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SystemName>WorkersInfo</SystemName>" + "<Workers>" + "</Workers>" + "</System>"; sw.WriteLine(content); sw.Close(); } //Load XML file. XElement xe = XElement.Load(@filePath); //Create a node info. XElement Worker = new XElement("Worker", new XElement("WorkerNumber", WorkerNumber.Text), new XElement("WorkerName", WorkerName.Text), new XElement("WorkerEmail", WorkerEmail.Text + "." + EmailEndFormat.Text), new XElement("WorkerDepartment", WorkerDepartment.Text) ); //Add the node under the specific node. xe.Element("Workers").Add(Worker); //Save the XML file. xe.Save(filePath); } private void SavePath_Click(object sender, EventArgs e) { if(SavePath.Text==@"C:\Tylan\Workers(For example)") { SavePath.Text = ""; } } } }
查找員工信息窗體:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; namespace WorkmatesInfoSystem { public partial class SearchInfo : Form { public SearchInfo() { InitializeComponent(); } private void SearchDepartmentButton_Click(object sender, EventArgs e) { XDocument workersInfo = XDocument.Load(@SearchPath.Text.ToString()); var results = from worker in workersInfo.Element("System").Element("Workers").Elements() where worker.Element("WorkerDepartment").Value == SearchWorkerDepartment.Text.ToString() select worker; foreach (var result in results) { MessageBox.Show(result.Element("WorkerNumber").Value + "\n" + result.Element("WorkerName").Value + "\n" + result.Element("WorkerEmail").Value + "\n" + result.Element("WorkerDepartment").Value); } } private void SearchNumberButton_Click(object sender, EventArgs e) { XDocument workersInfo = XDocument.Load(@SearchPath.Text.ToString()); var results = from worker in workersInfo.Element("System").Element("Workers").Elements() where int.Parse(worker.Element("WorkerNumber").Value) == int.Parse(SearchWokerNumber.Text.ToString()) select worker; foreach (var result in results) { MessageBox.Show(result.Element("WorkerNumber").Value + "\n" + result.Element("WorkerName").Value + "\n" + result.Element("WorkerEmail").Value + "\n" + result.Element("WorkerDepartment").Value); } } } }
由於考試時間倉促,只是在功能上滿足了這道題的要求,匆匆收尾,還請大家只關注以下三點在實際應用中的實踐:
1、使用LinQ查詢員工信息;
2、添加員工信息到XML的過程;
3、通過正則表達式驗證用戶輸入信息。