Account类

题目描述:
现有类Account表示银行账户,FixedDepositAccount 表示定期存款账户,BankingAccount表示理财账户
要求:1.为FixedDepositAccount和BankingAccount提供参数为()String idCard,double balance)的构造方法,在该构造方法中调用Account的构造方法
2.在FixedDepositAccount和BankingAccount中重写getInterest()方法计算利息
3.在FixedDepositAccount和BankingAccount中重写toString方法,输出账户的所有信息如
001账户10000.0元存款的36月存款利息:10500.00(年利率为3.50%)
002账户10000.0元存款的182天的存款利息:2592.88(年利率为5.20%)
说明:定期存款账户以月为利息的计算单位,计算利息的公式:利息=余额年利率月数/12。理财账户以天为单位,计算利息的公式:利息=余额年利率天数/365
ps:掌握继承层次下的类的设计,构造方法的设计和调用,以及父类方法的重写

import java.text.DecimalFormat;
import java.util.Scanner;
public class mainClass {
    public static void main(String[] args) {
        //建立一个3年期的定期账户
        Scanner scn=new Scanner (System.in);
        double balance=scn.nextDouble();
        DecimalFormat df=new DecimalFormat("#.00");
        FixedDepositAccount fda = new FixedDepositAccount();
        fda.setId("001");
        fda.setBalance(balance);
        fda.setMonths(36);
        fda.setRate(0.035);
        //DecimalFormat dr=new DecimalFormat("#.00");
        //建立一个182天的理财账户
        BankingAccount ba = new BankingAccount();
        ba.setId("002");
        ba.setBalance(balance);
        ba.setDays(182);
        ba.setRate(0.052);
        System.out.print(fda.toString()+df.format(fda.getInterest()));
        System.out.println();
        System.out.print(ba.toString()+df.format(ba.getInterest()));
           
    }
}
class Account{  
    //private String id;
    private double balance;
    public Account() {
           
    }
    public Account(double balance) {
       // this.id=id;
        this.balance=balance;
    }
  //  public String getIdCard() {
    //    return id;
  //  }
  //  public void setIdCard(String idCard) {
   //     this.id = idCard;
   // }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
       
}
class FixedDepositAccount extends Account{
    private String Id;
    private double balance;
    private int months;
    private double rate;
    DecimalFormat df=new DecimalFormat("#.00");
    public FixedDepositAccount() {
           
    }
    public FixedDepositAccount(String Id,double balance) {
        super(balance);
    }
    public String getId() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public int getMonths() {
        return months;
    }
    public void setMonths(int months) {
        this.months = months;
    }
    public double getRate() {
        return rate;
    }
    public void setRate(double rate) {
        this.rate = rate;
    }
    public double getInterest() {
        double interest;
        interest=balance*rate*(months/12.0);
        return interest;
    }
    public String toString() {
        return Id+"账户利息:";
    }
}
class BankingAccount extends Account{
    private String Id;
    private double balance;
    private int days;
    private double rate;
    DecimalFormat df=new DecimalFormat("#.00");
    public BankingAccount(){
           
    }
    public BankingAccount(double balance) {
        super(balance);
    }
    public String getId() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public int getDays() {
        return days;
    }
    public void setDays(int days) {
        this.days = days;
    }
    public double getRate() {
        return rate;
    }
    public void setRate(double rate) {
        this.rate = rate;
    }
    public double getInterest() {
        double interest;
        interest=balance*rate*(days/365.0);
        return interest;
    }
    public String toString() {
        return Id+"账户:";
    }
}

你可能感兴趣的:(Account类)