الإيداعات والسحوبات
جزء من قسم Object Oriented Programming في رحلة JavaScript على Coddy — الدرس 22 من 56.
التحدي
بعد ذلك، سنضيف وظائف المعاملات إلى فئة 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
1الكائنات والكلمة المفتاحية this
مراجعة سريعة: الكائناتإضافة Methods إلى الكائناتفهم الكلمة المفتاحية thisConstructor Functionsالكلمة المفتاحية newتحدي المراجعة7الوراثة والكلمة المفتاحية extends
الوراثةعلاقة "is-a"الكلمة المفتاحية extendsالدالة super()وراثة الخصائص والوظائفتحدي المراجعة2تنظيم الكود
ما هي الـ Modules؟التصدير باستخدام exportالاستيراد باستخدام importالـ Default مقابل الـ Named Exports8تنظيم كود OOP
تنظيم الـ Classes في Modules11مشروع: مصيّر الأشكال
إعداد: فئة Shape والتصديروراثة فئة Circle