温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java编程 新账户类(Newaccount)(java继承)

发布时间:2020-04-06 00:53:33 来源:网络 阅读:301 作者:梦T醒 栏目:编程语言

可记录多次存取信息

import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;//声明

public class TestNewaccount //测试类
{
    public static void main(String[] args) 
    {
        NewAccount account = new NewAccount("Mike",1122,1000);
        account.setAnnualInterestRate(1.5/100);
        account.deposit(30);
        account.deposit(40);
        account.deposit(50);
        account.withDraw(5);
        account.withDraw(4);
        account.withDraw(2);
        for(int i=0;i<account.transaction.size();i++)
        {
            System.out.println(account.transaction.get(i));
        }
    }
}
class Account 
{
    private int id=0;
    private double balance=0;
    static private double annualInterestRate=0;
    private Date dateCreated;
    public Account()
    {
        dateCreated=new Date();
    }
    public Account(int x,double y)
    {
        id=x;
        balance=y;
        dateCreated=new Date();
    }
    public void setId(int x)
    {
        id=x;
    }
    public int getId()
    {
        return id;
    }
    public void setBalance(double x)
    {
        balance=x;
    }
    public double getBalance()
    {
        return balance;
    }
    public void setAnnualInterestRate(double x)
    {
        annualInterestRate=x;
    }
    public double getAnnualInterestRate()
    {
        return annualInterestRate;
    }
    public Date getDateCreated()
    {
        return dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return annualInterestRate/12;
    }
    public double getMonthlyInterest()
    {
        return getMonthlyInterestRate()*balance;
    }
    public void withDraw(double money)
    {
        if(balance>=money)
        balance-=money;
    }
    public void deposit(double money)
    {
        balance+=money;
    }
}
class Transaction//用户交易信息类
{
    private char type;
    private Date date;
    private double money;
    private double balance;
    private String description;
    public Transaction(char type,double money,double balance,String description)
    {
        this.type=type;//交易类型(取款,存款)
        date=new Date();//时间
        this.money=money;//存取的多少
        this.balance=balance;//交易完还剩多少钱
        this.description=description;//交易备注
    }
    public String toString()
    {
        return "Type:"+type+"  Money:"+money+"  Balance:"+balance+"  Date:"+date+" "+description;
    }
}
class NewAccount extends Account//子类
{
    private String name;//新增变量
    ArrayList transaction=new ArrayList();//ArrayList方法
    public NewAccount(String name,int id,double balance)
    {
        super(id,balance);
        this.name=name;
    }
    public void withDraw(double x)//取钱
    {
        if(getBalance()>x)
        {
            setBalance(getBalance()-x);
            transaction.add(new Transaction('W',x,getBalance(),""));//往ArrayList方法中加元素
        }
    }
    public void deposit(double x)//存钱
    {
        setBalance(getBalance()+x);
        transaction.add(new Transaction('D',x,getBalance(),""));
    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI