Java/Java 연습 문제

[Java 연습 문제] 은행 계좌 문제

Joo.v7 2024. 8. 27. 20:38

문제 1) BankAccount 클래스 캡슐화 - private, getter/setter

문제 2) 계좌 번호 생성

문제 3) withDraw, deposit 메소드 추가

+ 추가 Lab 8-1) 계좌 이체 메소드: transferFrom

+ 추가 Lab 9-1) 계정 유형 enum: AccountType

import java.math.*;
import java.util.*;

public class BankAccount {
    private long accountNumber;
    private String ownerName;
    private BigDecimal balance;

    // 연습 2) 계좌 번호 생성. 
    private static long nextAccountNo;
    private static long nextNumber() {
        return nextAccountNo++;
    }

    // 연습 1) add setData method here.
    // BankAccount의 멤버 변수들을 private로 설정했기 때문에 이 변수들에 값을 할당하기 위함.
    public void setData(String ownerName, BigDecimal balance) {
        this.accountNumber = nextNumber();
        this.ownerName = ownerName;
        this.balance = balance;
    }

    // 연습 1) add getNumber method here. 
    public long getNumber() {
        return this.accountNumber;
    }

    public String getOwnerName() {
        return this.ownerName;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    // 연습 3) 다양한 메소드 추가
    public BigDecimal deposit(BigDecimal amount) {
        this.balance = this.balance.add(amount);
        return this.balance;
    }

    public boolean withDraw(BigDecimal amount) {
        if (amount.compareTo(this.balance) > 0) { // amount가 balance보다 큼
            return false; // 잔액 부족
        }
        else {
            balance = balance.subtract(amount);
            return true;
        }
    }
}

class CreateAccount {
    public static BankAccount createNewBankAccount(String ownerName, BigDecimal balance) {
        BankAccount newAccount = new BankAccount();
        // long accountNumber = BankAccount.nextNumber();
        newAccount.setData(ownerName, balance);

        return newAccount;
    }

    // 연습 3) 다양한 메소드 추가
    public static void TestDeposit(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter amount to deposit: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        account.deposit(amount);
       // scanner.close();
    }

    public static void TestWithDraw(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter amount to withdraw: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        if (!account.withDraw(amount)) {
            System.out.print("Insufficient funds!");
        }
    }

    public static void main(String[] args) {
       BankAccount bankAccount = CreateAccount.createNewBankAccount("Vesper Lind", new BigDecimal("0.0"));
       printBankAccount(bankAccount);
       TestDeposit(bankAccount);
       printBankAccount(bankAccount);
       TestWithDraw(bankAccount);
       printBankAccount(bankAccount);

       BankAccount bankAccount2 = CreateAccount.createNewBankAccount("Celine", new BigDecimal("0.0"));
       printBankAccount(bankAccount2);
       TestDeposit(bankAccount2);
       printBankAccount(bankAccount2);
       TestWithDraw(bankAccount2);
       printBankAccount(bankAccount2);
    }

    public static void printBankAccount(BankAccount account) {
        System.out.println("Account Number: " + account.getNumber());
        System.out.println("Owner Name: " + account.getOwnerName());
        System.out.println("Balance: " + account.getBalance());
    }
}

 

실행 결과

Account Number: 0
Owner Name: Vesper Lind
Balance: 0.0
Enter amount to deposit: 100
Account Number: 0
Owner Name: Vesper Lind
Balance: 100.0
Enter amount to withdraw: 
20
Account Number: 0
Owner Name: Vesper Lind
Balance: 80.0
Account Number: 1
Owner Name: Celine
Balance: 0.0
Enter amount to deposit: 50
Account Number: 1
Owner Name: Celine
Balance: 50.0
Enter amount to withdraw: 
50
Account Number: 1
Owner Name: Celine
Balance: 0.0

 

 


 

+ 추가) 계좌 이체 메소드: transferFrom

import java.math.*;
import java.util.Scanner;

public class BankAccount {
    private long accountNumber;
    private String ownerName;
    private BigDecimal balance;

