-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCh11Q08.java
73 lines (64 loc) · 1.96 KB
/
Ch11Q08.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.ArrayList;
import java.util.Date;
public class Ch11Q08 {
public static void main(String[] args) {
NewAccount newAccount = new NewAccount(1122,1000,"George");
newAccount.setAnnualInterestRate(1.5);
// newAccount.set
newAccount.deposit(30);
newAccount.deposit(40);
newAccount.deposit(50);
newAccount.withDraw(5);
newAccount.withDraw(4);
newAccount.withDraw(2);
newAccount.printTransactions();
}
}
class NewAccount extends Account{
private String name;
// NewAccount ctor
private ArrayList<Transaction> transactions = new ArrayList<>() ;
public NewAccount(int id, double balance,String Name) {
super(id, balance);
name = Name;
}
@Override
public void withDraw(double amount) {
super.withDraw(amount);
transactions.add(new Transaction('W',amount,getBalance(),"取款成功"));
}
@Override
public void deposit(double amount) {
super.deposit(amount);
transactions.add(new Transaction('D',amount,getBalance(),"存款成功"));
}
public void printTransactions() {
for(Transaction transaction: transactions){
System.out.println(transaction.toString());
}
}
}
class Transaction {
private Date date = new Date();
private char type; // 'D' 'W'
private double amount;
private double balance;
private String description; // KFC
Transaction(char Type, double Amount, double Balance, String Description) {
type = Type;
amount = Amount;
balance = Balance;
description = Description;
date = new Date();
}
@Override
public String toString() {
return "Transaction{" +
"date=" + date +
", type=" + type +
", amount=" + amount +
", balance=" + balance +
", description='" + description + '\'' +
'}';
}
}