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

可记录多次存取信息

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(),""));
    }
}

转载于:https://blog.51cto.com/14233078/2392524

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题。那么你的问题是有关Java银行账户account的,对吗? 银行账户account是一个常见的Java,它通常用于描述银行中的账户。在这个中,我们可以定义一些属性,例如账户号码、账户余额、账户持有人的姓名等等。同时,我们还可以定义一些方法,例如存款、取款、转账等等。 下面是一个简单的Java银行账户account的示例代码: ``` public class Account { private String accountNumber; private String accountHolderName; private double balance; public Account(String accountNumber, String accountHolderName, double balance) { this.accountNumber = accountNumber; this.accountHolderName = accountHolderName; this.balance = balance; } public String getAccountNumber() { return accountNumber; } public String getAccountHolderName() { return accountHolderName; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } public void transfer(Account destination, double amount) { withdraw(amount); destination.deposit(amount); } } ``` 在这个示例中,我们定义了一个Account,它有三个属性:账户号码、账户持有人的姓名和账户余额。我们还定义了三个方法:存款、取款和转账。这些方法可以用来对账户进行操作,例如存入现金、取出现金或向其他账户转账。 当我们创建一个Account对象时,需要提供账户号码、账户持有人的姓名和账户余额。例如: ``` Account myAccount = new Account("123456789", "John Smith", 1000.0); ``` 这个对象表示一个账户,其账户号码为“123456789”,账户持有人的姓名为“John Smith”,账户余额为1000.0元。 接下来,我们可以使用存款、取款或转账等方法来对这个账户进行操作,例如: ``` myAccount.deposit(500.0); // 存款500元 myAccount.withdraw(200.0); // 取款200元 Account destination = new Account("987654321", "Jane Doe", 0.0); myAccount.transfer(destination, 300.0); // 向destination账户转账300元 ``` 这些方法将会更账户的余额,并且我们可以随时使用getBalance()方法来查询账户的余额。 希望这个示例代码能够帮助你理解Java银行账户account的基本概念。如果你有任何问题或需要进一步的帮助,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值