Menu
CoddyTech
flag Ar iconالعربيةdown icon

مراجعة - مدير الحسابات البنكية

جزء من قسم البرمجة كائنية التوجّه في رحلة GDScript على Coddy. الدرس 14 من 65.

challenge icon

التحدي

متوسط

اكتب الصنف BankAccount في الملف bank_account.gd. فهو يجمع كل ما تعلمته في هذا الفصل.

  • متغير int ثابت interest_percent يبدأ بقيمة 2، وتشترك فيه جميع الحسابات
  • خاصيتان خاصتان: _owner_name (من النوع String) و_balance (من النوع int ويبدأ بقيمة 0)
  • خاصية للقراءة فقط owner_name، يعيد المُ getter فيها القيمة _owner_name
  • خاصية balance، يعيد المُ getter فيها القيمة _balance، بينما يطبع المُ setter القيمة Balance cannot be negative إذا كانت سالبة، وإلا يخزّن القيمة في _balance
  • المُنشئ _init(account_owner, start_balance) يخزّن اسم المالك ويُسنِد start_balance إلى balance، لكي يتحقق منها المُ setter

وهذه الطرق:

  • deposit(amount): تطبع Deposit amount must be positive وتُعيد false عندما تكون amount تساوي 0 أو أقل؛ وإلا تضيفها وتُعيد true
  • withdraw(amount): تطبع Withdrawal amount must be positive وتُعيد false عندما تكون amount تساوي 0 أو أقل، وتطبع Insufficient funds وتُعيد false عندما تكون أكبر من الرصيد؛ وإلا تطرحها وتُعيد true
  • apply_interest(): تضيف _balance * interest_percent / 100 (بالدولارات الكاملة) إلى الرصيد وتُعيد قيمة الفائدة
  • display_info(): تطبع ثلاثة أسطر: Account Owner: NAME وBalance: $BALANCE وInterest Rate: PERCENT%

يقرأ الكود المُقدَّم owner start_balance، وعدد الأوامر التالية، والأوامر deposit N وwithdraw N وset N (يُسنِد قيمة إلى balance) وinterest وrate N (يضبط BankAccount.interest_percent) وinfo. ويطبع سطرًا لكل عملية إيداع وسحب ودفع فائدة ناجحة، ثم يطبع رصيد المالك في النهاية. عند إدخال Coddy 1000 والأوامر deposit 500 وwithdraw 200 وinterest وinfo وset 5000 وwithdraw 10000، يطبع:

Deposited $500
Withdrew $200
Interest earned: $26
Account Owner: Coddy
Balance: $1326
Interest Rate: 2%
Insufficient funds
Coddy has $5000

اكتب الكود في bank_account.gd. يحتوي main.gd على كود الإدخال والإخراج المُقدَّم، ولا يمكن تعديله.

جرّب بنفسك

extends Node

func _ready():
	# كود الإدخال/الإخراج المقدَّم: اتركه كما هو
	var first := OS.read_string_from_stdin().split(" ")
	var account := BankAccount.new(first[0], int(first[1]))
	var count := int(OS.read_string_from_stdin())
	for i in count:
		var parts := OS.read_string_from_stdin().split(" ")
		match parts[0]:
			"deposit":
				if account.deposit(int(parts[1])):
					print("Deposited $%s" % parts[1])
			"withdraw":
				if account.withdraw(int(parts[1])):
					print("Withdrew $%s" % parts[1])
			"set":
				account.balance = int(parts[1])
			"interest":
				print("Interest earned: $%d" % account.apply_interest())
			"rate":
				BankAccount.interest_percent = int(parts[1])
			"info":
				account.display_info()
	print("%s has $%d" % [account.owner_name, account.balance])

جميع دروس البرمجة كائنية التوجّه

تدرّب بنفسك: مترجم GDScript عبر الإنترنت