java - 은행계좌 개설 예제

imjingu·2023년 8월 19일
0

개발공부

목록 보기
392/481
package chapter20230817.Banking;

import java.util.Scanner;

class Account {
    private int accID;      // 계좌번호
    private int balance;    // 잔    액
    private String cusName;   // 고객이름
    
    
    public Account(int accID, int balance, String cusName) {
    	this.accID = accID;
    	this.balance = balance;
    	this.cusName = cusName;
    }
    
    public boolean checkID(int id) {
    	return this.accID == id;
    }
    
    public boolean checkBalance(int money) {
    	return this.balance < money;
    }
    
    public void deposit(int money) {
    	this.balance += money;
    }
    
    public void withdraw(int money) {
    	this.balance -= money;
    }
    public void showAccInfo() {
    	System.out.println("계좌ID: " + this.accID);
        System.out.println("이  름: " + this.cusName);
        System.out.println("잔  액: " + this.balance);
    }
}

public class BankingSystem {

    static Scanner scanner = new Scanner(System.in);

    static Account[] accArr = new Account[100];   // Account 저장을 위한 배열, 여러명을 저장하기 위해
    static int accNum=0;        // 저장된 Account 수

    public static void main(String[] args) {
        int choice; // 메뉴번호
        while(true) {
            ShowMenu(); // 메뉴보여줌
            System.out.print("선택: ");
            choice = Integer.parseInt(scanner.nextLine());

            switch(choice) {
                case 1:
                    makeAccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    withdrawMoney();
                    break;
                case 4:
                    showAllAccInfo();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("Illegal selection..");
            }
        }
    }
    static void ShowMenu() {
        System.out.println(" -----Menu------");
        System.out.println("1. 계좌개설");
        System.out.println("2. 입    금");
        System.out.println("3. 출    금");
        System.out.println("4. 계좌정보 전체 출력");
        System.out.println("5. 프로그램 종료");
    }

    static void makeAccount() {
        /* 계좌개설 */
        int id;
        String name;
        int balance;

        // 계좌 개설 정보 입력 받음.
        System.out.println("[계좌개설]");
        System.out.print("계좌ID: ");
        id = Integer.parseInt(scanner.nextLine());
        System.out.print("이  름: ");
        name = scanner.nextLine();
        System.out.print("입금액: ");
        balance = Integer.parseInt(scanner.nextLine());
        System.out.println();

        accArr[accNum] = new Account(id, balance, name); // accArr해당되는 번 인덱스에 객체 생성
        accNum++; // 계속 0번을 받기때문에 ++을 해줌 해당하는 인덱스에 데이터를 장하고, 현재까지 입력된 데이터 수를 나타낼수있음
    }

    static void depositMoney() {
        /* 입 금 */
        int money;
        int id;

        // 입금 정보 입력 받음.
        System.out.println("[입    금]");
        System.out.print("계좌ID: ");
        id = Integer.parseInt(scanner.nextLine());
        System.out.print("입금액: ");
        money = Integer.parseInt(scanner.nextLine());

        // 계좌ID 찾고 입금 처리.
        for(int i = 0; i < accNum; i++) { // accNum - 
            if(accArr[i].checkID(id)) { // 계좌ID를 찾음.
            	accArr[i].deposit(money);
                System.out.println("입금완료");
                return;
            }
        }
        System.out.println("유효하지 않은 ID 입니다.");
    }

    static void withdrawMoney() {
        /* 출 금 */
        int money;
        int id;

        // 계좌 번호와 출금액 입력 받음
        System.out.println("[출    금]");
        System.out.print("계좌ID: ");
        id = Integer.parseInt(scanner.nextLine());
        System.out.println();
        System.out.print("출금액: ");
        money = Integer.parseInt(scanner.nextLine());

        // 계좌ID 찾고 출금 처리.
        for(int i = 0; i < accNum; i++) {
            if(accArr[i].checkID(id)) { // 계좌ID를 찾음.
                if(accArr[i].checkBalance(money)) {
                    System.out.println("잔액부족");
                    return;
                }

                accArr[i].withdraw(money);
                System.out.println("출금완료");
                return;
            }
        }
        System.out.println("유효하지 않은 ID 입니다.");
    }

    static void showAllAccInfo() {
        /* 계좌정보 전체 출력 */
        for(int i = 0; i < accNum; i++) {
        	accArr[i].showAccInfo();
            System.out.println();
        }
    }

}

0개의 댓글