一道綜合練習題實踐list及dictionary集合類

定義一個員工的集合,對員工集合內的元素進行查詢和刪除。
實現員工的簽到和簽退,要求以下:
//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);
        }
    }
}
View Code

公司信息類java

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 = 0; i < list.Count; i++)
            {
                if (list[i].ID == nID)
                {
                    list.RemoveAt(i);
                }
            }
        }

        public void PrintAllEmp()
        {
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ShowMsg();
            }

        }

        public Employee FindEmp(int nID)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ID == nID)
                {
                    return list[i];
                }
            }
            return null;
        }
    }
}
View Code

考勤記錄類ide

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;
    }
}
View Code

主程序類:ui

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)
        {
            ////  一、使用你所學的C#的容器類實現員工考勤信息管理, 
            //實現如下功能:要求:使用泛型集合list的添加,查詢和刪除操做) 
            // A:實現新增員工(工號,年齡,姓名,性別)
            List<Employee> list = new List<Employee>();
    

            c.AddEmp(new Employee(11, 18, "zhang3", ""));
            c.AddEmp(new Employee(22, 28, "li4", ""));
            c.AddEmp(new Employee(33, 38, "wang5", ""));
            c.AddEmp(new Employee(44, 48, "zhao6", ""));
            c.AddEmp(new Employee(55, 58, "qian7", ""));
            c.AddEmp(new Employee(66, 68, "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 == 0)
                    break;
                switch(n)
                {
                    case 1:
                        {
                            Console.WriteLine("請輸入要簽到的員工號碼");
                            int nEmpNo = Convert.ToInt32(Console.ReadLine());
                            qiandao(nEmpNo);
                            break;
                        }
                    case 2:
                        {
                            Console.WriteLine("請輸入要簽退的員工號碼");
                            int nEmpNo = Convert.ToInt32(Console.ReadLine());
                            qiantui(nEmpNo);
                            break;
                        }
                    case 3:
                        {
                            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 = -1;
                int i = 0;
                foreach (KaoqinRecord kr in lst)
                {
                    if (kr.nID == nID)
                    {
                        nFindIndex = i;
                        break;
                    }
                    i++;
                }

                if(nFindIndex >= 0)
                {
                    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);

        }
    }
}
View Code

 

java版this

公司信息類spa

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;
    }
}
View Code

考勤記錄類3d

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;
    }


}
View Code

主程序類:code

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;
    }
}
View Code
相關文章
相關標籤/搜索