ArrayList做的员工薪资管理系统

/**
*	公司职员薪水管理系统,有以下功能
*	1,当有新员工时,将加入该管理系统
*	2,根据员工号,显示该员工信息
*	3,可以显示所有员工的信息
*	4,可以修改员工的薪水
*	5,当员工离职时,从该系统中删除该员工
*	6,可以将员工按薪水高低排序(思考)
*	7,统计员工的平均工资,最高和最低工资
*/
import java.util.ArrayList;
import java.util.Scanner;

public class Employees {

	static ArrayList<Employee> list = new ArrayList<Employee>();
	static Scanner input = new Scanner(System.in);	
	
	public static void main(String[] args) {	
		System.out.println("欢迎使用职工薪水管理系统\n");
		while(true){
			System.out.println("-------------------------------------------------------------------------------------------------------------------");
			System.out.println("请选择:\n1.添加员工    2.查询员工信息    3.显示所有员工信息    4.修改员工薪水    5.删除员工    6.员工薪水排序    7.统计平均工资,最高、低工资");
			System.out.print("请输入序号:");
			int choice = input.nextInt();
			switch(choice){
			case 1:
				addEmployee(); //添加员工
				break;
			case 2:
				selectEmployee(); //根据员工号查询员工信息
				break;
			case 3:
				showAllEmployee(); //显示所有员工信息
				break;
			case 4:
				updateEmployeeMoney(); //修改员工薪水
				break;
			case 5:
				delEmployee(); //删除员工
				break;
			case 6:
				MoneySort(); //薪资排序
				break;
			case 7:
				other(); //统计员工的平均工资,最高和最低工资
				break;
			default:
				System.out.println("\n没有这个选项,请重新选择!!!!!");
			}
		}
		
	}
	
	
	//添加员工
	public static void addEmployee(){
		System.out.println("\n--------------添加员工-------------");
		String id;
		String name;
		double salary;
		
		while(true){
			boolean flag = false;
			System.out.print("请输入员工号:");
			id = input.next();
			for(int i=0;i<list.size();i++){   //判断是否有相同员工号
				Employee emp = list.get(i);
				if(emp.getId().equals(id)){
					flag = true;
				} else {
					break;
				}
			}
			//添加的员工号如果已存在会让重新输入,没有则会跳出循环
			if(flag){
				System.out.println("该员工号已存在,请重新输入!!!!!");
			} else {
				break;
			}
		}
		System.out.print("请输入员工姓名:");
		name = input.next();
		System.out.print("请输入员工工资:");
		salary = input.nextDouble();
		
		Employee emp = new Employee(id, name, salary);

		boolean bool = list.add(emp);
		if(bool==true){
			System.out.println("恭喜,添加成功!");
		} else {
			System.out.println("员工数据添加失败!");
		}
	}
	
	
	//查询员工信息
	public static void selectEmployee(){
		System.out.println("\n--------------查询员工信息-------------");
		System.out.print("请输入要查询的员工号:");
		
		boolean flag = false;
		
		String checkId = input.next();
		
		for(int i=0;i<list.size();i++){
			
			Employee emp = list.get(i);
			
			if(emp.getId().equals(checkId)){
				System.out.println("查询成功!");
				System.out.println("员工号   员工姓名      薪资");
				System.out.println(emp.toString());
				flag = true;
			}
		}
		if(flag == false){
			System.out.println("该员工不存在!");
		}
	}
	
	
	//显示所有员工信息
	public static void showAllEmployee(){
		if(list.size()==0){
			System.out.println("无员工信息");
			return;
		}
		System.out.println("员工号   员工姓名      薪资");
		for(Employee emp:list){
			System.out.println(emp.toString());
		}
	}
	
	
	//修改员工薪资
	public static void updateEmployeeMoney(){
		System.out.println("\n--------------员工薪资修改-------------");
		System.out.print("请输入要修改薪资的员工号:");
		String id = input.next();
		boolean flag = false;
		int index = 0; //需要修改薪资的下标,初始化0
		double newSalary; //新的薪资
		for(int i=0;i<list.size();i++){
			Employee emp = list.get(i);
			if(emp.getId().equals(id)){
				flag = true;
				index = i;
			}
		}
		if(flag == true){
			System.out.print("请输入你要修改的薪资:");
			newSalary = input.nextDouble();
			Employee emp = list.get(index); //下标对应的emp
			emp.setSalary(newSalary);
			System.out.println("修改成功!!!");
		} else {
			System.out.println("没有此员工!!!");
		}
	}
	
	
	
