员工管理系统
需求:完成员工管理系统,以对象数组的形式实现
功能:添加员工、显示员工信息、删除员工、查询员工信息、修改员工工资、按工资顺显示工资单、退出"
思路:先创建员工类和员工测试类,员工类写员工的属性,测试类先写登录方法,用for循环写登录有3次机会,如果3次输入都没成功则非法用户,然后写一个公共员工数组,然后先写6个功能方法写好注释,再对每个功能的内容进行完善。添加员工时员工数如果与数组长度相等要新建一个更长的数组去存放员工。写完功能后,写一个while死循环,不限制操作次数,循环里面用switch语句去调用前面写的功能方法。
代码展示:
1、创建员工类
/**
* 员工类
*/
public class Employee {
/**员工工号*/
public String code;
/**员工姓名*/
public String name;
/**员工工资*/
public int money;
}
2、创建员工测试类
import javax.swing.*;
/**
* 员工测试类
*/
public class EmployeeTest {
/**公共员工类数组*/
public static Employee[] EmployeeArray =new Employee[10];
/**加入的员工个数*/
public static int num =0;
public static void main(String[] args) {
if (login()==false){
JOptionPane.showMessageDialog(null,"非法用户");
return;
}
while (true) {
String str = JOptionPane.showInputDialog(null,
"请选择:\n1、添加员工\n2、显示员工信息\n3、删除员工\n" +
"4、查询员工信息\n5、修改员工工资\n6、按工资顺显示工资单\n7、退出");
switch (str) {
case "1":
addEmployee();
break;
case "2":
showInfor();
break;
case "3":
deleteEmployee();
break;
case "4":
look();
break;
case "5":
changInFor();
break;
case "6":
sort();
break;
case "7":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "请输入1~7");
break;
}
}
}
/**工资单排序*/
public static void sort(){
for (int i=0;i<num;i++){
for (int j=i+1;j<num;j++){
if (EmployeeArray[i].money<EmployeeArray[j].money){
Employee x =EmployeeArray[i];
EmployeeArray[i] =EmployeeArray[j];
EmployeeArray[j] =x;
}
}
}
showInfor();
}
/**修改员工工资*/
public static void changInFor(){
int index = checkEmployee();
if (index!=-1){
String str = JOptionPane.showInputDialog(null,"请输入修改工资");
EmployeeArray[index].money =Integer.parseInt(str);
showInfor();
}
}
/**查询员工信息*/
public static void look(){
String p ="工号 姓名 工资\n";
int index = checkEmployee();
if (index!=-1){
p += EmployeeArray[index].code+" "+EmployeeArray[index].name+" "+EmployeeArray[index].money;
JOptionPane.showMessageDialog(null,p);
}
}
/**删除员工*/
public static void deleteEmployee(){
int index =checkEmployee();
if (index!=-1){
for (int i=0;i<num-1;i++){
EmployeeArray[i] =EmployeeArray[i+1];
}
num--;
showInfor();
}
}
/**检测员工是否存在*/
public static int checkEmployee(){
String str = JOptionPane.showInputDialog(null,"请输入姓名");
for (int i=0;i<num;i++){
if (str.equals(EmployeeArray[i].name)){
return i;
}
}
JOptionPane.showMessageDialog(null,"查无此人");
return -1;
}
/**显示员工信息*/
public static void showInfor(){
String inFor ="工号 姓名 工资\n";
for (int i=0;i<num;i++){
inFor += EmployeeArray[i].code+" "+EmployeeArray[i].name+" "+EmployeeArray[i].money+"\n";
}
JOptionPane.showMessageDialog(null,inFor);
}
/**添加员工*/
public static void addEmployee(){
String newC = JOptionPane.showInputDialog(null,"请输入员工工号");
if (check(newC)!=true){
JOptionPane.showMessageDialog(null,"工号已存在,请重新输入");
return;
}
if (num==EmployeeArray.length){
newArray();
}
String name = JOptionPane.showInputDialog(null,"请输入姓名");
String money = JOptionPane.showInputDialog(null,"请输入工资");
String isContinue = JOptionPane.showInputDialog(null,"是否继续(y/n)");
EmployeeArray[num] =new Employee();
EmployeeArray[num].code =newC;
EmployeeArray[num].name =name;
EmployeeArray[num].money =Integer.parseInt(money);
num++;
if ("y".equalsIgnoreCase(isContinue)){//忽略大小写比较是否相等
addEmployee();
}
}
/**建个更长的数组*/
public static void newArray(){
Employee[] newEArray =new Employee[num+10];
for (int i=0;i<num;i++){
newEArray[i] =EmployeeArray[i];
// newEArray[i].code =EmployeeArray[i].code;
// newEArray[i].name =EmployeeArray[i].name;
// newEArray[i].money =EmployeeArray[i].money;
}
EmployeeArray =newEArray;
}
/**检测工号是否存在*/
public static boolean check(String newC){
for (int i=0;i<num;i++){
if (newC.equals(EmployeeArray[i].code)){
return false;
}
}
return true;
}
/**登录*/
public static boolean login(){
for (int i=0;i<3;i++){
String username = JOptionPane.showInputDialog(null,"请输入账号:");
String psw = JOptionPane.showInputDialog(null,"请输入账号:");
if ("123".equals(username) && "123".equals(psw)){
return true;
}
else {
JOptionPane.showMessageDialog(null,"账号或密码错误,请重新输入");
}
}
return false;
}
}
3、效果截图展示
登录输入账号
输入密码
账号或密码错误
3次账号或密码输入错误后
登录成功,显示功能菜单
添加员工
查看员工信息
员工工资从高到低排序