-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
42 lines (32 loc) · 1.37 KB
/
Copy pathAccount.java
File metadata and controls
42 lines (32 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package week02.advanced;
public class Account {
private String owner;
private int balance;
Account(String owner, int balance){
this.owner = owner;
this.balance = balance;
}
boolean transfer(Account target, int amount){
if (balance >= amount) {
this.balance -= amount; // 잔액 -이체금액
target.balance += amount; // target 잔액 + 이체금액
return true;
}
return false;
}
public String getOwner() {
return owner;
}
public int getBalance() {
return balance;
}
// 27단계 (심화): 은행 계좌 이체 트랜잭션
// 문제: Account.java (계좌주 owner, 잔액 balance)에, 다른 Account 객체와 이체할 금액을
// 매개변수로 받아 계좌 이체를 수행하는 transfer(Account target, int amount)메서드를
// 구현하세요. 이체에 성공하면 true, 잔액 부족으로 실패하면 false를 리턴하도록 만드세요.
// Bank.java에서 두 계좌를 만들고,이체 메서드를 호출한 뒤 리턴값에 따라 "이체 성공" 또는 "이체 실패"
// 를 출력하세요.
// 핵심 사고: 두 객체가 상호작용하며 서로의 상태를 변경하고, 작업의 성공/실패 여부를 리턴값으로 알려주는
// 트랜잭션 개념을 구현합니다.
// 힌트: 객체 간 상호작용, 메서드 리턴값(boolean), if문, private, getter, setter
}