C# 工资结算系统-类继承

Posted Link2Points

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 工资结算系统-类继承相关的知识,希望对你有一定的参考价值。


(一)阐述

  1. 普通员工:Employee固定月薪。
  2. 销售员:Salesman固定月薪+销售额*提成率。
  3. 计时工人:HourlyWorker固定月薪+工作小时数每小时酬金(每月超过160小时,酬金1.5倍)
  4. 计件工人:固定月薪+每件酬金*生产的产品件数。

(二)截图

  1. 添加员工
    员工号由程序自动提供,添加员工需要输入姓名和工种。
    在这里插入图片描述在这里插入图片描述
    同样依次输入员工信息。

  2. 计算并输出每月工人工资
    通过ID对于对应的员工输入对应信息:

    • 对于普通员工直接输出工资信息
    • 对于销售员输入销售额
    • 对于计时工输入工时,并体现激励机制
    • 对于计件工输入生产数
      在这里插入图片描述
  3. 查询单个员工本月工资
    在这里插入图片描述
    其它类同。

  4. 打印所有员工工资
    在这里插入图片描述

(三)代码

/*
 * 工资结算系统
 * class
 *      普通员工    Employee
 *      销售员     Salesman
 *      计时工     HourlyWorker
 *      计件工     PieceWorker
 *      主类      Program
 */

using System;
using System.Collections;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Convert;

namespace SalarySettlemen
{
    /*
     * 普通员工
     *  员工号、姓名、工种、固定工资
     */
    public class Employee
    {
        private int id;
        private string name = "";
        private string workType = "普通";
        private Decimal baseSalary;
        private Decimal salary;

        public Employee() {}

        public Employee(int id, string name, string workType, Decimal baseSalary)
        {
            this.id = id;
            this.name = name;
            this.workType = workType;
            this.baseSalary = baseSalary;
            salary = baseSalary;
        }
        // 打印
        public virtual void print()
        {
            WriteLine("员工号:{0}  员工姓名{1}\\n工种:{2}  固定月薪:{3}k", id, name, workType, baseSalary); 
        }

        public int ID
        {
            get => id;
            set => id = value;
        }
        public string Name
        {
            get => name;
            set => name = value;
        }
        public string WorkType
        {
            get => workType;
            set => workType = value;
        }
        public Decimal BaseSalary
        {
            get => baseSalary;
            set => baseSalary = value;
        }
        public decimal Salary
        {
            get => salary;
            set => salary = value;
        }
    }
    /*
     * 销售员
     *  提成、销售额、薪水
     */
    public class Salesman:Employee
    {
        private double deductPercent;
        private Decimal saleAmount;
        private Decimal salary;
        
        public Salesman() {}
        public Salesman(Decimal saleAmount, double deductPercent,
                        int id, string name, string workType, Decimal baseSalary)
        {
            ID = id;
            Name = name;
            WorkType = workType;
            BaseSalary = baseSalary;
            this.saleAmount = saleAmount;
            this.deductPercent = deductPercent;
            salary = saleAmount * (Decimal) deductPercent + baseSalary;
            Salary = salary;
        }
        // 打印
        public override void print()
        {
            base.print();
            WriteLine("销售额:{0} 提成率:{1}% 月薪:{2}k", saleAmount, deductPercent * 100, salary);
        }
    }
    /*
     * 计时工
     *  工时、时金、薪水
     */
    public class HourlyWorker:Employee
    {
        private int workHour;
        private Decimal reword;
        private Decimal salary;

        public HourlyWorker() {}
        public HourlyWorker(int workHour, Decimal hourReword, int incentDuration, double incent,
                            int id, string name, string workType, Decimal baseSalary)
        {
            ID = id;
            Name = name;
            WorkType = workType;
            BaseSalary = baseSalary;
            this.workHour = workHour;
            reword = workHour > incentDuration ? hourReword * (Decimal) incent : hourReword;
            salary = reword * workHour + baseSalary;
            Salary = salary;

        }
        // 打印
        public override void print()
        {
            base.print();
            WriteLine("工时:{0}h 时金:{1} 月薪:{2}k", workHour, reword, salary);
        }
    }
    /*
     * 计件工
     *  件数、单件酬金、薪水
     */
    public class PieceWorker:Employee
    {
        private int productNumber;
        private Decimal reword;
        private Decimal salary;

        public PieceWorker() {}

        public PieceWorker(int productNumber, Decimal pieReword,
                            int id, string name, string workType, Decimal baseSalary)
        {
            ID = id;
            Name = name;
            WorkType = workType;
            BaseSalary = baseSalary;
            reword = pieReword;
            this.productNumber = productNumber;
            salary = productNumber * pieReword + baseSalary;
            Salary = salary;
        }
        // 打印
        public override void print()
        {
            base.print();
            WriteLine("件数:{0} 单件酬金:{1} 月薪:{2}k", productNumber, reword, salary);
        }
    }
    /*
     * 主类
     */
    internal class Program
    {
        private static Employee employee;
        // 员工数据
        private static ArrayList list = new ArrayList();
        // 固定工资
        private static Decimal baseSalary = 30; 
        // 销售抽成
        private static double deductPercent = 0.01; 
        // 时金
        private static Decimal hourReword = 1;
        // 激励工时
        private static int incentDuration = 160;
        // 激励倍数
        private static double incent = 1.5;
        // 单件酬金
        private static Decimal pieReword = 1;
        
