Menu
CoddyTech

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

جزء من قسم البرمجة كائنية التوجه في رحلة JavaScript على Coddy. الدرس 22 من 56.

challenge icon

التحدي

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

مهمتك هي إضافة هاتين الطريقتين الجديدتين إلى الصنف 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 عبر الإنترنت