입금과 출금
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 56개 중 22번째.
챌린지
다음으로 deposit() 및 withdraw() 메서드를 구현하여 BankAccount 클래스에 거래 기능을 추가합니다.
BankAccount 클래스에 다음 두 메서드를 새로 추가하세요.
1. deposit(amount) 메서드:
amount가 양수(0보다 큰 값)인지 확인합니다.- 유효한 경우:
- 잔액에 금액을 더합니다.
- 정확히 다음을 로그로 출력합니다:
Deposited $${amount}
- 유효하지 않은 경우: 정확히 다음을 로그로 출력합니다:
Invalid deposit amount
2. withdraw(amount) 메서드:
amount가 양수이면서 현재 잔액 이하인지 확인합니다.- 유효한 경우:
- 잔액에서 금액을 뺍니다.
- 정확히 다음을 로그로 출력합니다:
Withdrew $${amount}
- 유효하지 않은 경우: 정확히 다음을 로그로 출력합니다:
Invalid withdrawal amount or insufficient funds
직접 해보기
import { BankAccount } from './bankAccount.js';
// 구현을 테스트합니다
const testAccount = new BankAccount('Alex Johnson', 500);
console.log('Initial state:');
console.log(testAccount.getAccountInfo()); // "Alex Johnson: $500"
testAccount.deposit(200); // 로그 출력: "Deposited $200"
console.log('New balance:', testAccount.getBalance()); // 700
testAccount.deposit(-50); // 로그 출력: "Invalid deposit amount"
console.log('Balance unchanged:', testAccount.getBalance()); // 600
testAccount.withdraw(100); // 로그 출력: "Withdrew $100"
console.log('New balance:', testAccount.getBalance()); // 600
testAccount.withdraw(800); // 로그 출력: "Invalid withdrawal amount or insufficient funds"
console.log('Balance unchanged:', testAccount.getBalance()); // 600
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 JavaScript 컴파일러