본문 바로가기

Java/과제

chapter 08) 인터페이스example - BankAccount 클래스 변경

상속 과제에서 만들었던 BankAccount 클래스를 추상클래스로 변경하여 보자.

package practice3;

public abstract class BankAccount {

	public abstract String getAccountType();

	protected int balance;

	public BankAccount(int balance) {
		this.balance = balance;
	}

	public int getBalance() {
		return balance;
	}

	public void deposit(int amount) {
		balance += amount;
	}

	public boolean withdraw(int amount) {
		if (balance >= amount) {
			balance -= amount;
			return true;
		}
		return false;
	}

	public boolean transfer(int amount, BankAccount otherAccount) {
		if (withdraw(amount)) {
			otherAccount.deposit(amount);
			return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return String.format("%,d", balance);
	}
}
package practice3;

	public class Bank {
		/**
		 * 은행의 고객 목록
		 */
		private Customer[] customers;
		/**
		 * 은행의 고객 수
		 */
		private int numberOfCustomers;
		// 필드의 값을 초기화 할때 생성자를 많이 사용
		public Bank() {
			customers = new Customer[10];
		}
		public void addCustomer(Customer customer) {
			customers[numberOfCustomers++] = customer;
		}
		public Customer[] getCustomers() {
			return customers;
		}
		public int getNumberOfCustomers() {
			return numberOfCustomers;
		}
		/**
		 * 배열의 index번째 고객(Customer) 객체를 반환
		 * @param index 고객 인덱스 번호
		 * @return index번째 고객 객체를 반환
		 */
		public Customer getCustomer(int index) {
			return customers[index];
		}
		
	}
package practice3;

public class CheckingAccount extends BankAccount {
	private SavingsAccount protectedBy;
	
	public CheckingAccount(int balance) {
		super(balance);
	}
	
	public CheckingAccount(int balance, SavingsAccount protectedBy) {
		super(balance);
		this.protectedBy = protectedBy;
	}
	
	@Override
	public boolean withdraw(int amount) {
		// 만약 SavingsAccount에 10000원이 있고,
		// CheckingAccount에 5000원이 있을 경우
		// 백지수표로 7500원짜리 물건을 구매했을 경우
		if (amount > balance) {
			protectedBy.balance -= amount - balance;
			balance = 0;
			return true;
		}
		return super.withdraw(amount);
	}
	public String getAccountType() {
		return "당좌예금";
	}
}
package practice3;

public class SavingsAccount extends BankAccount {
	private double interestRate;

	public SavingsAccount(int balance, double interestRate) {
		super(balance);
		this.interestRate = interestRate;
	}
	
	public void updateBalance(int period) {
		balance += balance * interestRate * period;
	}
	public String getAccountType() {
		return "저축예금";
		
	}
}
package practice3;

public class Customer {
	/**
	 * 고객 이름
	 */
	private String firstName;
	/**
	 * 고객 성
	 */
	private String lastName;
	/**
	 * 고객이 소유한 계좌
	 */
	private BankAccount[] accounts;
	private int numberOfAccounts;
	
	public Customer(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
		accounts = new BankAccount[5];
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void addAccount(BankAccount account) {
		accounts[numberOfAccounts++] = account;
	}
	public BankAccount getAccount(int index) {
		return accounts[index];
	}
	public int getNumberOfAccounts() {
		return numberOfAccounts;
	}
	@Override
	public String toString() {
		return String.format("이름: %s %s, 계좌의 갯수: %,d", firstName, lastName, numberOfAccounts);
	}
	
}
package practice3;

public class BankTest {
	public static void main(String[] args) {
		Bank bank = new Bank();

		Customer customer1 = new Customer("Tony", "Stark");
		SavingsAccount customer1savingsAccount = new SavingsAccount(2000,0.01);
		customer1.addAccount(customer1savingsAccount);
		customer1.addAccount(new CheckingAccount(3000, customer1savingsAccount));
		bank.addCustomer(customer1);
		
		Customer customer2 = new Customer("Peter", "Parker");
		customer2.addAccount(new CheckingAccount(2000));
		bank.addCustomer(customer2);

		Customer customer3 = new Customer("Steve", "Rogers");
		SavingsAccount customer3savingsAccount = new SavingsAccount(2000,0.03);
		customer3.addAccount(customer3savingsAccount);
		customer3.addAccount(new CheckingAccount(1000,customer3savingsAccount));
		bank.addCustomer(customer3);
		
		System.out.println();
		printCustomers(bank.getCustomers());
	}
	private static void printCustomers(Customer[] customers) {
		for(Customer c : customers) {
			if (c != null) {
				System.out.println("이름: " + c + "," + "계좌의 수: " + c.getNumberOfAccounts());
				for(int i = 0; i<c.getNumberOfAccounts(); i++) {
					System.out.println("계좌 종류: " + c.getAccount(i).getAccountType()+", 잔고: "+ c.getAccount(i) +"원");
				}
				System.out.println();
			}
		}
	}
}

Recent Posts
Popular Posts
Tags
더보기
Recent Comments