Java、新的Account类

 


————————————————————————————————————————————
                NewAccount
————————————————————————————————————————————
-id: int
-balance: double
-name: String
+annualInterestRate:double
-dateCreated: Date
-transactions: ArrayList<Transaction>
————————————————————————————————————————————
+NewAccount()
+NewAccount(id: int, balance: double)
+NewAccount(name: String, id: int, balance: double)
+withdraw(amount: double): boolean
+deposit(amount: double): boolean
+toString(): String
+getId(): int
+setId(id: int): void
+getName(): String
+setName(name: String): void
+getTransaction(): ArrayList<Transaction>
+getBalance(): double
+setBalance(balance: double): void
+getAnnualInterestRate(): double
+setAnnualInterestRate(annualInterestRate: double): void
+getDateCreated(): Date
—————————————————————————————————————————————
————————————————————————————————————————————
                Transaction
————————————————————————————————————————————
-date: Date
-type: char
-amount: double
-balance: double
-description: String
————————————————————————————————————————————
+Transaction(type: char, amount: double, 
        balance: double, description: String)
+getDate(): Date
+getType(): char
+setType(type: char): void
+getAmount(): double
+setAmount(amount: double): void
+getBalance(): double
+setBalance(balance: double): void
+getDescription(): String
+setDescription(description: String): void
————————————————————————————————————————————

 


package pack1;

import java.util.ArrayList;
import java.util.Date;

public class NewAccount {
    private int id; //账号
    private double balance; //余额
    private String name;    //客户名
    public static double annualInterestRate;    //年利率
    private Date dateCreated = new Date();  //开户时间
    private ArrayList<Transaction> transactions = new ArrayList<>();    //存储交易

    public NewAccount() {
    }

    /**带指定账号、余额的构造方法*/
    public NewAccount(int id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    /**带指定账号、余额、客户名的构造方法*/
    public NewAccount(int id, double balance, String name) {
        this.id = id;
        this.balance = balance;
        this.name = name;
    }

    /**取款*/
    public boolean withdraw(double amount) {
        if(amount < 0 || amount > balance)
            return false;
        balance -= amount;
        transactions.add(new Transaction('W', amount, balance, "withdrawal"));
        return true;
    }

    /**存款*/
    public boolean deposit(double amount) {
        if (amount < 0) return false;
        balance += amount;
        transactions.add(new Transaction('D', amount, balance, "deposit"));
        return true;
    }

    @Override   /**返回账号、余额、开户日期的字符串*/
    public String toString() {
        return String.format("Name: %-10sId: %-10dAnnual Interest Rate(%%): " + "
                 %-10.2fBalance: $%-10.2fDate created: %-10s",
                name, id, getAnnualInterestRate(), balance, dateCreated);
    }

    /**返回客户名*/
    public String getName() {
        return name;
    }

    /**设置客户名*/
    public void setName(String name) {
        this.name = name;
    }

    /**返回交易信息的数组列表*/
    public ArrayList<Transaction> getTransactions() {
        return transactions;
    }

    /**返回账号*/
    public int getId() {
        return id;
    }

    /**设置账号*/
    public void setId(int id) {
        this.id = id;
    }

    /**返回余额*/
    public double getBalance() {
        return balance;
    }

    /**设置余额*/
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**返回年利率的静态方法*/
    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    /**设置年利率的静态方法*/
    public static void setAnnualInterestRate(double annualInterestRate) {
        NewAccount.annualInterestRate = annualInterestRate;
    }

    /**返回开户日期*/
    public Date getDateCreated() {
        return dateCreated;
    }
}


 


package pack1;

import java.util.Date;

public class Transaction {
    private Date date = new Date(); //交易日期
    private char type;  //交易类型
    private double amount;  //交易数量
    private double balance; //交易后余额
    private String description; //交易描述

    /**带指定交易类型、数量、余额、描述的构造方法*/
    public Transaction(char type, double amount, double balance, String description) {
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.description = description;
    }

    @Override   /**返回交易日期、类型、数量、余额、描述的字符串*/
    public String toString() {
        return String.format("%-10s%10c%30.2f%28.2f%30s", date, type, amount, 
                balance, description);
    }

    /**返回交易类型*/
    public char getType() {
        return type;
    }

    /**设置交易类型*/
    public void setType(char type) {
        this.type = type;
    }

    /**返回交易数量*/
    public double getAmount() {
        return amount;
    }

    /**设置交易数量*/
    public void setAmount(double amount) {
        this.amount = amount;
    }

    /**返回交易余额*/
    public double getBalance() {
        return balance;
    }

    /**设置交易余额*/
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**返回交易描述*/
    public String getDescription() {
        return description;
    }

    /**设置交易描述*/
    public void setDescription(String description) {
        this.description = description;
    }

    /**返回交易日期*/
    public Date getDate() {
        return date;
    }
}



package pack1;

public class TestNewAccount {
    public static void main(String[] args) {
        NewAccount.setAnnualInterestRate(1.5);
        NewAccount account = new NewAccount(1122, 1000, "George");
        account.deposit(30);
        account.deposit(40);
        account.deposit(50);
        account.withdraw(5);
        account.withdraw(4);
        account.withdraw(2);

        System.out.println(account + "\n");
        System.out.printf("\t\t%-27s%-27s%-27s%-27s%-27s\n", "Date", "Type(W/D)", 
             "Amount($)", "Balance($)", "Description");
        for (Transaction transaction : account.getTransactions())
            System.out.println(transaction);
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值