    private static long nextAccountNumber;

    private static long nextNumber() {
        return nextAccountNumber++;
    }

    public void setData(String ownerName, BigDecimal balance) {
        this.accountNumber = nextNumber();
        this.ownerName = ownerName;
        this.balance = balance;
    }
        
    public long getNumber() {
        return this.accountNumber;
    }
        
    public String getOwnerName() {
        return this.ownerName;
    }
        
    public BigDecimal getBalance() {
        return this.balance;
    } 
    
    public BigDecimal deposit(BigDecimal amount) {
        this.balance = this.balance.add(amount);
        return this.balance;
    }

    public boolean withDraw(BigDecimal amount) {
        if (amount.compareTo(this.balance) == 1 || amount.compareTo(this.balance) == 0) {
            return false;
        }
        else {
            balance = balance.subtract(amount);
            return true;
        }
    }
    
    // 연습 1) 두 개의 파라미터를 가진 instance method 추가
    // To-do: Add transferFrom method here..
    public void transferFrom(BankAccount accForm, BigDecimal amount) {
        if(accForm.withDraw(amount)){
            this.deposit(amount);
        }
    }
}

class CreateAccount {
    public static BankAccount createNewBankAccount(String ownerName, BigDecimal balance) {
        BankAccount newAccount = new BankAccount();
        newAccount.setData(ownerName, balance);

        return newAccount;
    }

    public static void TestDeposit(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter amount to deposit: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        account.deposit(amount);
    }

    public static void TestWithDraw(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter amount to withdraw: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        if(!account.withDraw(amount)) {
            System.out.println("Insufficient funds!");
        }
    }

    public static void main(String[] args) {
        BankAccount bankAccount = CreateAccount.createNewBankAccount("Vesper Lind", new BigDecimal("0.0"));
        printBankAccount(bankAccount);
        TestDeposit(bankAccount);
        printBankAccount(bankAccount);
        TestWithDraw(bankAccount);
        printBankAccount(bankAccount);

        BankAccount bankAccount2 = CreateAccount.createNewBankAccount("Celine ", new BigDecimal("0.0"));      
        printBankAccount(bankAccount2);
        TestDeposit(bankAccount2);
        printBankAccount(bankAccount2);
        TestWithDraw(bankAccount2);
        printBankAccount(bankAccount2);
    }

    public static void printBankAccount(BankAccount account) {
        System.out.println("Account Number: " + account.getNumber());
        System.out.println("Owner Name: " + account.getOwnerName());
        System.out.println("Balance: " + account.getBalance() + "\n");
    }
}

 

/* 테스트 코드 */
import java.math.BigDecimal;

public class Test {
    public static void main(String[] args) {
        BankAccount account1 = new BankAccount();
        account1.setData("James", new BigDecimal("100.0"));
        BankAccount account2 = new BankAccount();
        account2.setData("Jason", new BigDecimal("100.0"));
        
        System.out.println("Before Transfer: ");
        displayAccountInfo(account1);
        displayAccountInfo(account2);

        account1.transferFrom(account2, new BigDecimal("10.0"));

        System.out.println("After Transfer: ");
        displayAccountInfo(account1);
        displayAccountInfo(account2);

    }

    public static void displayAccountInfo(BankAccount account) {
        System.out.println(account.getNumber() +  ", " + account.getOwnerName() + ", " + account.getBalance());
    }

}

 

실행 결과

Before Transfer: 
0, James, 100.0
1, Jason, 100.0
After Transfer: 
0, James, 110.0
1, Jason, 90.0

 

 


 

 

+ 추가) 계정 유형 enum: AccountType

import java.math.*;
import java.util.Scanner;

public class BankAccount {
    private long accountNumber;
    private String ownerName;
    private BigDecimal balance;
    
    // To-do: Add AccountType variable here.
    private AccountType accountType;

    private static long nextAccountNumber;

    // To-do: Add default constructor here
    public BankAccount() { // 기본 생성자
        this.accountNumber = nextNumber();
        this.ownerName = "Unknown";
        this.accountType = AccountType.Checking;
        this.balance = new BigDecimal("0");
    }

