/*
* [실습]
* Account
* 1)멤버 변수: 계좌번호(account_num), 예금주(name), 잔고(balance)
* 2)멤버 메서드: 1. 예금하기(deposit) : balance에 값 누적
* 예금 처리 후 "입금이 완료되었습니다." 문구 출력
* 2. 출금하기(withdraw) : balance에 값 차감
* 출금 처리 후 "출금이 완료되었습니다." 문구 출력
* 3. 계좌정보(printAccount) : 계좌번호, 예금주, 잔고 출력
*
* BankMain
* 1) Account 생성
* "계좌 기본 정보를 입력해서 계좌를 생성합니다." 문구 출력 후
* 계좌번호, 예금주, 잔고를 입력
* "홍길동님 계좌가 개설되었습니다." 문구 출력
* 2)while문을 만들고 메뉴 지정
* 1. 예금, 2. 출금 3. 잔고 확인, 4. 종료
*/
package kr.s11.object.method;
class Account{
String account_num;
String name;
int balance;
public void deposit(int m) {
if(m <= 0) {
System.out.println("입금액은 0보다 크게 입력하세요");
}else {
balance += m;
System.out.println("입금이 완료되었습니다.");
}
}
public void withdraw(int m) {
if(m <= 0) {
System.out.println("출금액은 0보다 크게 입력하세요");
}else if(balance < m){
System.out.println("잔액이 부족합니다.");
}else {
balance -= m;
System.out.println("출금이 완료되었습니다.");
}
}
public void printAccount() {
System.out.println("계좌번호: " + account_num);
System.out.println("예금주: " + name);
System.out.printf("잔고: %,d원%n", balance);
}
}
public class BankMain {
public static void main(String[] args) {
Account myAccount = new Account();
System.out.println("계좌 기본 정보를 입력해서 계좌를 생성합니다.");
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("계좌번호: ");
myAccount.account_num = input.nextLine();
System.out.print("예금주: ");
myAccount.name = input.nextLine();
System.out.print("잔고: ");
myAccount.balance = input.nextInt();
System.out.println(myAccount.name + "님 계좌가 개설되었습니다.");
while(true) {
System.out.println("=====================================");
System.out.println("1. 예금 | 2. 출금 | 3. 잔고 확인 | 4. 종료");
System.out.println("=====================================");
System.out.print("메뉴 선택> ");
int num = input.nextInt();
if(num == 1) {
System.out.print("예금액> ");
myAccount.deposit(input.nextInt());
}else if(num == 2) {
System.out.print("출금액> ");
myAccount.withdraw(input.nextInt());
}else if(num == 3) {
myAccount.printAccount();
}else if(num == 4) {
System.out.println("프로그램 종료");
break;
}else {
System.out.println("잘못 입력했습니다.");
}
}
input.close();
}
}
위 예제에서는 클래스를 나누어 은행 프로그램을 만들었으며 각 역할은 다음과 같다.
Account - 변수와 메서드를 저장해 객체로 활동하는 측면 = 하나의 계좌와 같음
BankMain - 메인이 있어서 객체 만들어 수행하는 측면
인자로 금액(int m)을 입력 받고
값이 왜곡되는 것을 막기 위해 if문으로 조건 체크를 진행,
입금액이 음수이거나 0일 경우 if(m <= 0) {System.out.println("입금액은 0보다 크게 입력하세요");}
입금액이 양수로 정상 입력되었다면, else로 처리 → 잔고(balance)에 금액 누적, 입금 완료를 안내한다.
→ {balance += m; System.out.println("입금이 완료되었습니다.");}
마찬가지로 인자로 금액(int m)을 받고
값이 왜곡되는 것을 막기 위해 if문으로 조건 체크를 진행,
출금액이 음수이거나 0일 경우 if(m <= 0) {System.out.println("출금액은 0보다 크게 입력하세요");}
else if로 잔액이 부족하여 입금할 수 없는 경우를 추가한다. 현재 보유한 금액이 누적되어 있는 잔고(balance)가 출력하고자 입력된 금액(m)보다 작은 경우를 말한다.
→ else if(balance < m){System.out.println("잔액이 부족합니다.");}
출금액이 양수로 정상 입력되었다면, else로 처리 → 잔고(balance)에 금액 차감, 출금 완료를 안내한다.
→{balance -= m;System.out.println("출금이 완료되었습니다.");}
System.out.println("계좌번호: " + account_num);
System.out.println("예금주: " + name);
System.out.printf("잔고: %,d원%n", balance);
Account 클래스의 멤버 변수와 멤버 메서드를 활용하기 위해 Account myAccount = new Account(); Account 객체를 생성한다.
Scanner를 통해 Account 객체의 멤버 변수에 데이터를 입력 받는다.
ex. myAccount.account_num = input.nextLine();
무한루프 형태인 while(true) { while문 안에 반복되어 출력될 메뉴를 작성하고, Scanner를 통해 번호를 입력 받는다.
System.out.println("=====================================");
System.out.println("1. 예금 | 2. 출금 | 3. 잔고 확인 | 4. 종료");
System.out.println("=====================================");
System.out.print("메뉴 선택> ");
int num = input.nextInt();
while문의 경우 조건이 true일 때만 수행문을 수행하는 구조이므로 if~else if를 통해 입력받는 값에 따라 경우가 나뉠 수 있도록 while 안에 입력 받는 번호에 따라 다르게 작성된 수행문이 담긴 if문을 넣어준다.
if(num == 1) { 입력된 번호가 1일 경우 Account 객체의 예금(deposit) 메서드를,
else if(num == 2) { 입력된 번호가 2일 경우 Account 객체의 출금(withdraw) 메서드를 호출하며 이때 금액이 될 인자는 Scanner를 통해 입력 받는다.
ex. myAccount.deposit(input.nextInt());, myAccount.withdraw(input.nextInt());
else if(num == 3) { 입력된 번호가 3일 경우 Account 객체의 계좌정보 조회(printAccount) 메서드를 호출한다.
ex. myAccount.printAccount();
else if(num == 4) { 입력된 번호가 4일 경우 '종료'이므로 무한루프 형태의 while 반복문을 빠져나올 수 있도록 break;를 명시한다.
마지막으로 입력된 번호가 메뉴에 명시된 1~4가 아닐 경우 System.out.println("잘못 입력했습니다.");를 출력한다.
작업이 모두 끝나면 input.close(); 한다.