계좌에서 입금 출금 계좌이체하는 프로그램을 자바로 만들고 있습니다.

class BankAccount {

private int accountNumber;

private int balance;

private static int changeNumber =0;

public BankAccount(int accountNumber) {

this.accountNumber = changeNumber+1;

changeNumber = accountNumber;

this.balance = 0;

}

public BankAccount(int accountNumber ,int balance1) {

this.accountNumber = changeNumber+1;

changeNumber = accountNumber;

this.balance = balance1;

}

public void deposit(int depositMoney) {

balance += depositMoney;

}

public boolean withdraw(int withdrawMoney){

if (withdrawMoney > balance) {

System.out.print("잔액이 부족합니다. 다시 확인해주세요");

return false;

}

else {

balance -= withdrawMoney;

return true;

}

}

public boolean transfer(int accountNumber , int transferMoney) {

if(transferMoney > balance) {

System.out.print("잔액이 부족합니다. 다시 확인해주세요.");

return false;

}

else {

to.setBalance(to.getBalance()+transferMoney);

}

return true;

}

public int getNumber()

{

return accountNumber;

}

public int getBalance()

{

return balance;

}

}

다른 부분은 괜찮은데 transfer 부분에서 다른 객체에서 값을 어떻게 받아와야 할지 모르겠습니다 !! 좋은 답변 해주시면 감사드리겠습니다.

    2개의 답변이 있어요!

    • 크게 2가지 방법을 알려드리겠습니다.

      1. 객체 내에서 보내기

      // transfer을 보낼 객체를 변수로 받아옵니다. public boolean transfer(BankAccount objToSendTo, int transferAmount) { if (transferAmount > this.balance) { System.out.println("잔액 부족"); return false; } else { this.withdraw(transferAmount); // 현재 계좌에서 돈을 출금합니다. objToSendTo.deposit(transferAmount); // 다른 계좌에 돈을 입금합니다. return true; } }

      2. 객체 외부에 있는 상위 객체에서 static method 사용 (꼭 static이어야할 필요는 없습니다.)

      // acc1 에서 acc2로 transferAmount만큼 송금 public static boolean transfer(BankAccount acc1, BankAccount acc2, int transferAmount) { if (acc1.getBalance() < transferAmount) { System.out.println("잔액 부족"); return false; } else { acc1.withdraw(transferAmount); // acc1에서 출금 acc2.deposit(transferAmount); // acc2에 입금 } }

    • 계정정보는 db를 활용하면 좋겠지만 연동에 제약이 있다면

      db처럼 활용할 수 있는 데이터 타입을 하나 만들어서 활용하는 방법을 적용하면 좋을것 같습니다.

      예제에서는 BankAccount 파일만 올려주셔서 BankAccount.bankAccounts 를 넣었지만 이를 분리하여 리팩토링 하시면 더 좋은 코드가 될것입니다. (local db를 활용한다면 더 좋은 코드가 될것입니다. 예제는 https://www.baeldung.com/spring-boot-h2-database)

      import java.util.HashMap; import java.util.Map; public class BankAccount { // 계정 생성시 저정할 데이터 타입 public static Map<Integer, BankAccount> bankAccounts = new HashMap<>(); private static int changeNumber = 0; public static void main(String[] args) { // 계정 생성 new BankAccount(1, 3000); new BankAccount(2, 10000); new BankAccount(3, 100000); // 계정을 조회하여 이체 BankAccount bankAccount = bankAccounts.get(2); bankAccount.transfer(1, 5000); // 이체한 계정 조회 BankAccount transferBankAccount = bankAccounts.get(1); System.out.println(transferBankAccount); } private int accountNumber; private int balance; public BankAccount(int accountNumber) { this.accountNumber = changeNumber + 1; changeNumber = accountNumber; this.balance = 0; // 계정 생성시 해당 데이터 저장 bankAccounts.put(accountNumber, this); } public BankAccount(int accountNumber, int balance1) { this.accountNumber = changeNumber + 1; changeNumber = accountNumber; this.balance = balance1; // 계정 생성시 해당 데이터 저장 bankAccounts.put(accountNumber, this); } public void deposit(int depositMoney) { balance += depositMoney; } public boolean withdraw(int withdrawMoney) { if (withdrawMoney > balance) { System.out.print("잔액이 부족합니다. 다시 확인해주세요"); return false; } else { balance -= withdrawMoney; return true; } } public boolean transfer(int accountNumber, int transferMoney) { if (transferMoney > balance) { System.out.print("잔액이 부족합니다. 다시 확인해주세요."); return false; } else { // 이체할 데이터 조회 및 이체 BankAccount to = bankAccounts.get(accountNumber); to.setBalance(to.getBalance() + transferMoney); // 이체 후 잔액 감산 balance -= transferMoney; } return true; } public int getNumber() { return accountNumber; } public int getBalance() { return balance; } public void setBalance(int balance) { this.balance = balance; } }