定义一个员工的集合,对员工集合内的元素进行查询和删除。
实现员工的签到和签退,要求如下:
//A:每天只能签到一次
//B:签退前必须已经签到
//C:显示打卡记录 代码如下:
员工信息类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Employee
{ private int id; public int ID
{
get { return id; }
set { id = value; }
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private string sex; public string Sex
{
get { return sex; }
set { sex = value; }
}
private int age; public int Age
{
get { return age; }
set { age = value; }
} // A:实现新增员工(工号,年龄,姓名,性别) public Employee(int _id, int _age, string _name, string _sex)
{
this.Name = _name;
this.Age = _age;
this.Sex = _sex;
this.ID = _id;
} //B:展示员工信息, public void ShowMsg()
{ Console.WriteLine("工号:{0} 年龄:{1} 姓名:{2} 性别:{3}", this.ID, this.Age, this.Name, this.Sex);
}
}
}

公司信息类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Company
{
public List<Employee> list = new List<Employee>();
public Dictionary<string, List<KaoqinRecord>> dicKaoQinEachDay = new Dictionary<string, List<KaoqinRecord>>();
public void AddEmp(Employee sc)
{
list.Add(sc);
} public void DelEmp(int nID)
{
for (int i = ; i < list.Count; i++)
{
if (list[i].ID == nID)
{
list.RemoveAt(i);
}
}
} public void PrintAllEmp()
{
for (int i = ; i < list.Count; i++)
{
list[i].ShowMsg();
} } public Employee FindEmp(int nID)
{
for (int i = ; i < list.Count; i++)
{
if (list[i].ID == nID)
{
return list[i];
}
}
return null;
}
}
}

考勤记录类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class KaoqinRecord
{ public int nID;
public DateTime dtStart;
public DateTime dtEnd;
}
}

主程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Program
{
static Company c = new Company();
static void Main(string[] args)
{
//// 1、使用你所学的C#的容器类实现员工考勤信息管理, 
//实现以下功能:要求:使用泛型集合list的添加,查询和删除操作)
// A:实现新增员工(工号,年龄,姓名,性别)
List<Employee> list = new List<Employee>(); c.AddEmp(new Employee(, , "zhang3", "男"));
c.AddEmp(new Employee(, , "li4", "女"));
c.AddEmp(new Employee(, , "wang5", "男"));
c.AddEmp(new Employee(, , "zhao6", "女"));
c.AddEmp(new Employee(, , "qian7", "男"));
c.AddEmp(new Employee(, , "sun8", "女")); c.PrintAllEmp(); //C:根据工号删除员工信息 Console.WriteLine("请输入删除员工信息!工号:");
int numDel = Convert.ToInt32(Console.ReadLine()); c.DelEmp(numDel); c.PrintAllEmp(); //D根据员工工号查询员工信息
Console.WriteLine("请输入查询员工信息!工号:");
int numShu = Convert.ToInt32(Console.ReadLine()); Employee sc = c.FindEmp(numShu);
sc.ShowMsg(); while(true)
{
Console.WriteLine("员工考勤系统");
Console.WriteLine("0--------------exit");
Console.WriteLine("1--------------qiandao");
Console.WriteLine("2--------------qiantui");
Console.WriteLine("3--------------ShowKaoqin"); int n = Convert.ToInt32(Console.ReadLine());
if (n == )
break;
switch(n)
{
case :
{
Console.WriteLine("请输入要签到的员工号码");
int nEmpNo = Convert.ToInt32(Console.ReadLine());
qiandao(nEmpNo);
break;
}
case :
{
Console.WriteLine("请输入要签退的员工号码");
int nEmpNo = Convert.ToInt32(Console.ReadLine());
qiantui(nEmpNo);
break;
}
case :
{
Console.WriteLine("请输入考勤日期:");
ShowKaoqins();
break;
}
default:
break; }
} } public static void ShowKaoqins()
{
string strDate = System.DateTime.Now.Date.ToShortDateString();
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate];
Console.WriteLine("今天考勤的人数有:");
foreach(KaoqinRecord kr in lst)
{
int nId = kr.nID;
Employee sc = c.FindEmp(nId);
Console.WriteLine("{0} 今天签到时间为:{1} 签退时间为 {2}", sc.Name, kr.dtStart, kr.dtEnd);
}
} public static void qiantui(int nID)
{
Employee sc = c.FindEmp(nID);
string strDate = System.DateTime.Now.Date.ToShortDateString();
if (!c.dicKaoQinEachDay.Keys.Contains(strDate))//第一个签到的员工
{
Console.WriteLine("{0}今天没有签到", sc.Name);
return;
}
else
{
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate];
int nFindIndex = -;
int i = ;
foreach (KaoqinRecord kr in lst)
{
if (kr.nID == nID)
{
nFindIndex = i;
break;
}
i++;
} if(nFindIndex >= )
{
KaoqinRecord kr = lst[nFindIndex];
kr.dtEnd = System.DateTime.Now;
}
else
{
Console.WriteLine("{0}今天没有签到", sc.Name);
}
}
}
public static void qiandao(int nID)
{
Employee sc = c.FindEmp(nID);
string strDate = System.DateTime.Now.Date.ToShortDateString();
if(!c.dicKaoQinEachDay.Keys.Contains(strDate))//第一个签到的员工
{
KaoqinRecord kr = new KaoqinRecord();
kr.nID = sc.ID;
kr.dtStart = System.DateTime.Now;
List<KaoqinRecord> lst = new List<KaoqinRecord>();
lst.Add(kr);
c.dicKaoQinEachDay.Add(strDate, lst);
}
else
{
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate]; foreach(KaoqinRecord kr in lst)
{
if(kr.nID == nID)
{
Console.WriteLine("{0}今天已经签到了",sc.Name);
return;
}
} KaoqinRecord krNew = new KaoqinRecord();
krNew.nID = sc.ID;
krNew.dtStart = System.DateTime.Now;
lst.Add(krNew); } Console.WriteLine("{0}签到成功",sc.Name); }
}
}

