Java實現(xiàn)員工管理系統(tǒng)
本文實例為大家分享了Java實現(xiàn)員工管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
本系統(tǒng)主要練習(xí)到的相關(guān)內(nèi)容:
1、 流程控制語句
2、 類、對象
3、 封裝、繼承、多態(tài)
4、 方法的重載、重寫
5、 訪問修飾符
6、 static
需求說明:
員工信息的基本情況
—————————普通員工—————————–
屬性:員工編號、員工姓名、員工職務(wù)、請假天數(shù)、基本工資
普通員工工資:
在基本工資的基礎(chǔ)上增加10%的工作餐,50%的崗位補助,200元住房補助
基本工資+基本工資*0.1+基本工資*0.5+200
—————————–經(jīng)理——————————–
屬性:員工編號、員工姓名、員工職務(wù)、請假天數(shù)、基本工資
經(jīng)理工資:
在基本工資的基礎(chǔ)上增加20%的工作餐,50%的崗位補助,500元住房補助
基本工資+基本工資*0.2+基本工資*0.5+500
——————————-董事——————————–
屬性:員工編號、員工姓名、員工職務(wù)、請假天數(shù)、基本工資
董事工資:
在基本工資的基礎(chǔ)上增加8%的工作餐,30%的崗位補助,2000元住房補助,3000元投資補助
基本工資+基本工資*0.08+基本工資*0.3+2000+3000
——————————–其他———————————
工資扣除部分,所有員工都一樣
無請假,基本工資全發(fā),有請假,扣除每天平均工資 * 請假天數(shù)
大體設(shè)計思路:

員工父類一個,普通員工,經(jīng)理,董事長子類各一個,分別重寫父類的工資方法。最后一個測試類。
實現(xiàn)后界面如圖:

父類子類的編寫沒什么問題,注意盡量做好封裝,屬性最好用private修飾。小編偷了個懶,主要把時間用在測試類的編寫上o( ̄ε ̄*)o。
注意:由于本系統(tǒng)只是將對象存于對象數(shù)組,數(shù)組初始化時定長設(shè)定為100,系統(tǒng)會自動初始化每個數(shù)組元素為null,所以在寫測試類的方法時一定注意寫好判斷預(yù)防遍歷賦值發(fā)生的空指針錯誤,小編比較笨,所以饒了好一會才寫出來(¬_¬)
還有就是如果更改員工的資料時注意,若是員工的職位發(fā)生變化該怎么處理,畢竟對象變了,處理工資的方法也不一樣。
以下貼出代碼:
首先是父類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+",請假天數(shù):"+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);
}
}
接下來就是關(guān)鍵的測試類,這里完成增刪改查== 有點多。
public class TestEMD {
static Scanner sc = new Scanner(System.in);
static Employee[] em = new Employee[100];
public static void caoZuo() {
System.out.println("---- 工資管理系統(tǒng) ----");
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("請輸入相關(guān)信息:");
System.out.print("ID:");
String id = sc.next();
System.out.print("姓名:");
String name = sc.next();
System.out.print("職務(wù):");
String position = sc.next();
System.out.print("請假天數(shù):");
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 "經(jīng)理":
Employee b = new Manager();
b.ID = id;
b.name = name;
b.position = "經(jīng)理";
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("不存在此職務(wù),請重新輸入!");
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("請重新輸入相關(guān)信息:");
System.out.print("ID:");
String id = sc.next();
System.out.print("姓名:");
String name = sc.next();
System.out.print("職務(wù):");
String position = sc.next();
System.out.print("請假天數(shù):");
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 "經(jīng)理":
if (em[i].position.equals("經(jīng)理")) {
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 = "經(jīng)理";
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("不存在此職務(wù),請重新輸入!");
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();
}
}
程序剛寫完就來發(fā)帖了,簡單測試并未發(fā)現(xiàn)什么問題,若是大家發(fā)現(xiàn)有什么不對的歡迎指正,謝謝啦。
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文
這篇文章主要介紹了Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文件的方法,需要的朋友可以參考下2015-12-12
Java中ArrayBlockingQueue和LinkedBlockingQueue
這篇文章主要介紹了Java中ArrayBlockingQueue和LinkedBlockingQueue,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-09-09
Java switch()括號內(nèi)參數(shù)的類型要求詳解
這篇文章主要介紹了Java switch()括號內(nèi)參數(shù)的類型要求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
深入了解Java中Synchronized關(guān)鍵字的實現(xiàn)原理
synchronized是JVM的內(nèi)置鎖,基于Monitor機制實現(xiàn),每一個對象都有一個與之關(guān)聯(lián)的監(jiān)視器?(Monitor),這個監(jiān)視器充當(dāng)了一種互斥鎖的角色,本文就詳細(xì)聊一聊Synchronized關(guān)鍵字的實現(xiàn)原理,需要的朋友可以參考下2023-06-06
使用redisTemplate的scan方式刪除批量key問題
這篇文章主要介紹了使用redisTemplate的scan方式刪除批量key問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

