本章作业2(面向对象高级)

该博客介绍了使用面向对象编程实现员工工资计算,包括部门经理和普通员工的工资计算逻辑。同时,还展示了如何扩展类来模拟银行存款和取款过程,包括手续费和每月利息的计算。通过具体的代码实例,展示了类的继承、方法重写以及交易计数等功能。
摘要由CSDN通过智能技术生成

(3)通过继承实现员工工资核算打印功能

父类:员工类

子类:部门经理类,普通员工类

(1)部门经理工资 = 1000+单日工资天数等级(1.2)

(2)普通员工工资 = 单日工资天数等级(1.2)

(3)员工属性:姓名,单日工资,工作天数

(4)员工方法(打印工资)

(5)普通员工及部门经理都是员工子类,需要重写打印工资方法

(6)定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部门经理对象,调用打印工资方法输入工资

  • 员工类:

package com.cyn.modifier;

public class Employ {
    private String name;
    private double salary;
    private int day;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public C(String name, double salary, int day) {
        this.name = name;
        this.salary = salary;
        this.day = day;
    }
    public void information(){
        System.out.println("姓名"+name+"\t\t单日工资"+salary+"\t工作天数"+day);

    }
}
  • 普通员工类:

public class General extends Employ {
   private double grade;
    public A(String name, double salary, int day, double grade) {
        super(name, salary, day);
        this.grade = grade;
    }
    public void information(){
        super.information();
        System.out.println("普通员工\t"+getName()+"的实得工资为"+getSalary()*grade*getDay());

    }
}
  • 经理类:

public class Manger extends Employ{
    private double grade;
    private int price;

    public D(String name, double salary, int day, double grade, int price) {
        super(name, salary, day);
        this.grade = grade;
        this.price = price;
    }

    public void information(){
        System.out.println("部门经理"+getName()+"的实得工资为"+(price+getSalary()*grade*getDay()));
    }
}

测试类:

public class Test{
    public static void main(String[] args){
        General general = new General("JACK",1000,28,1.0);
        general.information();
        Manger manger = new Manger("ROSE",3000,30,1.2,1000);
        manger.information();
 }
}

 

(4)拓展c类

①拓展c类:要求对每次存款和取款都收取一美元的手续费

②拓展A类:新类每个月都有利息产生,并且有每月三次免手续费的存款或取款,在earnMonthlyInterst方法中重置交易计数

C类:

package com.cyn.modifier;

public class C {
   private double balance;

    public C(double intialBalance) {
        this.balance = intialBalance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    //存款
    public void deposit(double amount){
        balance += amount;
    }
    //取款
    public void withdraw(double amount){
        balance -= amount;
    }
}

A类:

package com.cyn.modifier;


public class A extends C {
    public A(double intialBalance) {
        super(intialBalance);
    }

    @Override
    public void deposit(double amount) {
        super.deposit(amount-1);//存款,相当于存的时候减去一元
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount+1);//取款,多减一元
    }
}

 B类:

package com.cyn.modifier;

public class B extends C{
    private int count = 3;
    private double rate = 0.03;

    public B(double intialBalance) {
        super(intialBalance);
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
    public void eranMonthlyInterest(){
        count = 3;
        super.deposit(getBalance()*rate);
    }

    @Override
    public void deposit(double amount) {
        if(count > 0) {
            super.deposit(amount);
        }else{
            super.deposit(amount-1);
        }
        count--;
    }

    @Override
    public void withdraw(double amount) {
        if(count > 0) {
            super.withdraw(amount);
        }else{
            super.withdraw(amount+1);
        }
        count--;
    }
}

 Test类:

public class Test1 {
    public static void main(String[] args) {
//        A a = new A(1000);
//        a.deposit(100);//存100,1100 -1 = 1099
//        System.out.println(a.getBalance());
//        a.withdraw(20);//取20,1099 - 20 - 1 = 1078
//        System.out.println(a.getBalance());
        B b = new B(1000);
        b.deposit(1000);
        b.deposit(1000);
        b.deposit(1000);//4000
        System.out.println(b.getBalance());
        b.deposit(1000);//4000 + 1000 -1 = 4999
        System.out.println(b.getBalance());
        b.eranMonthlyInterest();
        System.out.println(b.getBalance());//5148.97
        b.deposit(1000);//5148.97+1000
        System.out.println(b.getBalance());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值