java版

公司信息类

package com.company;

import java.text.SimpleDateFormat;
import java.util.*; /**
* Created by ttc on 2017/6/30.
*/
public class Company {
private List<String> lstEmps = new ArrayList<String>();
//key是签到日期
private Map<String,List<KaoqinRecord>> map = new HashMap<>(); public void qiandao()
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入要签到的员工姓名");
String strName = sc.nextLine();
if(!lstEmps.contains(strName))
{
System.out.println("不存在该员工");
return;
} Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss");
String strDate = sdf.format(date);
String strTime = sdfTime.format(date);
if(!map.containsKey(strDate))//第一个签到的员工
{
List<KaoqinRecord> lst = new ArrayList<>(); KaoqinRecord kq = new KaoqinRecord();
kq.setEmpName(strName);
kq.setTimeStart(strTime);
kq.setTimeEnd("");
lst.add(kq); map.put(strDate,lst);
System.out.println(strName+"签到成功");
}
else//说明之前,今天已经有人签过到了
{
List<KaoqinRecord> lst = map.get(strDate); //判断是否已经签到过
boolean bIsFind = false;//假设没找到,没签到过
for(KaoqinRecord qr : lst)
{
if(qr.getEmpName().equals(strName))
{
System.out.println("已经签过到了");
bIsFind = true;
break;
}
}
if(!bIsFind)
{
KaoqinRecord kq = new KaoqinRecord();
kq.setEmpName(strName);
kq.setTimeStart(strTime);
kq.setTimeEnd("");
lst.add(kq);
System.out.println(strName+"签到成功");
} } }
public void qiantui()
{
System.out.println("请输入要签退的员工姓名");
Scanner sc = new Scanner(System.in);
String strName = sc.nextLine();
if(!lstEmps.contains(strName))
{
System.out.println("不存在该员工");
return;
}
Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss");
String strDate = sdf.format(date);
String strTime = sdfTime.format(date); if(!map.containsKey(strDate))//今天一个人也没签到
{
System.out.println("你没有签到,不能签退");
}
else
{
List<KaoqinRecord> lst = map.get(strDate);
boolean bIsFind = false;//假设没找到
for(KaoqinRecord kr : lst)
{
if(kr.getEmpName().equals(strName))
{
kr.setTimeEnd(strTime);
bIsFind = true;
break;
}
} if(!bIsFind)//真的没找到,也即意味着没签到
{
System.out.println("你没有签到,不能签退");
}
else
{
System.out.println(strName+"签退成功");
}
}
}
public void showKaoqinInfo()
{
System.out.println("请输入要查找的日期(yyyyMMdd)");
Scanner sc = new Scanner(System.in);
String strDate = sc.nextLine();
List<KaoqinRecord> lst = map.get(strDate);
for(KaoqinRecord kr : lst)
{
System.out.println(kr.getEmpName() + "签到时间为" + kr.getTimeStart()
+ "签退时间为" + kr.getTimeEnd());
}
} public List<String> getLstEmps() {
return lstEmps;
} public void setLstEmps(List<String> lstEmps) {
this.lstEmps = lstEmps;
} public Map<String, List<KaoqinRecord>> getMap() {
return map;
} public void setMap(Map<String, List<KaoqinRecord>> map) {
this.map = map;
}
}

考勤记录类

package com.company;

import java.util.Date;

/**
* Created by ttc on 2017/6/30.
*/
//某一个员工,某天的的考勤情况
public class KaoqinRecord {
private String empName;
private String timeStart;
private String timeEnd; public String getTimeStart() {
return timeStart;
} public void setTimeStart(String timeStart) {
this.timeStart = timeStart;
} public String getTimeEnd() {
return timeEnd;
} public void setTimeEnd(String timeEnd) {
this.timeEnd = timeEnd;
} public String getEmpName() {
return empName;
} public void setEmpName(String empName) {
this.empName = empName;
} }

