3. 코드 흐름 정리
account1.transfer(account2, amount)
- account1이 account2에게 amount(1000원) 만큼 돈을 보내려고 한다.
public void transfer(Account otherAccount, long amount) {
this.amount -= amount;
otherAccount.amount += amount;
transactions.add("송금: " + amount);
otherAccount.transactions.add("입금: " + amount);
}
- account1의 amount는 1000원 만큼 줄어들게 되고, account2의 amount는 1000원 만큼 늘어나게 된다.
- account1의 transactions arrayList에는 [송금: 1000]이 저장되고, account2의 arrayList에는 [입금: 1000]이 저장된다.
이제 저장된 transactions를 Transactions Panel에서 보여주기만 하면 된다.
transactions를 가져오는 getter (getTransactions)를 만들어준다.
Transactions Panel로 가서 저장된 transactions를 보여주는 코드를 작성한다.
for (String transaction : account1.getTransactions()) {
JLabel label = new JLabel(transaction);
this.add(label);
}
- account1에서 transactions를 가져와서 하나도 빠짐없이 모두 보여준다. -> 송금: 1000
for (String transaction : account2.getTransactions()) {
JLabel label = new JLabel(transaction);
this.add(label);
}
- account2에서 transactions를 가져와서 하나도 빠짐없이 모두 보여준다. -> 입금: 1000