工资支付系统:

  • 要求:为某公司编写一个工资支付系统,用于计算某一类员工的月薪。该公司共有四类员工:领固定月薪的(SalariedEmployee);计时取酬的(HourlyEmployee,如果一月工时超过160小时,则还需对额外的工时支付加班费);按销售额提成(CommissionEmployee)的和带底薪并按销售额提成的(BasePlusCommissionEmployee),其继承层次结构如下所示。已知每类员工均有表示员工工号、姓名和出生年月的属性,和用于计算员工月薪的方法。创建一个Employee变量数组,保存Employee类层次结构中每个具体类对象的引用,对每个Employee显示其工号、姓名、出生年月和月收入,如果当月是Employee的生日所在的月份,则还另发给他100元作为红包。
  • 继承关系图如下:
  • 代码实现:
//Employee.java(抽象类)
abstract class Employee {public int number;public String name;public int birthYear;public int birthMonth;public static int salary;public abstract int salary();
}
//GetSalaryPerMonth.java(接口)
public interface GetSalaryPerMonth {int SalaryPerMonth = 5000; //假设月薪为5000
}
//GetWorkHour.java(接口)
public interface GetWorkHour {int SalaryPerHour = 30; //给定时薪int OvertimeSalaryPerHour = 40; //给定超过标准工时的时薪
}
//GetSale.java(接口)
public interface GetSale {int SalaryPerSale = 10;
}
//GetBaseSalary.java(接口)
public interface GetBaseSalary {int BaseSalary = 1000; //给定底薪
}
  • 下面四个类是对抽象类的实现
