今天上午进行了公司的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、通过正则表达式验证用户输入信息。

C#中一道关于员工信息系统的题(主要考察LinQ和正则表达式验证)的更多相关文章

  1. C#中一道关于多线程的编程题

    题目的意思是这样的:让两个线程A和B将自己的ID轮番写入一个文件中,每个线程重复十次写入后执行一个回调函数,说“I'm OK”,就这样.我是一名QA,不是开发,出于兴趣报考了公司的C#课程考试,多线程 ...

  2. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  3. python--简易员工信息系统编写

    补充内容:eval 将字符串变成变量名locals   看输入的是否是字典中的一个keyfunc.__name____怎么看变量名的数据类型斐波那契数列 li=[1,1] while li[-1]&l ...

  4. ACM_一道耗时间的水题

    一道耗时间的水题 Time Limit: 2000/1000ms (Java/Others) Problem Description: Do you know how to read the phon ...

  5. 一道有趣的for循环题

    一道有趣的for循环题 今天在复习js基础知识时发现了一个for循环的题,第一眼看到直接懵逼了,没想到for循环竟然还可以这样玩?涨姿势了. 题目是这样的 for(i=0, j=0; i<10, ...

  6. SQL-50 将employees表中的所有员工的last_name和first_name通过(')连接起来

    题目描述 将employees表中的所有员工的last_name和first_name通过(')连接起来.CREATE TABLE `employees` (`emp_no` int(11) NOT ...

  7. 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)

    Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...

  8. 检查字符串长度 检查字符串是否为空 用正则表达式验证出版物的ISBN号 用正则表达式验证邮证编码 验证字符串中是否含有汉字

    <?php /** * 常用的正则表达式来验证信息.如:网址 邮箱 手机号等 */ class check { /** * 正则表达式验证email格式 * * @param string $s ...

  9. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

随机推荐

  1. ActiveMq C#客户端 消息队列的使用(存和取)

    1.准备工具 VS2013Apache.NMS.ActiveMQ-1.7.2-bin.zipapache-activemq-5.14.0-bin.zip 2.开始项目 VS2013新建一个C#控制台应 ...

  2. JDBC实例--JDBC连接池技术解密,连接池对我们不再陌生

    一.为什么我们要用连接池技术? 前面的数据库连接的建立及关闭资源的方法有些缺陷.统舱传统数据库访问方式:一次数据库访问对应一个物理连接,每次操作数据库都要打开.关闭该物理连接, 系统性能严重受损. 解 ...

  3. hdu 4031(树状数组+辅助数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    ...

  4. js getAttribute getAttributeNode

    getAttribute():返回属性值,是一个文本字符串 getAttributeNode("属性名"):返回属性节点,是一个对象 <p id="bj" ...

  5. 搭建前端vue环境,安装vue-cli遇到Please try running this command again as root/Administrator的解决方案

    最近在搭前端环境,装完node.js之后,准备安装vue工程的初始化工具时(npm install -g vue-cli),遇到这个坑: 大体的意思就是权限问题,导致/usr/local/lib/no ...

  6. IOS 内存优化和调试技巧

    基础部分 1: 图片内存大小小结 a: 图片:是占用内存的大户,尤其是手机游戏图片资源众多.对图片资源在内存中占用量的计算成为J2ME游戏开发者的经常性工作,CoCoMo来解释一下如何计算图片在内存中 ...

  7. HOJ 1402 整数划分

    HOJ1402 整数划分 http://acm.hit.edu.cn/hoj/problem/view?id=1402 [题目描述] 整数划分是一个经典的问题.希望这道题会对你的组合数学的解题能力有所 ...

  8. java MessageFormat.format

    sql 语句中格式化,如果加入{}占位符,要替代的是整形变量,而恰好这个整形变量的位数超过4位, MessageFormat.format 会在这个整形变量中默认每隔三位加一个逗号,类似这样:1000 ...

  9. Android Fragment之间传递List数据

    要说的是在两个Fragment之间传递List数据,比如有个List<User>,以及传递字符串数据,比如testId,该如何从FragmentA传递到FragmentB呢? 下面这个例子 ...

  10. ASP.NET CORE 学习之自定义异常处理

    为什么异常处理选择中间件? 传统的ASP.NET可以采用异常过滤器的方式处理异常,在ASP.NET CORE中,是以多个中间件连接而成的管道形式处理请求的,不过常用的五大过滤器得以保留,同样可以采用异 ...