首页 > 试题广场 >

(新的 Account 类)编程练习题 9.7 中给出了一个

[问答题]
(新的 Account 类)编程练习题 9.7 中给出了一个 Account 类,如下设计一个新的 Account类: • 添加一个 String 类型的新数据域 name 来存储客户的名字。 • 添加一个新的构造方法,该方法创建一个具有指定名字、id 和收支额的账户。 • 添加一个名为 transactions的 ArrayList 类型的新数据域.用于为账户存储交易。每笔交 易都是一个 Transaction 类的实例。Transaction 类的定义如图丨丨 - 6 所示。 • 修改 withdraw 和 deposit 方法,向 transactions 数组线性表添加一笔交易。 • 其他所有属性和方法都和编程练习題 9.7 中的一样。

编写一个测试程序,创建一个年利率为 1.5%、收支额为 1000、id 为 1122 而名字为 George 的 Account。向该账户存入 30 美元、40 美元和 50 美元并从该账户中取出 5 美元、4 美 元和 2 美元。打印账户清单,显示贱户持有者名字、利率、收支额和所有的交易。 
import java.util.ArrayList; import java.util.Date; public class E1102_2019110009 { public static void main(String[] args) {
Account account=new Account("George",1122,1000,0.015);
System.out.println("账户持有者:"+account.name+"\n账户利率:"+account.annualInterestRate);
account.withDraw(30);
account.withDraw(40);
account.withDraw(50);
account.deposit(5);
account.deposit(4);
account.deposit(2);
System.out.print("交易流水:"+account.transactions);
    }
} class Account{ public String name; int id=0; double balance=0; double annualInterestRate=0;
    Date dataCreated;
    ArrayList transactions=new ArrayList(); public Account() {
    } public Account(int id, double balance) { this.id = id; this.balance = balance;
    } public Account(String name, int id, double balance) { this.name = name; this.id = id; this.balance = balance;
    } public Account(String name, int id, double balance, double annualInterestRate) { this.name = name; this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate;
    } public void setId(int id) { this.id = id;
    } public void setBalance(double balance) { this.balance = balance;
    } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate;
    } public Date getDataCreated() { return dataCreated;
    } public int getId() { return id;
    } public double getBalance() { return balance;
    } public double getAnnualInterestRate() { return annualInterestRate;
    } public void withDraw(double amount){ balance-=amount; transactions.add(new Transaction('W&(455)#39;,amount,getBalance()));
    } public void deposit(double amount){ balance+=amount; transactions.add(new Transaction('D&(656)#39;,amount,getBalance()));
    }
} class Transaction { char type; double amount; double balance;
    String description; public void setType(char type) { this.type = type;
    } public void setAmount(double amount) { this.amount = amount;
    } public void setBalance(double balance) { this.balance = balance;
    } public void setDescription(String description) { this.description = description;
    } public char getType() { return type;
    } public double getAmount() { return amount;
    } public double getBalance() { return balance;
    } public String getDescription() { return description;
    } public Transaction(char type, double amount, double balance, String description) { this.type = type; this.amount = amount; this.balance = balance; this.description = description;
    } public Transaction(char type, double amount, double balance) { this.type = type; this.amount = amount; this.balance = balance;
    } @Override  public String toString() { return "type=" + type + ", amount=" + amount + ", balance=" + balance ;
    }
}
发表于 2020-05-11 09:53:40 回复(0)