//SalariedEmployee.java
import java.util.Calendar;public class SalariedEmployee extends Employee implements GetSalaryPerMonth{public static int RedEnvelope = 100; //生日红包public int salary() {int month = Calendar.getInstance().get(Calendar.MONTH) + 1; //获取当前月份if (month == birthMonth)salary = SalaryPerMonth + RedEnvelope;elsesalary = SalaryPerMonth;return salary;}
}
//HourlyEmployee.java
import java.util.Calendar;
import java.util.InputMismatchException;
import java.util.Scanner;public class HourlyEmployee extends Employee implements GetWorkHour {public static double RedEnvelope = 100;public static int StandardWorkTime = 160;public int salary() {try { //规范输入int month = Calendar.getInstance().get(Calendar.MONTH) + 1;int WorkHour;System.out.print("请输入您的工时:");WorkHour = new Scanner(System.in).nextInt();if (WorkHour < 0) //规范输入{System.out.println("请输入正整数!");return salary();}else{ //计算工资if (WorkHour > StandardWorkTime)salary = SalaryPerHour * StandardWorkTime + OvertimeSalaryPerHour * (WorkHour - 160);elsesalary = SalaryPerHour * WorkHour;if (month == birthMonth)salary += RedEnvelope;return salary;}}catch (InputMismatchException e){System.out.println("请输入整数!");return salary();}}
}
//CommissionEmployee.java
import java.util.Calendar;
import java.util.InputMismatchException;
import java.util.Scanner;public class CommissionEmployee extends Employee implements GetSale{public static double RedEnvelope = 100;public int salary() {try {int month = Calendar.getInstance().get(Calendar.MONTH) + 1;int AmountOfSale;System.out.print("请输入您的销售量:");AmountOfSale = new Scanner(System.in).nextInt();if (AmountOfSale < 0){System.out.println("请输入正整数!");return salary();}else{salary = AmountOfSale * SalaryPerSale;if (month == birthMonth)salary += RedEnvelope;return salary;}}catch (InputMismatchException e){System.out.println("请输入整数!");return salary();}}
}
//BasePlusCommissionEmployee.java
import java.util.Calendar;
import java.util.InputMismatchException;
import java.util.Scanner;public class BasePlusCommissionEmployee extends Employee implements GetSale,GetBaseSalary {public static int RedEnvelope = 100;public int salary() {try {int month = Calendar.getInstance().get(Calendar.MONTH) + 1;int AmountOfSale;System.out.print("请输入您的销售量:");AmountOfSale = new Scanner(System.in).nextInt();if (AmountOfSale < 0){System.out.println("请输入正整数!");return salary();}else{salary = BaseSalary + AmountOfSale * SalaryPerSale;if (month == birthMonth)salary += RedEnvelope;return salary;}}catch (InputMismatchException e){System.out.println("请输入整数!");return salary();}}
}
  • 主类
//PayrollSystem.java
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;public class PayrollSystem {public static int choice;//选择菜单public static int choice() {try {choice = new Scanner(System.in).nextInt();if (choice < 0 || choice > 4){System.out.println("请输入整数0-4!");return choice();}else if (choice == 0)System.exit(0);elsereturn choice;}catch (InputMismatchException e){System.out.println("请输入整数!");return choice();}return -1;}//获取工号public static int get_number() {Scanner sc = new Scanner(System.in);System.out.print("请输入工号:");try {int number = sc.nextInt();if (number <= 0){System.out.println("请输入正整数!");return get_number();}return number;}catch (InputMismatchException e){System.out.println("请输入整数!");return get_number();}}//获取姓名public static String get_name() {Scanner sc = new Scanner(System.in);System.out.print("请输入姓名:");String name = sc.next();Pattern pattern = Pattern.compile(".*\\d+.*");if (pattern.matcher(name).matches()){System.out.println("不能输入数字");return get_name();}return name;}//获取出生年份public static int get_birthYear() {Scanner sc = new Scanner(System.in);System.out.print("请输入出生年份:");try {int birthYear = sc.nextInt();if (birthYear < 1900 || birthYear > 2021){System.out.println("输入有误,请重新输入!");return get_birthYear();}return birthYear;}catch (InputMismatchException e){System.out.println("请输入数字!");return get_birthYear();}}//获取出生月份public static int get_birthMonth() {Scanner sc = new Scanner(System.in);System.out.print("请输入出生月份:");try {int birthMonth = sc.nextInt();if (birthMonth < 1 || birthMonth > 12){System.out.println("输入有误,请重新输入!");return get_birthMonth();}return birthMonth;}catch (InputMismatchException e){System.out.println("请输入数字!");return get_birthMonth();}}public static void main(String[] args) {Employee []Data = new Employee[4];Data[0] = new SalariedEmployee();Data[1] = new HourlyEmployee();Data[2] = new CommissionEmployee();Data[3] = new BasePlusCommissionEmployee();System.out.println("请输入员工类型:1.领固定月薪 2.计时取酬 3.按销售额提成 4.带底薪并按销售额提成 0.退出");choice = choice();switch (choice){case 1:{Data[0].number = get_number();Data[0].name = get_name();Data[0].birthYear = get_birthYear();Data[0].birthMonth = get_birthMonth();System.out.println("处理结果:");System.out.println("工号:"+Data[0].number);System.out.println("姓名:"+Data[0].name);System.out.println("出生年月:"+Data[0].birthYear+"年"+Data[0].birthMonth+"月");System.out.println("工资:"+Data[0].salary());break;}case 2:{Data[1].number = get_number();Data[1].name = get_name();Data[1].birthYear = get_birthYear();Data[1].birthMonth = get_birthMonth();int salary = Data[1].salary();System.out.println("处理结果:");System.out.println("工号:"+Data[1].number);System.out.println("姓名:"+Data[1].name);System.out.println("出生年月:"+Data[1].birthYear+"年"+Data[1].birthMonth+"月");System.out.println("工资:"+salary);break;}case 3:{Data[2].number = get_number();Data[2].name = get_name();Data[2].birthYear = get_birthYear();Data[2].birthMonth = get_birthMonth();int salary = Data[2].salary();System.out.println("处理结果:");System.out.println("工号:"+Data[2].number);System.out.println("姓名:"+Data[2].name);System.out.println("出生年月:"+Data[2].birthYear+"年"+Data[2].birthMonth+"月");System.out.println("工资:"+salary);break;}case 4:{Data[3].number = get_number();Data[3].name = get_name();Data[3].birthYear = get_birthYear();Data[3].birthMonth = get_birthMonth();int salary = Data[3].salary();System.out.println("处理结果:");System.out.println("工号:"+Data[3].number);System.out.println("姓名:"+Data[3].name);System.out.println("出生年月:"+Data[3].birthYear+"年"+Data[3].birthMonth+"月");System.out.println("工资:"+salary);break;}default:System.out.println("error");}}
}
  • 测试结果

用Java编写一个工资支付系统相关推荐

  1. java实现网上支付_java编写一个网上支付系统界面

    展开全部 购物与结算 (beta4)/IE9.js"> function pay() { var link = document.getElementById("a1&quo ...

  2. 假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资:

    假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资: (1)经理(Manager)--每月获得一份固定的工资 (2)销售人员(Sal ...

  3. 停车场管理系统 java_使用java编写一个停车场管理系统

    使用java编写一个停车场管理系统 发布时间:2020-11-30 16:00:28 来源:亿速云 阅读:145 作者:Leah 这篇文章给大家介绍使用java编写一个停车场管理系统,内容非常详细,感 ...

  4. 用Java模拟一个银行ATM系统

    用Java模拟一个银行ATM系统 系统功能介绍: 全部代码 示例截图 系统功能介绍: 使用面向对象的编程思想,尽可能模拟真实世界中的银行ATM业务流程. main方法里通过调用一行代码,完成整个业务流 ...

  5. 用Java编写一个最简单的桌面程序

    使用Java的优势在于网络应用方面,但Java也提供了强大的用于开发桌面程序的API,它们包含在javax.swing包中.使用这个包可以编写简单的Java桌面应用程序. Java的javax.swi ...

  6. 用Java编写城市公交查询系统

    城市公交查询系统是一个实用的应用程序,可以帮助用户在城市中查找公交车路线.站点和到达时间.在这个回答中,我将用Java编写一个城市公交查询系统,包括代码和说明. 一.系统架构和功能 该城市公交查询系统 ...

  7. java编写存钱_用Java编写一个简单的存款

    package desposit.money; public class DespositMoney { public static void main(String[] args) { Custom ...

  8. 用java编写一个简单计算器

    java 采用java编写一个简单计算器,使用awt和swing 代码如下: import java.awt.Color; import java.awt.Font; import java.awt. ...

  9. 自动点名系统c语言,用C语言编写一个随机点名系统

    /*编写一个随机点名系统,运行该系统后,按空格键可以显示出一名同学,以前被选中的同学,将不会再次被选中*/ #include /*standard input & output*/ #incl ...

最新文章

  1. 人工智能python框架_Python 与 AI 智能框架 - 随笔分类 - Hopesun - 博客园
  2. Centos6安装Zabbix3.4
  3. 内存溢出和内存泄漏的区别、产生原因以及解决方案 转
  4. 3、常用关键字,变量赋值,多个变量赋值,标准数据类型,数字,字符串,列表,元组,字典,数据类型转换
  5. 跨域获取json电商数据
  6. 《Pycharm操作和配置指南》这些不会,写Python肯定慢嘛
  7. hashmap转红黑树的阈值为8_面试必考的 HashMap,这篇总结到位了
  8. goldendb基于mysql_中兴通讯GoldenDB在中信银行信用卡核心应用实践
  9. 简单批处理与多道批处理
  10. FPDF中文应用攻略
  11. Python学习之路(一)字符串
  12. EtherCAT总线运动控制学习笔记(RXXW_Dor)
  13. crossApp部署到Eclipse
  14. Cannot access a disposed object. A common cause of this error is disposing a context that ...问题解决
  15. sp3 文件格式说明
  16. redis manager desktop下载、安装、连接redis教程(官网)
  17. 计算机连接不上蓝牙鼠标,蓝牙鼠标怎么连接到笔记本电脑?
  18. BGA封装扇出过孔-BGA芯片的布局布线技巧
  19. 自动化测试和软件测试的区别,自动化测试和手动测试之间的区别
  20. 电话,手机,微信账号,邮箱正则表达式校验

热门文章

  1. 【泛微ecology】Linux下 ecology日志截取
  2. 前端二进制文件流下载
  3. px4源码编译之 建立自己的程序模块
  4. Office办公软件介绍
  5. Linux 小白笔记第八弹,手把手教你Ubuntu(乌班图)搭建samba服务器,从安装到配置。
  6. 步进电机驱动选型的详细指南
  7. div左右自适应式布局
  8. CF784D Touchy-Feely Palindromes
  9. pip安装python包出错:Could not find a version that satisfies the requirement skimage (from versions: )
  10. 高并发解决方案类考察点