Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

الإيداعات والسحوبات

جزء من قسم Object Oriented Programming في رحلة JavaScript على Coddy — الدرس 22 من 56.

challenge icon

التحدي

بعد ذلك، سنضيف وظائف المعاملات إلى فئة BankAccount الخاصة بك عن طريق تنفيذ الأساليب deposit() و withdraw().

مهمتك هي إضافة هذين الأسلوبين الجديدين إلى فئة BankAccount:

1. الأسلوب <strong>deposit(amount)</strong>:

  • يتحقق مما إذا كان amount موجبًا (أكبر من 0)
  • إذا كان صالحًا:
    • يضيف المبلغ إلى الرصيد
    • يسجل (Logs) بالضبط: Deposited ${amount}
  • إذا كان غير صالح: يسجل (Logs) بالضبط: Invalid deposit amount

2. الأسلوب <strong>withdraw(amount)</strong>:

  • يتحقق مما إذا كان amount موجبًا وَ أقل من أو يساوي الرصيد الحالي
  • إذا كان صالحًا:
    • يطرح المبلغ من الرصيد
    • يسجل (Logs) بالضبط: Withdrew ${amount}
  • إذا كان غير صالح: يسجل (Logs) بالضبط: 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




جميع دروس Object Oriented Programming