UniCode
[이것이 자바다] BankApplication (클래스메소드+예외처리) 본문
<Class 파일>
Account : 계좌 라이브러리 클래스
public class Account {
//필드
private String ano;
private String owner;
private int balance;
//생성자
private Account(String ano, String owner, int balance) {
this.ano = ano;
this.owner = owner;
this.balance = balance;
}
//메소드 (getter/setter)
public static Account setAccount(String ano, String owner, int balance) {
return new Account(ano,owner,balance);
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
AccountException : 계좌 생성 시 발생되는 사용자 예외 처리 클래스
public class AccountException extends Exception{
public AccountException() {}
public AccountException(String msg) {
super(msg);
}
}
BankApplication : 메뉴 선택, 계좌 생성, 입금, 출금 등 수행 클래스
import java.util.*;
public class BankApplication {
// 정적 필드
private static List<Account> accountArr = new ArrayList<Account>();
private static Scanner sc = new Scanner(System.in);
private static BankApplication ba = new BankApplication();
// 메뉴 선택 예외 처리
public int selectnum(int num) throws AccountException{
if(num < 1 || num > 5) {
throw new AccountException("잘못된 메뉴 선택입니다.");
}
return num;
}
// 계좌 생성 시 계좌 번호 예외 처리
public void sameAnoException(Account ac, String ano) throws AccountException{
if(ac != null) {
if(ac.getAno().equals(ano))
throw new AccountException("같은 계좌가 존재합니다.");
}
}
// 예금 및 출금 시 금액 예외 처리
//op : t > 예금 // f> 출금
public void balanceException(Account ac, int money, boolean op) throws AccountException{
if(money < 0 ) {
throw new AccountException("잘못 입력된 금액입니다.");
}
if(ac.getBalance() < money && op==false) {
throw new AccountException("계좌 잔액이 부족합니다");
}
}
// 메뉴 출력 메소드
public void printMenu() {
System.out.println("-----------------------------------------------");
System.out.println("1.계좌생성 | 2. 계좌목록 | 3. 예금 | 4. 출금 | 5. 종료");
System.out.println("-----------------------------------------------");
System.out.print("선택 > ");
}
// 메뉴 선택 메소드
public boolean selectMenu(int num) {
switch(num) {
case 1:
createAccount();
return true;
case 2:
accountList();
return true;
case 3:
deposit();
return true;
case 4:
withdraw();
return true;
case 5:
stopApp();
return false;
default :
return true;
}
}
private void createAccount() {
System.out.println("--------");
System.out.println("계좌생성");
System.out.println("--------");
System.out.print("계좌번호 > ");
String ano = sc.next();
System.out.print("계좌주 > ");
String name = sc.next();
System.out.print("초기 입금액 >");
int money = sc.nextInt();
System.out.print("결과 : ");
try {
Account ac = findAccount(ano);
ba.sameAnoException(ac, ano);
accountArr.add(Account.setAccount(ano, name, money));
System.out.println("계좌 생성을 성공했습니다.");
}catch (AccountException e) {
System.out.println(e.getMessage());
System.out.println("계좌 생성을 실패했습니다.");
}
}
// 계좌 목록 출력 메소드
private void accountList() {
System.out.println("--------");
System.out.println("계좌목록");
System.out.println("--------");
for(int i = 0; i < accountArr.size(); i++) {
System.out.printf("%s \t %s \t %d\n",accountArr.get(i).getAno(),accountArr.get(i).getOwner(),accountArr.get(i).getBalance());
}
}
// 예금 메소드
private void deposit() {
System.out.println("--------");
System.out.println("예금");
System.out.println("--------");
System.out.print("계좌번호 > ");
String ano = sc.next();
try {
Account ac = findAccount(ano);
System.out.printf("반갑습니다 %s 고객님\n",ac.getOwner());
System.out.print("예금액 > ");
int money = sc.nextInt();
try {
ba.balanceException(ac,money,true);
money = ac.getBalance() - money;
ac.setBalance(money);
} catch (AccountException e) {
System.out.println(e.getMessage());
}
} catch (NullPointerException e) {
System.out.println("잘못된 계좌번호 입니다");
}
}
// 출금 메소드
private void withdraw() {
System.out.println("--------");
System.out.println("출금");
System.out.println("--------");
System.out.print("계좌번호 > ");
String ano = sc.next();
try {
Account ac = findAccount(ano);
System.out.printf("반갑습니다 %s 고객님\n",ac.getOwner());
System.out.print("출금액 > ");
int money = sc.nextInt();
try {
ba.balanceException(ac,money,false);
money = ac.getBalance() - money;
ac.setBalance(money);
} catch (AccountException e) {
System.out.println(e.getMessage());
System.out.println("금액을 다시 입력해주세요");
}
} catch (NullPointerException e1) {
System.out.println("잘못된 계좌번호 입니다");
}
}
// 프로그램 실행 종료 메소드
private void stopApp() {
System.out.println("프로그램을 종료합니다.");
}
//Account 배열에서 ano와 동일한 Account 객체 찾기
private static Account findAccount(String ano) {
for(int i=0; i<accountArr.size(); i++) {
if(accountArr.get(i).getAno().equals(ano)) {
return accountArr.get(i);
}
}
return null;
}
}
BankApplicationEx : main 메소드 클래스
import java.util.*;
public class BankApplicationEx {
// 정적 필드
private static Scanner sc = new Scanner(System.in);
//main메소드
public static void main(String[] args) {
BankApplication ba = new BankApplication();
int selectNo = 0;
boolean run = true;
while(run) {
ba.printMenu();
try {
selectNo = ba.selectnum(sc.nextInt());
run = ba.selectMenu(selectNo);
}catch(AccountException e) {
System.out.println(e.getMessage());
}catch(InputMismatchException e) {
System.out.println("잘못된 메뉴 선택입니다.");
}
}
}
}
<실행 화면>
'이것이 자바다' 카테고리의 다른 글
[이것이 자바다]CH13.제네릭(확인문제풀이) (0) | 2021.05.16 |
---|---|
[이것이 자바다]CH12.멀티 스레드(확인문제풀이) (0) | 2021.05.15 |
[이것이 자바다]CH10.예외처리(확인문제풀이) (0) | 2021.05.05 |
[이것이 자바다]CH09.중첩 클래스와 중첩 인터페이스(확인문제풀이) (0) | 2021.05.02 |
[이것이 자바다]CH08.인터페이스(확인문제풀이) (0) | 2021.05.02 |