java小题练习
Java小题练习
题目1
*公司类
属性
总资产属性
所有员工属性
方法
展示公司信息,以及每个员工的信息
发工资方法:从总资产中减去所有员工支出。
员工:
属性
姓名
工号
存款
工资*
经理、厨师、服务员均为员工子类。经理在员工基础上添加奖金属性。发完工资后,请查看所有员工的存款情况.
公司类加入调整员工工资方法:
返回值为void
参数为:被调整工资的员工与调整金额(涨工资为正数、降工资为负数)。
方法体逻辑为,在员工工资的基础上调整相应的金额
在上边需求基础上,添加评选幸运员工(随机抽取一名员工并返回)。
工具:IDEA
package ti;
/*
员工:
属性
姓名
工号
存款
工资
*/
员工类:
public class Employee {
private String name;
private String id;
private double cunKuan;
private double gongZi;
public Employee() {
}
public Employee(String name, String id, double cunKuan, double gongZi) {
this.name = name;
this.id = id;
this.cunKuan = cunKuan;
this.gongZi = gongZi;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getCunKuan() {
return cunKuan;
}
public void setCunKuan(double cunKuan) {
this.cunKuan = cunKuan;
}
public double getGongZi() {
return gongZi;
}
public void setGongZi(double gongZi) {
this.gongZi = gongZi;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", cunKuan=" + cunKuan +
", gongZi=" + gongZi +
'}';
}
}
厨师类:
package ti;
public class chuShi extends Employee{
public chuShi() {
}
public chuShi(String name, String id, double cunKuan, double gongZi) {
super(name, id, cunKuan, gongZi);
}
}
经理类:
package ti;
/经理在员工基础上添加奖金属性/
public class Manger extends Employee {
private double jingJin;
public Manger() {
}
public Manger(String name, String id, double cunKuan, double gongZi, double jingJin) {
super(name, id, cunKuan, gongZi);
this.jingJin = jingJin;
}
public double getJingJin() {
return jingJin;
}
public void setJingJin(double jingJin) {
this.jingJin = jingJin;
}
}
服务员类:
package ti;
public class waiter extends Employee{
public waiter() {
}
public waiter(String name, String id, double cunKuan, double gongZi) {
super(name, id, cunKuan, gongZi);
}
}
package ti;
import jdk.nashorn.internal.ir.CallNode;
import java.util.ArrayList;
公司类:
public class Company {
private double zongZiChan;
private ArrayList list;
public Company() {
}
public Company(double zongZiChan, ArrayList<Employee> list) {
this.zongZiChan = zongZiChan;
this.list = list;
}
public void show(){
System.out.println("公司的总资产:" + zongZiChan);
System.out.println("______________________________________________");
for (int i = 0; i < list.size(); i++) {
Employee ee = list.get(i);
System.out.println("员工的名字:" + ee.getName());
System.out.println("员工的工号:" + ee.getId());
System.out.println("员工的工资:" + ee.getGongZi());
System.out.println("员工的存款:" + ee.getCunKuan());
if (ee instanceof Manger) {
System.out.println("员工的职位是经理");
System.out.println("经理的奖金是"+((Manger) ee).getJingJin());
}
System.out.println("__________________________________________");
}
}
public double getZongZiChan() {
return zongZiChan;
}
public void setZongZiChan(double zongZiChan) {
this.zongZiChan = zongZiChan;
}
public ArrayList<Employee> getList() {
return list;
}
public void setList(ArrayList<Employee> list) {
this.list = list;
}
public void faGongZi(){
for (int i = 0; i < list.size(); i++) {
Employee ee = list.get(i);
double gongZi = ee.getGongZi();//遍历之后得到的员工工资
zongZiChan=zongZiChan-gongZi;//公司的总资产=总资产-员工的工资
ee.setCunKuan(ee.getGongZi()+ee.getCunKuan());//员工的工资=员工的存款+现在的工资
if(ee instanceof Manger){
zongZiChan=zongZiChan-((Manger) ee).getJingJin();
ee.setCunKuan( ee.getCunKuan()+((Manger) ee).getJingJin());
}
}
}
public Employee xingyun(){
Random random = new Random();
int index = random.nextInt(list.size());
Employee employee = list.get(index);
return employee;
}
}
测试类:
package ti;
import day11.demo05.GongSi;
import java.util.ArrayList;
public class Text {
public static void main(String[] args) {
double zongZiChan=10000;
chuShi chu = new chuShi(“小明”,“001”,5000,2000);
Manger ma= new Manger(“小红”,“002”,7000,3000,500);
waiter wai = new waiter(“小刚”,“003”,4000,3500);
ArrayList list = new ArrayList<>();
list.add(chu);
list.add(ma);
list.add(wai);
Company com = new Company(zongZiChan,list);
com.show();
com.faGongZi();
System.out.println("__发工资后");
com.show();
System.out.println("_幸运之星");
Employee xingyun = com.xingyun();
System.out.println(xingyun);
}
}
运行结果:
公司的总资产:100000.0
员工的名字:小明
员工的工号:001
员工的工资:2000.0
员工的存款:5000.0
员工的名字:小红
员工的工号:002
员工的工资:3000.0
员工的存款:7000.0
员工的职位是经理
经理的奖金是500.0
员工的名字:小刚
员工的工号:003
员工的工资:3500.0
员工的存款:4000.0
__发工资后
公司的总资产:91000.0
员工的名字:小明
员工的工号:001
员工的工资:2000.0
员工的存款:7000.0
员工的名字:小红
员工的工号:002
员工的工资:3000.0
员工的存款:10500.0
员工的职位是经理
经理的奖金是500.0
员工的名字:小刚
员工的工号:003
员工的工资:3500.0
员工的存款:7500.0
调薪后_
公司的总资产:91000.0
员工的名字:小明
员工的工号:001
员工的工资:2000.0
员工的存款:7000.0
员工的名字:小红
员工的工号:002
员工的工资:8000.0
员工的存款:10500.0
员工的职位是经理
经理的奖金是500.0
员工的名字:小刚
员工的工号:003
员工的工资:3500.0
员工的存款:7500.0
幸运之星_____
Employee{name=‘小明’, id=‘001’, cunKuan=7000.0, gongZi=2000.0}