	//删除员工
	public static void delEmployee(){
		System.out.println("\n--------------删除员工信息-------------");
		System.out.print("请输入要删除员工的员工号:");
		String id = input.next();
		boolean flag = false;
		for(int i=0;i<list.size();i++){
			Employee emp = list.get(i);
			if(emp.getId().equals(id)){
				list.remove(i);
				flag = true;
			}
		}
		if(flag == true){
			System.out.println("删除成功!!!");
		} else {
			System.out.println("未找到此员工,删除失败!!!"); 
		}
	}
	
	
	//薪资排序
	public static void MoneySort(){
		System.out.println("\n--------------员工薪资排序-------------");
		System.out.println("请选择:1.薪资从高到低     2.薪资从低到高");
		System.out.print("请输入:");
		int choice = input.nextInt();
		
		switch(choice){
		case 1:
			Desc();
			break;
		case 2:
			Asc();
			break;
		}
	}
	
	
	
	//薪资升序排列
	public static void Asc(){
		Employee temp;
		System.out.println("薪资从低到高排序结果是:");
		System.out.println("员工号   员工姓名      薪资");
		for(int i=0;i<list.size()-1;i++){
			for(int j=0;j<list.size()-1-i;j++){
				if(list.get(j).getSalary()>list.get(j+1).getSalary()){
					temp = list.get(j);
					list.set(j, list.get(j+1));
					list.set(j+1, temp);
				}
			}
		}
		for(int i =0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
	//薪资降序排列
	public static void Desc(){
		Employee temp;
		System.out.println("薪资从高到低排序结果是:");
		System.out.println("员工号   员工姓名      薪资");
		for(int i=0;i<list.size()-1;i++){
			for(int j=0;j<list.size()-1-i;j++){
				if(list.get(j).getSalary()<list.get(j+1).getSalary()){
					temp = list.get(j);
					list.set(j, list.get(j+1));
					list.set(j+1, temp);
				}
			}
		}
		for(int i =0;i<list.size();i++){
			System.out.println(list.get(i));
		}
				
	}
	
	
	//统计平均工资,最高、低工资
	public static void other(){
		System.out.println("\n--------------员工平均、最高、最低工资-------------");
		double sum = 0;
		double avg;
		
		double max = list.get(0).getSalary();
		double min = list.get(0).getSalary();
		
		for(int i=0;i<list.size();i++){
			Employee emp = list.get(i);
			sum += emp.getSalary();	
			
			if(list.get(i).getSalary() > max){
				max = list.get(i).getSalary();
			}			
			
			if(list.get(i).getSalary()<min){
				min = list.get(i).getSalary();
			}
		}
		avg = sum/list.size();
		showAllEmployee();
		System.out.println("员工平均工资:"+avg+", 最高工资"+max+", 最低工资"+min);	
	}
			
}


class Employee{
	private String id;
	private String name;
	private double salary;
	
	public Employee() {
		super();
	}
	
	public Employee(String id, String name, double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}

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

	@Override
	public String toString() {
		return id + "\t" + name + "\t" + salary ;
	}	
}

![运行结果](https://img-blog.csdnimg.cn/20200108093513588.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTYwNDc5,size_16,color_FFFFFF,t_70)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以的,使用Java编写工资管理系统可以实现查询、添加和删除员工的功能。以下是一个简单的示例代码: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } } class SalaryManagementSystem { private List<Employee> employees; public SalaryManagementSystem() { employees = new ArrayList<>(); } public void addEmployee(Employee employee) { employees.add(employee); } public void removeEmployee(Employee employee) { employees.remove(employee); } public void displayAllEmployees() { System.out.println("Employee List:"); for (Employee employee : employees) { System.out.println("Name: " + employee.getName() + ", Salary: " + employee.getSalary()); } } public Employee findEmployeeByName(String name) { for (Employee employee : employees) { if (employee.getName().equals(name)) { return employee; } } return null; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); SalaryManagementSystem system = new SalaryManagementSystem(); while (true) { System.out.println("1. Add Employee"); System.out.println("2. Remove Employee"); System.out.println("3. Display All Employees"); System.out.println("4. Find Employee by Name"); System.out.println("5. Exit"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume the newline character switch (choice) { case 1: System.out.print("Enter employee name: "); String name = scanner.nextLine(); System.out.print("Enter employee salary: "); double salary = scanner.nextDouble(); scanner.nextLine(); // Consume the newline character Employee employee = new Employee(name, salary); system.addEmployee(employee); System.out.println("Employee added successfully!"); break; case 2: System.out.print("Enter employee name: "); String removeName = scanner.nextLine(); Employee removeEmployee = system.findEmployeeByName(removeName); if (removeEmployee != null) { system.removeEmployee(removeEmployee); System.out.println("Employee removed successfully!"); } else { System.out.println("Employee not found!"); } break; case 3: system.displayAllEmployees(); break; case 4: System.out.print("Enter employee name: "); String findName = scanner.nextLine(); Employee findEmployee = system.findEmployeeByName(findName); if (findEmployee != null) { System.out.println("Name: " + findEmployee.getName() + ", Salary: " + findEmployee.getSalary()); } else { System.out.println("Employee not found!"); } break; case 5: System.exit(0); default: System.out.println("Invalid choice!"); } } } } ``` 这个示例代码演示了一个简单的工资管理系统,可以添加、删除和查询员工信息。用户可以通过在控制台中输入选项来执行相应的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值