本系统主要练习到的相关内容:
1、 流程控制语句
2、 类、对象
3、 封装、继承、多态
4、 方法的重载、重写
5、 访问修饰符
6、 static
需求说明:
员工信息的基本情况
---------------------------普通员工-----------------------------
属性:员工编号、员工姓名、员工职务、请假天数、基本工资
普通员工工资:
在基本工资的基础上增加10%的工作餐,50%的岗位补助,200元住房补助
基本工资+基本工资0.1+基本工资0.5+200
-----------------------------经理--------------------------------
属性:员工编号、员工姓名、员工职务、请假天数、基本工资
经理工资:
在基本工资的基础上增加20%的工作餐,50%的岗位补助,500元住房补助
基本工资+基本工资0.2+基本工资0.5+500
-------------------------------董事--------------------------------
属性:员工编号、员工姓名、员工职务、请假天数、基本工资
董事工资:
在基本工资的基础上增加8%的工作餐,30%的岗位补助,2000元住房补助,3000元投资补助
基本工资+基本工资0.08+基本工资0.3+2000+3000
--------------------------------其他---------------------------------
工资扣除部分,所有员工都一样
无请假,基本工资全发,有请假,扣除每天平均工资 * 请假天数
大体设计思路:
员工父类一个,普通员工,经理,董事长子类各一个,分别重写父类的工资方法。最后一个测试类。
实现后界面如图:
父类子类的编写没什么问题,注意尽量做好封装,属性最好用private修饰。小编偷了个懒,主要把时间用在测试类的编写上。
**注意:**由于本系统只是将对象存于对象数组,数组初始化时定长设定为100,系统会自动初始化每个数组元素为null,所以在写测试类的方法时一定注意写好判断预防遍历赋值发生的空指针错误,小编比较笨,所以饶了好一会才写出来(¬_¬)
还有就是如果更改员工的资料时注意,若是员工的职位发生变化该怎么处理,毕竟对象变了,处理工资的方法也不一样。
以下贴出代码:
首先是父类Employee
//父类
public class Employee {
String ID;
String name;
String position;
int holiday;
double salary;
public Employee(){}
public void sumSalary(){}
public void display(){
System.out.println("ID:"+ID+",姓名:"+name+",职位:"+position+",请假天数:"+holiday+",工资:"+salary);
}
}
三个子类:
public class CommonEmployee extends Employee{
@Override
public void sumSalary(){
super.salary=super.salary+super.salary*0.1+super.salary*0.5+200-super.holiday*(super.salary/30);
}
}
public class Manager extends Employee{
@Override
public void sumSalary(){
super.salary=super.salary+super.salary*0.2+super.salary*0.5+200-super.holiday*(super.salary/30);
}
}
public class Director extends Employee{
@Override
public void sumSalary(){
super.salary=super.salary+super.salary*0.08+super.salary*0.3+2000+3000-super.holiday*(super.salary/30);
}
}
接下来就是关键的测试类,这里完成增删改查 有点多。。
public class TestEMD {
static Scanner sc = new Scanner(System.in);
static Employee[] em = new Employee[100];
public static void caoZuo() {
System.out.println("---- 工资管理系统 ----");
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("请输入你要选择的操作:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
switch (s) {
case "1":
addEmployee();
break;
case "2":
delEmployee();
break;
case "3":
updateEmployee();
break;
case "4":
queryEmployee();
break;
case "0":
System.out.println("谢谢使用O(∩_∩)O");
break;
default:
System.out.println("指令错误请重新输入!");
caoZuo();
break;
}
}
public static void addEmployee() {
System.out.println("------增加员工------");
System.out.println("请输入相关信息:");
System.out.print("ID:");
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 "普通员工":
Employee a = new CommonEmployee();
a.ID = id;
a.name = name;
a.position = "普通员工";
a.holiday = holiday;
a.salary = salary;
a.sumSalary();
for (int i = 0; i < 100; i++) {
if (em[i] == null) {
em[i] = a;
System.out.println("添加成功!");
em[i].display();
break;
} else {
continue;
}
}
break;
case "经理":
Employee b = new Manager();
b.ID = id;
b.name = name;
b.position = "经理";
b.holiday = holiday;
b.salary = salary;
b.sumSalary();
for (int i = 0; i < 100; i++) {
if (em[i] == null) {
em[i] = b;
System.out.println("添加成功!");
em[i].display();
break;
} else {
continue;
}
}
break;
case "董事长":
Employee c = new Director();
c.ID = id;
c.name = name;
c.position = "董事长";
c.holiday = holiday;
c.salary = salary;
c.sumSalary();
for (int i = 0; i < 100; i++) {
if (em[i] == null) {
em[i] = c;
System.out.println("添加成功!");
em[i].display();
break;
} else {
continue;
}
}
break;
default:
System.out.println("不存在此职务,请重新输入!");
addEmployee();
break;
}
caoZuo();
}
public static void delEmployee() {
System.out.println("----------删除员工---------");
System.out.println("请输入员工姓名:");
String n = sc.next();
for (int i = 0; i < 100; i++) {
if (em[i] != null) {
if (em[i].name.equals(n)) {
System.out.println("你要删除的是:" + em[i].toString());
System.out.println("你确定要删除吗?\n [Y]确定,[N]取消");
String s = sc.next();
if (s.equals("y")) {
em[i] = null;
System.out.println("删除成功!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
caoZuo();
} else if (s.equals("n")) {
caoZuo();
} else {
System.out.println("输入指令不正确,请重新输入!");
delEmployee();
}
} else {
if (i != 99) {
continue;
} else {
System.out.println("你输入的账号不存在!请重新输入!");
delEmployee();
}
}
} else {
if (i != 99) {
continue;
} else {
System.out.println("你输入的账号不存在!请重新输入!");
delEmployee();
}
}
}
}
public static void updateEmployee() {
System.out.println("--------------修改员工资料-------------");
System.out.println("请输入你要修改的姓名:");
String s = sc.next();
out: for (int i = 0; i < 100; i++) {
if (em[i] != null) {
if (em[i].name.equals(s)) {
System.out.println("你要修改的是:");
em[i].display();
System.out.println("请重新输入相关信息:");
System.out.print("ID:");
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 "普通员工":
if (em[i].position.equals("普通员工")) {
em[i].ID = id;
em[i].name = name;
em[i].position = position;
em[i].holiday = holiday;
em[i].salary = salary;
em[i].sumSalary();
System.out.println("修改成功!");
em[i].display();
} else {
em[i] = null;
Employee a = new CommonEmployee();
a.ID = id;
a.name = name;
a.position = "普通员工";
a.holiday = holiday;
a.salary = salary;
a.sumSalary();
for (int j = 0; j < 100; j++) {
if (em[j] == null) {
em[j] = a;
System.out.println("修改成功!");
em[j].display();
break;
} else {
continue;
}
}
}
break;
case "经理":
if (em[i].position.equals("经理")) {
em[i].ID = id;
em[i].name = name;
em[i].position = position;
em[i].holiday = holiday;
em[i].salary = salary;
em[i].sumSalary();
System.out.println("修改成功!");
em[i].display();
} else {
em[i] = null;
Employee b = new Manager();
b.ID = id;
b.name = name;
b.position = "经理";
b.holiday = holiday;
b.salary = salary;
b.sumSalary();
for (int j = 0; j < 100; j++) {
if (em[j] == null) {
em[j] = b;
System.out.println("修改成功!");
em[j].display();
break;
} else {
continue;
}
}
}
break;
case "董事长":
if (em[i].position.equals("董事长")) {
em[i].ID = id;
em[i].name = name;
em[i].position = position;
em[i].holiday = holiday;
em[i].salary = salary;
em[i].sumSalary();
System.out.println("修改成功!");
em[i].display();
} else {
em[i] = null;
Employee c = new Director();
c.ID = id;
c.name = name;
c.position = "董事长";
c.holiday = holiday;
c.salary = salary;
c.sumSalary();
for (int j = 0; j < 100; j++) {
if (em[j] == null) {
em[j] = c;
System.out.println("添加成功!");
em[j].display();
break;
} else {
continue;
}
}
}
break;
default:
System.out.println("不存在此职务,请重新输入!");
addEmployee();
break;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
caoZuo();
} else {
if (i != 99) {
continue out;
} else {
System.out.println("你输入的员工不存在!请重新输入!");
caoZuo();
}
}
} else {
if (i != 99) {
continue out;
} else {
System.out.println("你输入的员工不存在!请重新输入!");
caoZuo();
}
}
}
}
public static void queryEmployee() {
System.out.println("--------------所有员工信息---------------");
for (int i = 0; i < 100; i++) {
if (em[i] != null) {
em[i].display();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
caoZuo();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestEMD.caoZuo();
}
}
程序刚写完就来发帖了,简单测试并未发现什么问题,新手同学可以简单参考下,谢谢