        /*
         * 初始化界面
         */
        public static void initInterface()
        {
            WriteLine(" ******************************* ");
            WriteLine("*    SALARY SETTLEMEN SYSTEM    *");
            WriteLine("*                               *");
            WriteLine(" ******************************* ");
            WriteLine("**************主界面**************");
        }
        /*
         * 添加员工界面
         *      输入姓名、工种
         *      输入验证
         *      自动填充ID等信息
         */
        public static void hireInterface()
        {
            int id = list.Count;
            while (true)
            {
                WriteLine("员工号:{0}", id);
                Write("姓名:");
                string name = ReadLine();
                if (name.Length == 0)
                {
                    WriteLine("不能为空");
                    continue;
                }
                Write("1)普通 2)销售 3)计时 4)计件\\n" +
                      "输入工种:");
                string workType = ReadLine();
                if (workType.Length == 0 || !Regex.IsMatch(workType, "1|2|3|4"))
                {
                    WriteLine("输入错误");
                    continue;
                }

                switch (workType)
                {
                    case "1":
                        workType = "普通";
                        break;
                    case "2":
                        workType = "销售";
                        break;
                    case "3":
                        workType = "计时";
                        break;
                    case "4":
                        workType = "计件";
                        break;
                }
                
                ArrayList info = new ArrayList();
                info.Add(id);           // 员工号
                info.Add(name);         // 姓名
                info.Add(workType);     // 工种
                info.Add(0);            // 工资
                info.Add(0);            // 销售额
                info.Add(0);            // 工时
                info.Add(0);            // 生产件数
                list.Add(info);
                break;
            }
        }
        /*
         * 计算工资界面
         *      输入员工号、对应信息
         *      输入验证
         */
        public static void calInterface()
        {
            while (true)
            {
                Write("输入员工号:");
                string queryID = ReadLine();
                int index = ToInt32(queryID);
                if (index >= list.Count)
                {
                    WriteLine("无此信息");
                    continue;
                }
                
                ArrayList info = new ArrayList();
                info = (ArrayList) list[index];
                int id = (int) info[0];
                string name = (string) info[1];
                string workType = (string) info[2];
                switch (workType)
                {
                    case "普通":
                        employee = new Employee(id, name, workType, baseSalary);
                        info[3] = employee.Salary;
                        break;
                    case "销售":
                        Write("输入销售额:");
                        try
                        {
                            Decimal saleAmount = ToDecimal(ReadLine());
                            employee = new Salesman(saleAmount, deductPercent,
                                                    id, name, workType, baseSalary);
                            info[3] = employee.Salary;
                            info[4] = saleAmount;
                        }
                        catch (Exception e)
                        {
                            WriteLine(e);
                            throw;
                        }
                        break;
                    case "计时":
                        Write("输入时长:");
                        try
                        {
                            int workHour = ToInt32(ReadLine());
                            employee = new HourlyWorker(workHour, hourReword, incentDuration, incent,
                                                        id, name, workType, baseSalary);
                            info[3] = employee.Salary;
                            info[5] = workHour;
                        }
                        catch (Exception e)
                        {
                            WriteLine(e);
                            throw;
                        }
                        break;
                    case "计件":
                        Write("输入件数:");
                        try
                        {
                            int productNumber = ToInt32(ReadLine());
                            employee = new PieceWorker(productNumber, pieReword,
                                                       id, name, workType, baseSalary);
                            info[3] = employee.Salary;
                            info[6] = productNumber;
                        }
                        catch (Exception e)
                        {
                            WriteLine(e);
                            throw;
                        }
                        break;
                }
                WriteLine("**************结果***************");
                employee.print();
                WriteLine("*********************************");

                Write("Y)继续  任意键)退回\\n" +
                      "输入:");
                if (ReadLine().Equals("Y"))
                {
                    continue;
                }
                break;
            }
        }
        /*
         * 查询
         */
        public static void query(ArrayList info)
        {
            int id = (int) info[0];
            string name = (string) info[1];
            string workType = (string) info[2];
            Decimal salary = ToDecimal(info[3]);
            Decimal saleAmount = ToDecimal(info[4]);
            int workHour = (int) info[5];
            int productNumber = (int) info[6];
            switch (workType)
            {
                case "普通":
                    employee = new Employee(id, name, workType, salary);
                    break;
                case "销售":
                    employee = new Salesman(saleAmount, deductPercent,
                                            id, name, workType, baseSalary);
                    break;
                case "计时":
                    employee = new HourlyWorker(workHour, hourReword, incentDuration, incent,
                                                id, name, workType, baseSalary);
                    break;
                case "计件":
                    employee = new PieceWorker(productNumber, pieReword,
                                               id, name, workType, baseSalary);
                    break;
            }
            employee.print();
            WriteLine("*********************************");
        }
        /*
         * 查询单个员工工资
         *      输入员工号
         */
        public static void querySigEmpInterface()
        {
            while (true)
            {
                Write("输入员工号:");
                string queryID = ReadLine();
                int index = ToInt32(queryID);
                WriteLine("**************结果***************");
                if (index >= list.Count)
                {
                    WriteLine("无此信息");
                    continue

以上是关于C# 工资结算系统-类继承的主要内容,如果未能解决你的问题,请参考以下文章

python面向对象总结

C#桌面办公应用-工资管理系统系列五

有啥比较好的员工工资管理系统么?

python学习之工资结算

对象继承封装多态抽象类的组合应用

Pthon100天学习笔记Day19 面向对象基础