총 10분 중 11분
2001
시즌 2개, 그리고 영화
시즌 2: 5화 “아일랜드”
출연: 이나영, 김민준, 김민정, 현빈
장르: 애초에 역경을 딛고 이룩하는 숭고한 사랑이란 없다. 그 역경 자체가 사랑이다.
프로그램 특징: 그 곳에서 살아남는 사랑이 어떤 모습으로 걸어오는지 기다려 보고 싶다.
Programming [JAVA] 이것이 자바다(3판) 6장 20번 답안

20. 키보드로부터 계좌 정보를 입력받아 계좌를 관리하는 프로그램입니다. 계좌는 Account 객체로 생성되고 BankApplication에서 길이 100 Account[] 배열로 관리됩니다. 



Account.class

package ThisisJAVA.ch06.bank;

public class Account {
    static final int MIN_BALANCE = 0;
    static final int MAX_BALANCE = 1000000;

    String accountNum;
    String accountOwner;
    int balance, withdraw, deposit;

    Account(String accountNum, String accountOwner, int deposit) {
        this.accountNum = accountNum;
        this.accountOwner = accountOwner;
        this.balance = deposit;
        System.out.println("결과: 결과가 생성되었습니다.");
    }

    String getAccountNum() {
        return this.accountNum;
    }

    String getAccountOwner() {
        return this.accountOwner;
    }

    int getBalance() {
        return this.balance;
    }


    void deposit(int deposit) {
        if (deposit < MIN_BALANCE || deposit > MAX_BALANCE) {
            System.out.println("cannot deposit to the account.");
        } else {
            this.balance += deposit;
            System.out.println("deposit completed successfully.");
        }
    }

    void withdrawal(int withdraw) {
        if (balance - withdraw < MIN_BALANCE || withdraw > MAX_BALANCE) {
            System.out.println("failed to withdraw from the account.");
        } else {
            this.balance -= withdraw;
            System.out.println("withdrawal completed successfully.");
        }
    }

}

BankApplicatioin.class

package ThisisJAVA.ch06.bank;

import java.util.Scanner;

public class BankApplicatioin {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        boolean run = true;
        Account[] accounts = new Account[5];
        int accCount = 0;

        while (run) {
            System.out.print(""" 
                    
                    -----------------------------------------------------------------------------------------
                    1. 계좌생성 || 2. 계좌목록 || 3. 예금 || 4. 출금 || 5. 종료
                    -----------------------------------------------------------------------------------------
                    선택> """);

            int select = sc.nextInt();

            if (select == 1) {
                System.out.print("""
                        ---------
                         계좌생성
                        ---------
                        계좌번호:  """);
                String accountNo = sc.next();
                System.out.print("계좌주: ");
                String accountOwner = sc.next();

                boolean exist = false;
                for (int i = 0; i < accCount; i++) {
                    if (accounts[i].getAccountNum().equals(accountNo)) {
                        exist = true;
                        break;
                    }
                }
                if (exist) {
                    System.out.println("이미 존재하는 계좌번호입니다.");
                    continue;
                }
                System.out.print("초기입금액: ");
                int deposit = sc.nextInt();
                accounts[accCount++] = new Account(accountNo, accountOwner, deposit);

            } else if (select == 2) {
                System.out.print("""
                        ---------
                         계좌목록
                        ---------
                        계좌번호:
                        """);

                for (int i = 0; i < accCount; i++) {
                    System.out.println(accounts[i].getAccountNum() + " " + accounts[i].getAccountOwner() + " " + accounts[i].getBalance());
                }

            } else if (select == 3) {
                System.out.print("""
                        ---------
                         예금
                        ---------
                        계좌번호: """);
                String accountNo = sc.next();
                System.out.print("예금액: ");
                int deposit = sc.nextInt();
                for (int i = 0; i < accCount; i++) {
                    if (accounts[i].getAccountNum().equals(accountNo)) {
                        accounts[i].deposit(deposit);
                    }
                }

            } else if (select == 4) {
                System.out.print("""
                        ---------
                         출금
                        ---------
                        계좌번호: """);
                String accountNo = sc.next();
                System.out.print("출금액: ");
                int withdraw = sc.nextInt();
                for (int i = 0; i < accCount; i++) {
                    if (accounts[i].getAccountNum().equals(accountNo)) {
                        accounts[i].withdrawal(withdraw);
                    }
                }
            } else {
                System.out.println("exit the program.");
                run = false;
            }
        }
    }
}

배열로 하면 크기 변경이 불가능해서 리스트를 이용하는 것이 더 효율적이다. 

List<Account> accounts = new ArrayList<>();

// 계좌 생성
accounts.add(new Account(accountNo, accountOwner, deposit));
Programming [JAVA] 이것이 자바다(3판) 6장 20번 답안