20230304 [Java] 4인용 마카오뱅크 프로그램을 만들며 배운 것들

Daisy🌷·2023년 3월 4일
0

1. 멤버 변수

  • 아래와 같이 코드를 짜면 transfer를 할 때마다 새로운 ArrayList가 생기게 되어 이전의 거래내역 위에 직전 거래내역이 덮어 씌워진다. 그래서 마지막 거래 내역만 보이게 된다.
  • 따라서 다음과 같이 transactions를 멤버 변수로 두어 ArrayList는 한 번만 만들어주고 하나의 ArrayList에 송금한 금액을 계속해서 더해주어 저장되도록 해야 한다.

2. 매개 변수

  • Account라는 클래스에 내 거래 내역과 상대의 거래 내역을 모두 보관하려 했다. 나의 계좌에서 상대의 거래 내역을 보여주는 것은 이상하다.
  • yourTransactions는 지우고 매개변수로 들어오는 otherAccount의 transactions에 거래 내역이 저장되도록 코드를 수정해준다.

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
profile
티스토리로 블로그를 이전했습니다. 😂 구경 오세요! 👉🏻 https://u-ryu-logs.tistory.com

0개의 댓글