Java/이것이자바다

6장 확인문제 19번 ** Getter, Setter 기본 문제

유호야 2020. 10. 15. 12:07
반응형

 

package Chapter6;

public class Account {
	
	private final static int MIN_BALANCE = 0;
	private final static int MAX_BALANCE = 1000000;
	
	private int balance;
	
	//balance를 받을 getter 생성
	
	public int getBalance() {
		return balance;
	}
	
	public void setBalance(int balance) {
		if(balance > MAX_BALANCE || balance < MIN_BALANCE) {
			return;
		} else {
			this.balance = balance;
		}
	
	
	}
}

 

package Chapter6;

public class AccountExample {

	public static void main(String[] args) {
		
		
		Account account = new Account();
		
		account.setBalance(9000);
		System.out.println("현재잔액 : " + account.getBalance());
		account.setBalance(-9000);
		System.out.println("현재잔액 : " + account.getBalance());

	}

}
반응형