[java]代码库
/**
* 功能: 简单的员工薪水管理系统(运用集合ArrayList)
*/
package com.text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;;
public class Text1 {
public static void main(String[] args) throws Exception {
//创建一个EmpManage
EmpManage em=new EmpManage();
BufferedReader br =new BufferedReader( new InputStreamReader(System.in));
while(true){
System.out.println("请选择操作:");
System.out.println("1.添加员工");
System.out.println("2.查询员工信息");
System.out.println("3.修改员工工资");
System.out.println("4.显示全部员工信息");
System.out.println("5.删除员工");
System.out.println("6.退出系统");
String type=br.readLine();
if (type.equals("1")) {
System.out.println("请输入员工编号:");
String empNum =br.readLine();
System.out.println("请输入名字:");
String name=br.readLine();
System.out.println("请输入员工工资");
float sal =Float.parseFloat(br.readLine());
Emp emp =new Emp(empNum, name, sal);
em.addEmp(emp);
}else if (type.equals("2")) {
System.out.println("请输入员工编号:");
String empNum=br.readLine();
em.showInfor(empNum);
}else if (type.equals("3")) {
System.out.println("请输入员工编号;");
String empNum=br.readLine();
System.out.println("请输入修改后的工资:");
float newSal=Float.parseFloat(br.readLine());
em.updateSal(empNum, newSal);
}else if (type.equals("4")) {
em.showAllInfor();
}else if (type.equals("5")) {
System.out.println("请输入员工编号:");
String empNum=br.readLine();
em.deleteEmp(empNum);
}else if (type.equals("6")) {
System.exit(0);
}
}
}
}
class EmpManage{
private ArrayList al =null;
// 构造函数
public EmpManage(){
al=new ArrayList();
}
//加入员工
public void addEmp(Emp emp){
al.add(emp);
}
//根据员工号显示员工信息
public void showInfor(String empNum){
//遍历ArrayList
for (int i=0;i<al.size();i++){
//取出Emp对象
Emp emp=(Emp)al.get(i);
//比较员工号
if (emp.getEmNum().equals(empNum)) {
System.out.println("该员工的信息为:");
System.out.println("员工编号:"+emp.getEmNum());
System.out.println("名字:"+emp.getName());
System.out.println("工资:"+emp.getSal());
}else {
System.out.println("查无此人!");
}
}
}
//显示全部员工信息
public void showAllInfor(){
for (int i=0;i<al.size();i++){
Emp emp=(Emp)al.get(i);
System.out.println("员工编号"+" 名字"+" 工资");
System.out.println(emp.getEmNum()+" "+emp.getName()+" "+emp.getSal());
}
}
//修改员工工资
public void updateSal(String empNum,float newSal){
//遍历ArrayList
for (int i=0;i<al.size();i++){
Emp emp=(Emp)al.get(i);
if (emp.getEmNum().equals(empNum)) {
emp.setSal(newSal);
System.out.println("修改成功!!");
}
else {
System.out.println("输入有误,无此人!");
}
}
}
//删除员工
public void deleteEmp(String empNum){
for (int i=0;i<al.size();i++){
Emp emp =(Emp)al.get(i);
if (emp.getEmNum().equals(empNum)) {
al.remove(i);
System.out.println("删除成功!!");
}
}
}
}
class Emp{
private String name;
private String emNum;
private float sal;
public Emp(String empNum,String name,float sal){
this.name=name;
this.emNum=empNum;
this.sal=sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmNum() {
return emNum;
}
public void setEmNum(String emNum) {
this.emNum = emNum;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
}