该员工管理系统可以实现基本的增删改查,在父类Employee中定义员工属性,三个子类CommenEmployee、Manager、Director分别继承父类并实现父类中的sumSalary()方法,计算各自的工资。
<span style="font-size:14px;">package employee;
public class Employee { //员工父类
String ID;
String name;
String position;
int holiday;
double 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 String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int getHoliday() {
return holiday;
}
public void setHoliday(int holiday) {
this.holiday = holiday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double sumSalary(double salary){
return salary;
}
/*public void display(){
System.out.println("员工编号:" + ID + ", 姓名:" + name + ", 职位:"
+ position + ", 请假天数:" + holiday + ", 工资:" + salary);
}
*/ @Override
public String toString() {
return "员工编号:" + ID + ", 姓名:" + name + ", 职位:"
+ position + ", 请假天数:" + holiday + ", 工资:" + salary;
}
}
</span>
<span style="font-size:14px;">
</span>
<pre name="code" class="java"><span style="font-size:14px;">package employee;
public class CommenEmployee extends Employee{ //普通员工类
public CommenEmployee(){
}
public double sumSalary(double salary){
return salary+salary*0.1+salary*0.5+200;
}
}</span>
<span style="font-size:14px;">
</span>
<pre name="code" class="java"><span style="font-size:14px;">package employee;
public class Manager extends Employee{ //经理类
public Manager(){
}
public double sumSalary(double salary){
return salary+salary*0.2+salary*0.5+500;
}
}
</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">package employee;
public class Director extends Employee{ //董事长类
public Director(){
}
public double sumSalary(double salary){
return salary+salary*0.3+salary*0.8+5000;
}
}
</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">package employee;
/**
* @author zhanglianqi
*
* 2015年9月3日下午4:51:29
*/
public class Emanager { //主类
public static void main(String[] args) {
YuanGong yg=new YuanGong();
}
}
</span>
<span style="font-size:14px;">package employee;
import java.util.*;
public class YuanGong { //方法类
ArrayList<Employee> list=new ArrayList<>();
static int count = 0;
Scanner sc = new Scanner(System.in);
public YuanGong() {
choice();
}
public void choice() { // 选择页面
System.out.println("|----------------|");
System.out.println("|----1.添加-------|");
System.out.println("|----2.删除-------|");
System.out.println("|----3.修改-------|");
System.out.println("|----4.查询-------|");
System.out.println("|----0.退出-------|");
System.out.println("|----------------|");
System.out.println("请选择业务:");
int xz = sc.nextInt();
switch (xz) {
case 1:
add();
System.out.println("添加成功");
choice();
break;
case 2:
delete();
choice();
break;
case 3:
change();
choice();
break;
case 4:
selete();
choice();
break;
case 0:
break;
default:
System.out.println("输入错误,请重新输入!");
choice();
}
}
public void add() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入员工信息");
System.out.print("员工编号:");
String ID = sc.next();
System.out.print("员工姓名:");
String name = sc.next();
System.out.print("员工职务(普通员工、经理、董事长):");
String position = sc.next();
System.out.print("请假天数:");
int holiday = sc.nextInt();
System.out.print("基本工资:");
double salary = sc.nextDouble();
switch (position) {
case "普通员工":
CommenEmployee cm = new CommenEmployee();
cm.setID(ID);
cm.setName(name);
cm.setPosition(position);
cm.setHoliday(holiday);
cm.setSalary(cm.sumSalary(salary));
list.add(cm);
count++;
cm.toString();
break;
case "经理":
Manager ma = new Manager();
ma.setID(ID);
ma.setName(name);
ma.setPosition(position);
ma.setHoliday(holiday);
ma.setSalary(ma.sumSalary(salary));
list.add(ma);
count++;
ma.toString();
break;
case "董事长":
Director di = new Director();
di.setID(ID);
di.setName(name);
di.setPosition(position);
di.setHoliday(holiday);
di.setSalary(di.sumSalary(salary));
list.add(di);
count++;
di.toString();
;
break;
default:
System.out.println("输入有误,请重新输入");
add();
break;
}
}
public void delete() { // 删除
if (count > 0) {
System.out.print("请输入删除的姓名:");
String mz = sc.next();
int a = 0;
for (int i = 0; i < count; i++) {
if (list.get(i).getName().equals(mz)) { // 查找匹配的员工并输出
System.out.println(list.get(i).toString());
} else {
a++;
}
}
if (a == count) { // 表示没有找到名字相同的员工
System.out.println("系统中没有该员工,请重新输入!");
choice();
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(mz)) { // 查找匹配的员工并输出
list.remove(i);
count--;
i--;
}
}
System.out.println("删除成功");
}
} else {
System.out.println("系统中无信息");
choice();
}
}
public void change() { // 修改
final int b = count;
if (count > 0) {
System.out.print("请输入修改的员工姓名:");
String mz = sc.next();
int a = 0;
for (int i = 0; i < count; i++) {
if (list.get(i).getName().equals(mz)) { // 查找匹配的员工并输出
System.out.println(list.get(i).toString());
} else {
a++;
}
}
if (a == b) {
System.out.println("电话本中没有该联系人,请重新输入!");
choice();
} else { // 删除匹配员工,重新添加
for (int i = 0; i < count; i++) {
if (list.get(i).getName().equals(mz)) {
list.remove(i);
count--;
}
}
System.out.println("请重新输入");
add();
System.out.println("修改成功");
}
} else {
System.out.println("系统中无信息");
choice();
}
}
public void selete() { // 查询
int b = 0;
if (count > 0) { // 先判断有没有员工
System.out.println("请输入查询的姓名:");
String mz = sc.next();
for (int i = 0; i < count; i++) {
if (list.get(i).getName().equals(mz)) {
System.out.println(list.get(i).toString());
} else {
b++;
}
}
if (b == count) {
System.out.println("系统中无该员工信息");
}
} else {
System.out.println("系统中无信息");
}
}
}
</span>