本文实例为大家分享了java实现员工管理系统的具体代码,供大家参考,具体内容如下
本系统主要练习到的相关内容:
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修饰。小编偷了个懒,主要把时间用在测试类的编写上o( ̄ε ̄*)o。
注意:由于本系统只是将对象存于对象数组,数组初始化时定长设定为100,系统会自动初始化每个数组元素为null,所以在写测试类的方法时一定注意写好判断预防遍历赋值发生的空指针错误,小编比较笨,所以饶了好一会才写出来(¬_¬)
还有就是如果更改员工的资料时注意,若是员工的职位发生变化该怎么处理,毕竟对象变了,处理工资的方法也不一样。
以下贴出代码:
首先是父类employee
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//父类
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);
}
}
|
三个子类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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 );
}
}
|
接下来就是关键的测试类,这里完成增删改查== 有点多。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
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();
}
}
|
程序刚写完就来发帖了,简单测试并未发现什么问题,若是大家发现有什么不对的欢迎指正,谢谢啦。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/hahaha_sxm/article/details/48169711