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;
                }
                
                ArrayList info = new ArrayList();
                info = (ArrayList) list[index];
                query(info);
                
                Write("Y)继续  任意键)退回\n" +
                      "输入:");
                if (ReadLine().Equals("Y"))
                {
                    continue;
                }
                break;
            }
        }
        /*
         * 查询全部员工工资
         */
        public static void queryAllEmployee()
        {
            WriteLine("**************结果***************");
            foreach (ArrayList info in list)
            {
                query(info);
            }
        }
        /* 主操作界面
         *      1) 添加员工
         *          输入员工号(自动)、姓名、工种
         *      2) 计算每月员工工资
         *          输入员工号 ---> 输出姓名、工种
         *          输入对应工种的计算参数
         *      3) 查询单个员工工资
         *          输入员工号 ---> 输出姓名、工种、工资
         *      4) 查询所有员工工资
         *          输入员工号 ---> 输出姓名、工种、工资
         *      ANY KEYS) 退出
         * 回调
         */
        public static void operationInterface()
        {
            WriteLine("1) 添加新员工       2) 计算每月员工工资\n" +
                      "3) 查询单个员工工资  4) 查询所有员工工资");
            Write("输入(任意键 退出):");
            string inputCommand = ReadLine();
            WriteLine("**************子界面**************");
            switch (inputCommand)
            {
                case "1":
                    hireInterface();
                    break;
                case "2":
                    calInterface();
                    break;
                case "3":
                    querySigEmpInterface();
                    break;
                case "4":
                    queryAllEmployee();
                    break;
                default:
                    WriteLine("**************退出***************");
                    return;
            }
            // 结束子操作 回调 主操作界面
            WriteLine("**************主界面**************");
            operationInterface();
        }
        /*
         * 主函数
         */
        public static void Main(string[] args)
        {
            initInterface();
            operationInterface();
        }
    }
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
某公司雇员(Employee)包括经理(Manager),技术人员(Technician)和销售员(Salesman)。开发部经理(DeveloperManger),既是经理也是技术人员。销售部经理(SalesManager),既是经理也是销售员。 以Employee为虚基派生出Manager,Technician和Salesman;再进一步派生出Developermanager和Salesmanager。 Employee的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。 Technician派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 Salesman派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 Manager派生属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。 而DeveloperManager,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。 SalesManager,pay()函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。 编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值