## 과제_1 : 은행 계좌 관리 시스템의 예외 처리 개선
목표 : OOP 특징과 예외 처리 기법을 활용하여, 기존의 은행 계좌 관리 시스템의 예외 처리를 개선한다.
요구사항 :
1. **예외 처리 클래스 설계**:
- 은행 계좌 관리 시스템에 대한 예외 처리를 위한 사용자 정의 예외 클래스를 설계한다.
- 다음과 같은 상황에서 발생할 수 있는 예외들에 대해 각각 별도의 예외 클래스를 정의한다:
- 음수 금액 입금 시도(`NegativeDepositException`)
- 음수 금액 출금 시도(`NegativeWithdrawException`)
- 음수 금액 대출 시도(`NegativeLoanException`)
- 대출 금액이 최대 대출 가능 금액을 초과하는 경우(`ExceedCreditLimitException`)
- 상환 금액이 현재 대출 잔액을 초과하는 경우(`ExceedLoanBalanceException`)
- 각 예외 클래스는 적절한 에러 메시지를 포함해야 한다.
2. **예외 처리 로직 구현**:
- `SavingsAccount`와 `CheckingAccount` 클래스 내의 메서드들에서, 위에서 정의된 예외들을 적절하게 던지도록 한다.
- 예외 상황이 발생할 경우, 예외를 던지고 해당 예외를 적절히 처리하는 로직을 구현한다.
- 예외 처리는 `try-catch` 블록을 사용하여 구현하며, 예외 발생 시 사용자에게 적절한 피드백을 제공해야 한다.
## 과제_2 : 프로그래머스 문제 풀기
1. [숨어있는 숫자의 덧셈 (1)](https://school.programmers.co.kr/learn/courses/30/lessons/120851)
2. [숨어있는 숫자의 덧셈 (2)](https://school.programmers.co.kr/learn/courses/30/lessons/120864)
3. [연속된 수의 합](https://school.programmers.co.kr/learn/courses/30/lessons/120923)
---
## 과제_1 : 은행 계좌 관리 시스템의 예외 처리 개선 모범 답안
```java
// 사용자 정의 예외 클래스
class NegativeDepositException extends Exception {
public NegativeDepositException(String message) {
super(message);
}
}
class NegativeWithdrawException extends Exception {
public NegativeWithdrawException(String message) {
super(message);
}
}
class NegativeLoanException extends Exception {
public NegativeLoanException(String message) {
super(message);
}
}
class ExceedCreditLimitException extends Exception {
public ExceedCreditLimitException(String message) {
super(message);
}
}
class ExceedLoanBalanceException extends Exception {
public ExceedLoanBalanceException(String message) {
super(message);
}
}
// AbstractAccount 클래스 변경사항
public abstract class AbstractAccount implements Account {
// 기존 필드 및 메서드 생략...
@Override
public void deposit(double amount) throws NegativeDepositException {
if (amount < 0) {
throw new NegativeDepositException("입금 금액은 음수가 될 수 없습니다.");
}
balance += amount;
}
@Override
public void withdraw(double amount) throws NegativeWithdrawException {
if (amount < 0) {
throw new NegativeWithdrawException("출금 금액은 음수가 될 수 없습니다.");
}
balance -= amount;
}
}
// SavingsAccount 클래스 변경사항
public class SavingsAccount extends AbstractAccount {
// 기존 필드 및 메서드 생략...
// 이자 추가 메서드는 예외 처리가 필요하지 않으므로 변경사항 없음
}
// CheckingAccount 클래스 변경사항
public class CheckingAccount extends AbstractAccount {
// 기존 필드 및 메서드 생략...
@Override
public void loan(double amount) throws NegativeLoanException, ExceedCreditLimitException {
if (amount < 0) {
throw new NegativeLoanException("대출 금액은 음수가 될 수 없습니다.");
}
if (amount > creditLimit) {
throw new ExceedCreditLimitException("대출 금액이 한도를 초과합니다.");
}
loanBalance += amount;
deposit(amount); // 예외 전파
}
@Override
public void repayLoan(double amount) throws NegativeWithdrawException, ExceedLoanBalanceException {
if (amount < 0) {
throw new NegativeWithdrawException("상환 금액은 음수가 될 수 없습니다.");
}
if (amount > loanBalance) {
throw new ExceedLoanBalanceException("상환 금액이 대출 잔액을 초과합니다.");
}
loanBalance -= amount;
withdraw(amount); // 예외 전파
}
}
// Main 클래스 변경사항
public class Main {
public static void main(String[] args) {
try {
SavingsAccount sa = new SavingsAccount("123-45-67890", "홍길동", 100000, 0.05);
sa.deposit(-100); // NegativeDepositException 시뮬레이션
} catch (NegativeDepositException e) {
System.err.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
// 이하 다른 메서드 호출 및 예외 처리 로직 생략...
}
}
```
---
## 과제_2 : 프로그래머스 문제 풀기 모범 답안
1. 숨어있는 숫자의 덧셈 (1)
```java
class Solution {
public int solution(String my_string) {
int answer = 0;
// 문자열을 순회하면서 각 문자가 숫자인지 확인
for (int i = 0; i < my_string.length(); i++) {
char c = my_string.charAt(i);
// 문자가 숫자인 경우에만 answer에 더함
if ('0' <= c && c <= '9') {
answer += c - '0'; // 문자를 정수로 변환하여 더함
}
}
return answer;
}
}
```
2. 숨어있는 숫자의 덧셈 (2)
```java
class Solution {
public int solution(String my_string) {
int answer = 0;
// 숫자를 저장할 임시 문자열 변수
String temp = "0";
// 문자열을 순회하면서 각 문자 처리
for (int i = 0; i < my_string.length(); i++) {
char c = my_string.charAt(i);
// 문자가 숫자인 경우 temp에 추가
if ('0' <= c && c <= '9') {
temp += c;
} else {
// 숫자가 아닌 경우 기존에 누적된 숫자를 answer에 더하고 temp를 초기화
answer += Integer.parseInt(temp);
temp = "0";
}
}
// 마지막에 누적된 숫자 처리
answer += Integer.parseInt(temp);
return answer;
}
}
```
3. 연속된 수의 합
```java
class Solution {
public int[] solution(int num, int total) {
int middle = total / num;
int start = middle - num / 2;
if (num % 2 == 0) {
start += 1;
}
int[] result = new int[num];
for (int i = 0; i < num; i++) {
result[i] = start + i;
}
return result;
}
}
```