下面就让我来详细讲解“Java实现工资管理简单程序”的完整攻略。
1. 确定需求
首先我们需要确定程序的需求。根据题目的要求,我们需要实现一个工资管理简单程序,这个程序需要实现以下功能:
- 添加新员工的信息
- 计算员工工资
- 查询员工信息
- 删除员工信息
- 修改员工信息
基于以上需求,我们可以大致分析出需要用到的Java知识点:
- 类与对象的概念
- 静态变量与静态方法的概念
- 条件语句、循环语句和方法的使用
- 集合的概念和使用
2. 设计程序架构
基于需求,我们可以设计出如下的程序架构:
- Employee类:员工信息的保存和管理
- Salary类:员工工资的计算和管理
- Manager类:用户的输入和查询,与其他类的交互
3. 编写程序
根据上述架构,我们可以开始编写程序。以下是细节实现。
//Employee类-员工信息的保存和管理
public class Employee {
//员工编号
private String id;
//员工姓名
private String name;
//员工工资
private double salary;
//员工部门
private String department;
public Employee(String id,String name,double salary,String department){
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
//Salary类-员工工资的计算和管理
public class Salary {
//所有员工对象的存储容器
private static List<Employee> employees = new ArrayList<>();
//计算员工工资
public static double calculateSalary(Employee employee){
double salary = employee.getSalary();
String department = employee.getDepartment();
//根据员工所在部门计算津贴
switch (department){
case "sale":
salary += 1000;
break;
case "tech":
salary += 1500;
break;
case "hr":
salary += 800;
break;
}
return salary;
}
//添加新员工的信息
public static void addEmployee(Employee employee){
employees.add(employee);
}
//查询员工信息
public static Employee queryEmployee(String id){
for (Employee employee : employees){
if (employee.getId().equals(id)){
return employee;
}
}
return null;
}
//删除员工信息
public static void deleteEmployee(String id){
Employee employee = queryEmployee(id);
if(employee != null){
employees.remove(employee);
System.out.println("删除员工信息成功!");
}else{
System.out.println("不存在这个员工!");
}
}
//修改员工信息
public static void updataEmployee(Employee employee){
for(int i = 0;i < employees.size();i++){
if(employee.getId().equals(employees.get(i).getId())){
employees.set(i, employee);
System.out.println("修改员工信息成功!");
return;
}
}
System.out.println("不存在这个员工!");
}
}
//Manager类-用户的输入和查询,与其他类的交互
public class Main {
public static void main(String[] args) {
//初始化员工信息
Employee e1 = new Employee("001","Tom",2000.0,"sale");
Employee e2 = new Employee("002","Jack",3000.0,"tech");
Employee e3 = new Employee("003","Rose",5000.0,"hr");
//加入员工信息
Salary.addEmployee(e1);
Salary.addEmployee(e2);
Salary.addEmployee(e3);
//交互
Scanner input = new Scanner(System.in);
System.out.println("1.查看员工信息\n2.添加员工信息\n3.删除员工信息\n4.修改员工信息\n请输入:");
while (input.hasNextLine()){
int choose = input.nextInt();
switch (choose){
case 1://查看员工信息
System.out.println("请输入员工编号:");
String id = input.next();
Employee employee = Salary.queryEmployee(id);
if(employee != null){
double salary = Salary.calculateSalary(employee);
System.out.println("编号:" + employee.getId() + " " + "姓名:" + employee.getName() + " " + "工资:" + salary);
}else{
System.out.println("不存在这个员工!");
}
break;
case 2://添加员工信息
System.out.println("请输入员工编号:");
String addId = input.next();
System.out.println("请输入员工姓名:");
String addName = input.next();
System.out.println("请输入员工薪水:");
double addSalary = input.nextDouble();
System.out.println("请输入员工部门:");
String addDepartment = input.next();
Salary.addEmployee(new Employee(addId,addName,addSalary,addDepartment));
System.out.println("添加员工信息成功!");
break;
case 3://删除员工信息
System.out.println("请输入员工编号:");
String deleteId = input.next();
Salary.deleteEmployee(deleteId);
break;
case 4://修改员工信息
System.out.println("请输入员工编号:");
String updataId = input.next();
Employee updataEmployee = Salary.queryEmployee(updataId);
if(updataEmployee != null){
System.out.println("请输入修改后的员工姓名:");
String updataName = input.next();
System.out.println("请输入修改后的员工薪水:");
double updataSalary = input.nextDouble();
System.out.println("请输入修改后的员工部门:");
String updataDepartment = input.next();
Employee updata = new Employee(updataId,updataName,updataSalary,updataDepartment);
Salary.updataEmployee(updata);
}else{
System.out.println("不存在这个员工!");
}
break;
default:
System.out.println("请重新输入!");
}
System.out.println("1.查看员工信息\n2.添加员工信息\n3.删除员工信息\n4.修改员工信息\n请输入:");
}
}
}
4. 示例说明
这里给出两个样例,用于演示程序的基本使用。
示例1-查看员工信息
- 输入:
1
001
- 输出:
编号:001 姓名:Tom 工资:3000.0
示例2-修改员工信息
- 输入:
4
001
Jerry
3000
sale
- 输出:
修改员工信息成功!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现工资管理简单程序 - Python技术站