본문 바로가기

Java/Learn_Java

6. 클래스_연습문제(은행 계좌)

- 키보드로부터 계좌 정보를 입력받아서, 계좌를 관리하는 프로그램입니다.

실행 결과를 보고, 알맞게 BankApplication 클래스의 Method를 작성해보세요.

//Account.java

public class Account {
	private String ano;
	private String owner;
	private int balance;

	public Account(String ano, String owner, int balance) {
		this.ano = ano;
		this.owner = owner;
		this.balance = 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;
	}

}// class

Main

package account;

import java.util.Scanner;

public class BankApplication {

	private static Account[] accountArray = new Account[100];
	private static Scanner sc = new Scanner(System.in);

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

			int selNo = sc.nextInt();

			if (selNo == 1) {
				createAccount();
			} else if (selNo == 2) {
				accountList();
			} else if (selNo == 3) {
				deposit();
			} else if (selNo == 4) {
				withdraw();
			} else if (selNo == 5) {
				run = false;
			}

		} // while

		System.out.println("프로그램 종료");
	}// main

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

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

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

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

		Account newAccount = new Account(ano, owner, balance);
		for (int i = 0; i < accountArray.length; i++) {
			if (accountArray[i] == null) {
				accountArray[i] = newAccount;
				System.out.println("결과: 계좌가 생성되었습니다.");
				break;
			}
		} // for

	}// createAccount()

	private static void accountList() {
		System.out.println("----------");
		System.out.println("계좌목록");
		System.out.println("----------");

		for (int i = 0; i < accountArray.length; i++) {
			Account account = accountArray[i];
			if (account != null) {
				System.out.print(account.getAno());
				System.out.print("         ");
				System.out.print(account.getOwner());
				System.out.print("         ");
				System.out.print(account.getBalance());
				System.out.println();
			}
		}
	}// accountlist

	private static void deposit() {
		System.out.println("----------");
		System.out.println("입금");
		System.out.println("----------");
		System.out.print("계좌번호: ");
		String ano = sc.next();

		Account account = findAccount(ano);
		if (account == null) {
			System.out.println("계좌가 없습니다.");
			return;
		}

		System.out.println("예금액: ");
		int money = sc.nextInt();

		account.setBalance(account.getBalance() + money);
		System.out.println("예금 성공");
		System.out.println("현재 금액: " + account.getBalance());

	

main계속

}// deposit

	private static void withdraw() {
		System.out.println("----------");
		System.out.println("입금");
		System.out.println("----------");
		System.out.print("계좌번호: ");
		String ano = sc.next();

		Account account = findAccount(ano);
		if (account == null) {
			System.out.println("계좌가 없습니다.");
			return;
		}

		System.out.println("출금액: ");
		int money = sc.nextInt();

		if (money > account.getBalance()) {
			System.out.println("잔액이 부족합니다.");
		} else {
			account.setBalance(account.getBalance() - money);
			System.out.println("출금 성공");
			System.out.println("현재 금액: " + account.getBalance());
		}
	}// withdraw

//	Account 배열에서 ano와 동일한 Account 객체 찾기
	private static Account findAccount(String ano) {
		Account account = null;
		for (int i = 0; i < accountArray.length; i++) {
			if (accountArray[i] != null) {
				String dbAno = accountArray[i].getAno();
				if (dbAno.equals(ano)) {
					account = accountArray[i];
					break;
				}
			}
		} // for
		return account;

	}// findAccount

}// class