Java SE 基础回顾编程之: 员工工资及信息管理系统
2009-09-16 00:00:00 来源:WEB开发网Java se 管理系统代码
package com.youngmaster;
import java.util.Scanner;
/**
* application name Employee.java
* copyright Copyright 2008 少爷 版权所有
* company personal
* time 2009-3-22
*
* @author youngmaster
* @version ver 1.0
*/
//Employee类
class Employee{
//Employee类属性
private int id;
private String name;
private int grade;
private int basicSalary;
private int factSalary;
public Employee(){//默认构造函数
}
public Employee(int id,String name,int grade,int basicSalary){//构造函数
this.id = id;
this.name = name;
this.grade = grade;
this.basicSalary = basicSalary;
}
public int getId(){//获得ID
return id;
}
public void setId(int id){//设定ID
this.id = id;
}
public String getName(){//获得姓名
return name;
}
public void setName(String name){//设定姓名
this.name = name;
}
public int getGrade(){//获得等级
return grade;
}
public void setGrade(int grade){//设定等级
this.grade = grade;
}
public int getBasicSalary(){//获得基本工资
return basicSalary;
}
public void setBasicSalary(int basicSalary){//设定基本工资
this.basicSalary = basicSalary;
}
public int getFactSalary(){//获得基本工资
return factSalary;
}
public void setFactSalary(int factSalary){//设定基本工资
this.factSalary = factSalary;
}
}
//Manager 类
class Manager{
private int count = 0;//计数器,记下所有添加的记录
private int i;
Scanner sc = new Scanner(System.in);
Employee[] emp = new Employee[1000];//可包含1000条记录,为Employee类集合
//插入员工
public void insertEmp(){
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t**************添加员工*************");
System.out.print("\t\t请输入ID:");
int num1 = sc.nextInt();
System.out.print("\t\t请输入员工名:");
String name = sc.next();
System.out.print("\t\t请输入员工等级:");
int num2 = sc.nextInt();
System.out.print("\t\t请输入员工的基本工资:");
int wage = sc.nextInt();
emp[count] = new Employee(num1,name,num2,wage);
count ++;
}
//依ID删除员工
public void deletEmpById(){
int i,k= 0;
boolean flag = false;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.print("\t\t请选择您将删除的ID:");
int key = sc.nextInt();
for(i=0;i<count;i++){
if(key == emp[i].getId()){
flag = true;
k = i;
break;
}
}
if(flag == false){
System.out.printf("\n\t\t不存在ID为 %d 的员工\n",key);
}
if(flag == true){
for(i=k;i<count;i++){
emp[i] = emp[i+1];
}
count --;
}
}
//显示所有员工
public void showAllEmps(){
int i;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t****************************************");
System.out.println("\t\t|-------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 |");
System.out.println("\t\t|-------------------------------------|");
for(i=0;i<count;i++){
System.out.printf("\t\t| %d | %s | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade(),emp [i].getBasicSalary());
System.out.println();
}
}
//依ID查询员工
public void showEmpById(){
int i;
boolean flag = false;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.println("\n\t\t-----------------------------");
System.out.print("\t\t请输入您要查询的ID:");
int input = sc.nextInt();
for(i=0;i<count;i++){
if(input == emp[i].getId()){
System.out.println("\t\t|-------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 |");
System.out.printf("\t\t| %d | %s | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade(),emp [i].getBasicSalary());
flag = true;
break;
}
}
if(flag == false){
System.out.printf("\n\t\t不存在ID为 %d 的员工,请重新尝试!",input);
showEmpById();
}
}
//更新员工信息
public void updateEmp(){
boolean flag = false;
int i = 0;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.print("\n\t\t请输入您需要的ID:");
int num = sc.nextInt();
for(i=0;i<count;i++){
if(num == emp[i].getId()){
System.out.print("\n\t\tID:");
int id = sc.nextInt();
System.out.print("\t\t员工姓名:");
String name = sc.next();
System.out.print("\t\t员工等级:");
int grade = sc.nextInt();
System.out.print("\t\t基本工资:");
int basicSalary = sc.nextInt();
int factSalary = emp[i].getBasicSalary() + emp[i].getGrade()*500;
emp[i].setFactSalary(factSalary);
emp[i]=new Employee(id,name,grade,basicSalary);
flag = true;
break;
}
}
if(flag == false){
System.out.printf("\n\t\t不存在ID为 %d 的员工,请重新尝试!\n",num);
updateEmp();
}
}
//计算工资
public void computeSalary(){
int i = 0;
boolean flag = false;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.print("\n\t\t请输入ID:");
int num = sc.nextInt();
for(i=0;i<count;i++){
if(num == emp[i].getId())
{
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t***********************************************");
System.out.println("\t\t|--------------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 | 实际工资 |");
System.out.printf("\t\t| %d | %s | %d | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade (),emp[i].getBasicSalary(),(emp[i].getBasicSalary() + emp[i].getGrade()*500));
flag = true;
break;
}
}
if(flag == false){
System.out.printf("\n\t\t工号为 %d 的员工不存在,请重新尝试!\n",num);
computeSalary();
}
}
//显示所有工资
public void showAllSalary(){
int i;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t***********************************************");
System.out.println("\t\t|--------------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 | 实际工资 |");
System.out.println("\t\t|--------------------------------------------|");
for(i=0;i<count;i++){
System.out.printf("\t\t| %d | %s | %d | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade (),emp[i].getBasicSalary(),(emp[i].getBasicSalary() + emp[i].getGrade()*500));
System.out.println();
}
}
//依ID查询工资
public void showSalaryById(){
int i;
boolean flag = false;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.print("\n\t\t请输入您要查询的ID:");
int input = sc.nextInt();
for(i=0;i<count;i++){
if(input == emp[i].getId()){
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t***********************************************");
System.out.println("\t\t|--------------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 | 实际工资 |");
System.out.printf("\t\t| %d | %s | %d | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade (),emp[i].getBasicSalary(), (emp[i].getBasicSalary() + emp[i].getGrade()*500));
flag = true;
break;
}
}
if(flag == false){
System.out.printf("\n\t\t不存在ID为 %d 的员工,请重新尝试!\n",input);
showSalaryById();
}
}
//工资更新
public void updateSalary(){
int i;
boolean flag = false;
System.out.println("\n\t\t****************************************************");
System.out.println("\n\t\t请输入您要更改工资的员工的ID和基本工资:");
System.out.print("\t\tID:");
int num = sc.nextInt();
System.out.print("\t\t基本工资:");
int newBasicSalary = sc.nextInt();
for(i=0;i<count;i++){
if(num == emp[i].getId()){
emp[i].setBasicSalary(newBasicSalary);
emp[i].setFactSalary(emp[i].getBasicSalary() + emp[i].getGrade() * 500);
flag = true;
break;
}
}
System.out.println("\t\t|--------------------------------------------|");
System.out.println("\t\t| ID | 员工名 | 等级 | 基本工资 | 实际工资 |");
if(flag == true){
for(i =0;i<count;i++){
System.out.printf("\t\t| %d | %s | %d | %d | %d |",emp[i].getId(),emp[i].getName(),emp[i].getGrade (),emp[i].getBasicSalary(),(emp[i].getBasicSalary() + emp[i].getGrade()*500));
}
}
if(flag == false){
System.out.printf("\n\t\t不存在ID为 %d 的员工,请重新尝试!\n",num);
updateSalary();
}
}
}
//Controller类
class Controller{
Manager manager = new Manager();
//登录菜单
public void showLoginMenu(){
System.out.println("\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
while(true){
Scanner sc = new Scanner(System.in);
System.out.print("\t\t姓名:");
String name = sc.next();
System.out.print("\t\t密码:");
Eraser eraser = new Eraser('*');
Thread eraserThread = new Thread(eraser);
eraserThread.start(); //启动一个线程
String password = sc.next();
eraser.setActive(false);
System.out.println();
if("master".equals(name)&&"007".equals(password)){
System.out.printf("\t\t%s ,您已经登录成功!\n\n",name);
break;
}
else{
System.out.println("\t\t您的登录名或密码错误,请重新尝试!\n");
showLoginMenu();
}
}
}
//主菜单
public void showMainMenu(){
System.out.println("\t\t欢迎使用企业人事管理系统\n"
+"\t\t*****************************");
System.out.println("\t\t请选择您要进行的操作");
System.out.println("\t\t1.员工信息管理");
System.out.println("\t\t2.工资结算管理");
System.out.print("\n\t\t返回请按 9 ,请选择您需要的菜单(1,2,9):");//返回请按 9
Scanner sc = new Scanner(System.in);//是否可放在类声明区域
int num1 = sc.nextInt();
switch(num1){
case 1:
showEmpMenu();break;//调用本类内部方法
case 2:
showSalaryMenu();break;//调用本类内部方法
case 9:
showLoginMenu();break;
default:
System.out.println("\t\t输入错误,请重新尝试!");
showMainMenu();break;
}
}
//员工菜单
public void showEmpMenu(){
boolean flag = true;
System.out.println("\n\t\t欢迎使用企业人事管理系统");
System.out.println("\t\t*****************************");
System.out.println("\t\t1.增加员工信息");
System.out.println("\t\t2.删除员工信息");
System.out.println("\t\t3.显示员工信息");
System.out.println("\t\t4.显示特定员工");
System.out.println("\t\t5.更新员工信息");
System.out.print("\n\t\t返回请按 9,请选择您需要的操作(1,2,3,4,5,9):");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
switch(num){
case 1:
while(flag){
manager.insertEmp();
System.out.print("\n\t\t插入成功!返回上一级菜单请按 9,继续添加请按1:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showEmpMenu();break;
case 2:
while(flag){
manager.deletEmpById();
System.out.print("\n\t\t删除成功!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showEmpMenu();break;
case 3:
while(flag){
manager.showAllEmps();
System.out.print("\n\t\t显示完毕!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showEmpMenu();break;
case 4:
while(flag){
manager.showEmpById();
System.out.print("\n\t\t查询完毕!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showEmpMenu();break;
case 5:
while(flag){
manager.updateEmp();
System.out.print("\n\t\t更新完毕!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showEmpMenu();break;
case 9:
showMainMenu();break;
default:
System.out.println("\t\t输入错误,请重新选择!");
showEmpMenu();break;
}
}
//工资菜单
public void showSalaryMenu(){
System.out.println("\n\t\t欢迎使用企业人事管理系统\n"
+"\t\t*****************************");
System.out.print("\t\t请选择以下操作\n");
System.out.println("\t\t1.计算员工工资");
System.out.println("\t\t2.显示所有工资");
System.out.println("\t\t3.查询员工工资");
System.out.println("\t\t4.更新员工工资");
System.out.print("\n\t\t返回请按 9,请选择您需要的操作(1,2,3,4,9):");
boolean flag = true;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
switch(num){
case 1:
while(flag){
manager.computeSalary();
System.out.print("\n\t\t操作成功!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showSalaryMenu();break;
case 2:
while(flag){
manager.showAllSalary();
System.out.print("\n\t\t操作成功!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showSalaryMenu();break;
case 3:
while(flag){
manager.showSalaryById();
System.out.print("\n\t\t查询完毕!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showSalaryMenu();break;
case 4:
while(flag){
manager.updateSalary();
System.out.print("\n\t\t更新完毕!返回上一级菜单请按 9:");
int num1 = sc.nextInt();
if(num1 == 9){
flag = false;
}
}
showSalaryMenu();break;
case 9:
showMainMenu();break;
default:
System.out.println("\t\t输入错误,请重新选择!");
showSalaryMenu();break;
}
}
}
//屏蔽密码(接口实现)
class Eraser implements Runnable{
private boolean active;
private String mask;
public Eraser(){
this('*');
}
public Eraser(char maskChar){
active = true;
mask ="\010"+maskChar;
}
public void setActive(boolean active){
this.active = active;
}
public boolean isActive(){
return active;
}
public void run(){
while(isActive()){
System.out.print(mask);
}
}
}
//程序入口
public class EPMS{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Controller con = new Controller();
System.out.println("\t\t欢迎使用企业人事管理系统\n"
+"\t\t*****************************\n"
+"\t\t1.登录系统\n"
+"\t\t2.退出\n");
System.out.print("\t\t请选择(1,2):");
int num = scanner.nextInt();
System.out.println();
switch(num){
case 1:
con.showLoginMenu();break;
case 2:
System.exit(0);break;
}
con.showMainMenu();//显示主菜单,进而提供更多功能
}
}
更多精彩
赞助商链接