Menu
Coddy logo textTech

입금과 출금

Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 22번째.

challenge icon

챌린지

다음으로, deposit()withdraw() 메서드를 구현하여 BankAccount 클래스에 거래 기능을 추가합니다.

여러분의 과제는 BankAccount 클래스에 다음 두 가지 새로운 메서드를 추가하는 것입니다:

1. <strong>deposit(amount)</strong> 메서드:

  • amount가 양수(0보다 큰지)인지 확인합니다.
  • 유효한 경우:
    • 잔액(balance)에 금액을 더합니다.
    • 정확히 다음을 출력합니다: Deposited ${amount}
  • 유효하지 않은 경우: 정확히 다음을 출력합니다: Invalid deposit amount

2. <strong>withdraw(amount)</strong> 메서드:

  • 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




객체 지향 프로그래밍의 모든 레슨