Chapter 06 - 클래스 확인문제

김태원·2023년 1월 12일
0

Chapter 05 - 참조 타입 확인문제

정답: 3

하나의 클래스로 여러 객체를 생성할 수 있다.

정답: 4

로컬 변수는 클래스의 구성 멤버가 아니다.

정답: 4

클래스는 필드와 메소드 없이도 선언이 가능하다.

정답: 3

필드 선언은 클래스 중괄호 {} 블록 어디서든 존재할 수 있다. 생성자 선언과 메소드 선언의 앞과 뒤 어떤 곳에서도 필드 선언이 가능하다.

정답: 1

객체를 생성하려면 생성자 호출이 반드시 필요하다.

정답: 4

메소드 오버로딩을 통해 중복 선언이 가능하다.

정답: 2

리턴 타입이 반드시 다를 필요는 없다.

정답: 2

정적 블록에서는 초기화될 수 없다.

정답: 2

final 필드는 가능하지만 상수(static final)는 생성자에서 초기화될 수 없다.

정답: 4

정답: 3

default 접근 제한은 같은 패키지 내부에서만 사용을 허가한다.

정답: 필드, 생성자, 메소드

정답:

public class Member {
    String name;
    String id;
    String password;
    int age;
}

정답:

Member(String name, String id) {
    this.name = name;
    this.id = id;
}

정답:

public class MemberService {
    boolean login(String id, String password) {
        return id.equals("hong") && password.equals("12345");
    }

    void logout(String id) {
        System.out.println("로그아웃 되었습니다.");
    }
}

정답:

public class Printer {
    public void println(int value) {
        System.out.println(value);
    }

    public void println(boolean value) {
        System.out.println(value);
    }

    public void println(double value) {
        System.out.println(value);
    }

    public void println(String value) {
        System.out.println(value);
    }
}

정답:

public class Printer {
    public static void println(int value) {
        System.out.println(value);
    }

    public static void println(boolean value) {
        System.out.println(value);
    }

    public static void println(double value) {
        System.out.println(value);
    }

    public static void println(String value) {
        System.out.println(value);
    }
}

정답:

public class ShopService {
    private static ShopService singleton = new ShopService();

    private ShopService() {
    }

    static ShopService getInstance() {
        return singleton;
    }
}

정답:

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

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        if (Account.MIN_BALANCE <= balance && balance <= Account.MAX_BALANCE) this.balance = balance;
    }
}

정답:

Account.java
public class Account {
    private String id;
    private String name;
    private int balance;

    public Account(String id, String owner, int balance) {
        this.id = id;
        this.name = owner;
        this.balance = balance;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}
BankApplication.java
import java.util.Scanner;

public class BankApplication {
    private static final Account[] accounts = new Account[100];
    private static final Scanner sc = new Scanner(System.in);
    private static int lastIndexOfAccounts = 0;

    public static void main(String[] args) {
        int menu = 0;
        while (menu != 5) {
            System.out.println("----------------------------------------------------------");
            System.out.println("1.계좌생성 | 2.계좌목록 | 3.예금 | 4.출금 | 5.종료");
            System.out.println("----------------------------------------------------------");
            System.out.print("선택> ");
            menu = sc.nextInt();

            switch (menu) {
                case 1 -> createAccount();
                case 2 -> getAccounts();
                case 3 -> depositBalance();
                case 4 -> withdrawBalance();
                case 5 -> {}
            }
        }
        System.out.println("프로그램 종료");
    }

    private static void createAccount() {
        System.out.println("--------------");
        System.out.println("계좌생성");
        System.out.println("--------------");

        System.out.print("계좌번호: ");
        String id = sc.next();

        System.out.print("계좌주: ");
        String name = sc.next();

        System.out.print("초기입금액: ");
        int balance = sc.nextInt();

        Account account = new Account(id, name, balance);
        accounts[++lastIndexOfAccounts] = account;

        System.out.println("결과: 계좌가 생성되었습니다.");
    }

    private static Account getAccount(String id) {
        for (Account account : accounts) {
            if (account != null && account.getId().equals(id)) return account;
        }
        System.out.println("존재하지 않는 계좌번호입니다.");
        return null;
    }

    private static void getAccounts() {
        System.out.println("--------------");
        System.out.println("계좌목록");
        System.out.println("--------------");
        for (Account account : accounts) {
            if (account != null) {
                System.out.printf("%s\t%s\t%d\n", account.getId(), account.getName(), account.getBalance());
            }
        }
    }

    private static void depositBalance() {
        System.out.println("--------------");
        System.out.println("예금");
        System.out.println("--------------");

        System.out.print("계좌번호: ");
        String id = sc.next();

        System.out.print("예금액: ");
        int price = sc.nextInt();

        Account account = getAccount(id);
        if (account == null) return;

        account.setBalance(account.getBalance() + price);
        System.out.println("결과: 예금이 성공되었습니다.");
    }

    private static void withdrawBalance() {
        System.out.println("--------------");
        System.out.println("출금");
        System.out.println("--------------");

        System.out.print("계좌번호: ");
        String id = sc.next();

        System.out.print("출금액: ");
        int price = sc.nextInt();

        Account account = getAccount(id);
        if (account == null) return;

        account.setBalance(account.getBalance() - price);
        System.out.println("결과: 출금이 성공되었습니다.");
    }
}
profile
개발이 재밌어서 하는 Junior Backend Developer

0개의 댓글