主程序类:

package com.company;

import java.util.Calendar;
import java.util.Scanner; public class Main { public static void main(String[] args) {
// write your code here
//
// 20170630---zhangsan 9:00 18:00,lisi 9:00 18:00,wangwu 10:00 21:00
// 20170629---zhangsan 9:00 18:00,lisi 9:00 ,wangwu 10:00 21:00
// Map<String,List<KaoqinRecord>>
//
// map.put("20170630",[{name:hanwei,startTime:8:00,endTime:00},xiuwei]) Company company = new Company(); company.getLstEmps().add("马云");
company.getLstEmps().add("李连杰");
company.getLstEmps().add("赵本山"); Scanner sc = new Scanner(System.in);
int cmd = -1;
Calendar calendar = Calendar.getInstance();
String str = calendar.toString();
do
{
System.out.println("员工考勤系统");
System.out.println("0------exit");
System.out.println("1------签到");
System.out.println("2------签退");
System.out.println("3------显示考勤信息"); cmd = sc.nextInt();
switch (cmd)
{
case 1: {
company.qiandao();
break;
}
case 2: {
company.qiantui();
break;
}
case 3: {
company.showKaoqinInfo();
break;
}
default:
{
break;
}
} }while (cmd != 0); return;
}
}
 

一道综合练习题实践list及dictionary集合类的更多相关文章

  1. 一道C++练习题,替换一个字符串里所有实例

    做了一道C++练习题,替换一个字符串里面的所有实例. #include <iostream> #include <string> using namespace std; co ...

  2. CSS3背景 制作导航菜单综合练习题

    CSS3背景 制作导航菜单综合练习题 小伙伴们,根据所学知识,使用CSS3实现下图的导航菜单效果 任务 1.制作导航圆角 提示:使用border-radius实现圆角 2.制作导航立体风格 提示:使用 ...

  3. 从一道NOI练习题说递推和递归

    一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...

  4. 一道综合渗透题引发的updatexml()注入思考

    MYSQL数据库updatexml报错注入UPDATEXML (XML_document, XPath_string, new_value); 第一个参数:XML_document是String格式, ...

  5. 一道Python练习题

    有关字符串与编码的,转自廖雪峰Python教程. 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: # -*- co ...

  6. python全栈开发笔记---数据类型--综合练习题

    一.有两个列表 l1 = [11,22,33]       l2 = [22,33,44] a. 获取内容相同的元素列表 for item in l1: if item in l2: print(it ...

  7. ADO.Net 综合练习题

    题目: 第一部分: 新建一个数据库:ADO测试,包含下面两个数据表,使用代码创建,并保留创建的代码文本. 专业表Subject: 专业编号(SubjectCode):nvarchar类型,不能为空,主 ...

  8. 第2章 Python基础-字符编码&数据类型 综合 练习题

    1.转换 将字符串s = "alex"转换成列表 s = "alex" s_list = list(s) print(s_list) 将字符串s = " ...

  9. if else 选择机构 _多重if选择机构_if选择结构嵌套(综合练习题——code)

    import java.util.*; class If01{ public static void main(String[ ]args){ //练习1:假如张三参加Java考试,判断如果在95分以 ...

随机推荐

  1. java bigdemical比较大小

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_33451004/article/details/71247041 java中对bigdimic ...

  2. 动态规划:高维DP

    例子当然是王八棋这道题,这道题以前是写烂了 先来一个大暴力,zlw教的暴力~~ #include<iostream> using namespace std; ,maxm=; ]; int ...

  3. 51nod 1020 逆序排列——dp

    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...

  4. 【洛谷 P4342】[IOI1998]Polygon(DP)

    题目链接 题意不再赘述. 这题和合并石子很类似,但是多了个乘法,而乘法是不满足"大大得大"的,因为两个非常小的负数乘起来也会很大,一个负数乘一个很大的整数会很小,所以我们需要添加一 ...

  5. kernel 中 sscanf和sprintf()函数使用说明【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/8480695 在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于s ...

  6. golang写一个简单的爬虫

    package main import( "fmt" "io/ioutil" "net/http" ) func gethtml(url s ...

  7. mininet+floodlight搭建sdn环境并创建简单topo

    第一步:安装git sudo apt-get update sudo apt-get install git 测试git是否安装成功: git 第二步:安装mininet 1.获取mininet最新源 ...

  8. 聊聊C++模板函数与非模板函数的重载

    前言 函数重载在C++中是一个很重要的特性.之所以有了它才有了操作符重载.iostream.函数子.函数适配器.智能指针等非常有用的东西. 平常在实际的应用中多半要么是模板函数与模板函数重载,或者是非 ...

  9. python 多进程并发与多线程并发

    本文对python支持的几种并发方式进行简单的总结. Python支持的并发分为多线程并发与多进程并发(异步IO本文不涉及).概念上来说,多进程并发即运行多个独立的程序,优势在于并发处理的任务都由操作 ...

  10. hdu 1513(滚动数组)

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...