    // To-do: Add the first overloaded constructor here
    public BankAccount(AccountType accountType) { // 생성자 오버로딩 - 1
        this.accountNumber = nextNumber();
        this.ownerName = "Unknown";
        this.accountType = accountType;
        this.balance = new BigDecimal("0");
    }

    public BankAccount(String ownerName, BigDecimal balance) { // 생성자 오버로딩 - 2
        this.accountNumber = nextNumber();
        this.ownerName = ownerName;
        this.accountType = accountType.Checking;
        this.balance = balance;
    }

    public BankAccount(String ownerName, BigDecimal balance, AccountType accountType) {
        this.accountNumber = nextNumber();
        this.ownerName = ownerName;
        this.accountType = accountType;
        this.balance = balance;

    }

    private static long nextNumber() {
        return nextAccountNumber++;
    }
        
    public long getNumber() {
        return this.accountNumber;
    }
        
    public String getOwnerName() {
        return this.ownerName;
    }
        
    public BigDecimal getBalance() {
        return this.balance;
    } 

    // To-do: Add getAccountType method here.
    public AccountType getAccountType() {
        return this.accountType;
    }
    
    public BigDecimal deposit(BigDecimal amount) {
        this.balance = this.balance.add(amount);
        return this.balance;
    }

    public boolean withDraw(BigDecimal amount) {
        if (amount.compareTo(this.balance) == 1 || amount.compareTo(this.balance) == 0) {
            return false;
        }
        else {
            balance = balance.subtract(amount);
            return true;
        }
    }

    public void transferFrom(BankAccount accFrom, BigDecimal amount) {
        if (accFrom.withDraw(amount)) {
            this.deposit(amount);
        }
    }
}

class CreateAccount {
    /*
    public static BankAccount createNewBankAccount(String ownerName, BigDecimal balance) {
        BankAccount newAccount = new BankAccount();
        newAccount.setData(ownerName, balance);

        return newAccount;
    }
    */

    public static void TestDeposit(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter amount to deposit: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        account.deposit(amount);
        scanner.close();
    }

    public static void TestWithDraw(BankAccount account) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter amount to withdraw: ");
        BigDecimal amount = new BigDecimal(scanner.next());
        if(!account.withDraw(amount)) {
            System.out.println("Insufficient funds!");
        }
        scanner.close();
    }

    public static void main(String[] args) {
        // 생성자 테스트 코드
        BankAccount account1, account2, account3, account4;

        account1 = new BankAccount();
        account2 = new BankAccount(AccountType.Deposit);
        account3 = new BankAccount("James", new BigDecimal("100"));
        account4 = new BankAccount("Jason", new BigDecimal("500"), AccountType.Saving);

        printBankAccount(account1);
        printBankAccount(account2);
        printBankAccount(account3);
        printBankAccount(account4);
    }

    public static void printBankAccount(BankAccount account) {
        System.out.println("Account Number: " + account.getNumber());
        System.out.println("Owner Name: " + account.getOwnerName());
        System.out.println("Account Type: " + account.getAccountType());
        System.out.println("Balance: " + account.getBalance() + "\n");
    }
}

 

 

실행 결과

Account Number: 0
Owner Name: Unknown
Account Type: Checking
Balance: 0

Account Number: 1
Owner Name: Unknown
Account Type: Deposit
Balance: 0

Account Number: 2
Owner Name: James
Account Type: Checking
Balance: 100

Account Number: 3
Owner Name: Jason
Account Type: Saving
Balance: 500

 

 

2024.08.27 - [Java] - [Java] 07. 객체지향 프로그래밍 기본

 

 

2024.08.28 - [Java] - [Java] 08. 참조 타입

 

 


 

 

출처: https://github.com/gikpreet/class-programming_with_java/tree/master

 

GitHub - gikpreet/class-programming_with_java

Contribute to gikpreet/class-programming_with_java development by creating an account on GitHub.

github.com