مشاركة حلول: الاحتفاظ بعمليات السحب

مرحباً جميعاً

المهمة المطلوبة

بإمكانك الإطلاع على المهمة المطلوبة من هنا

كيفية كتابة الأكواد في المنتدى

  • إذا كان لديك كيبورد فرنسي, اضغط على alt gr و 7 لكتابة الرمز `

2 Likes

السلام عليكم
اليكم محاولتي

class ATM:
    def __init__(self, bank_name, balance):
        self.bank_bame = bank_name
        self.balance = balance
        self.withdrawals_list = []

    def withdraw(self, request):

        print("Current balance = ", self.balance)
        result = self.balance

        if request > self.balance:
            print("Can't give you all this money !!")

        elif request < 0:
            print("More than zero plz!")

        else:
            self.withdrawals_list.append(request)
            result = self.balance - request

            notes = [100, 50, 10, 5]
            for note in notes:
                while request >= note:
                    request -= note
                    print("give " + str(note))
            if request % 5 != 0:
                print('give ' + str(request) + ' pieces')
                request = 0

        return result

    def show_withdrawals(self):
        for withdrawal in self.withdrawals_list:
            print(withdrawal)


balance1 = 500
balance2 = 1000

atm1 = ATM("Smart Bank", balance1)
atm2 = ATM("Baraka Bank", balance2)

atm1.withdraw(700)
atm1.withdraw(300)

atm2.withdraw(500)
atm2.withdraw(250)

atm2.show_withdrawals()

1 Like

قمت بتشغيل البرنامج, وظهر لي التالي:

Current balance =  1000                                                                                                                          
give 100                                                                                                                                         
give 100                                                                                                                                         
give 100                                                                                                                                         
give 100                                                                                                                                         
give 100                                                                                                                                         
Current balance =  1000                                                                                                                          
give 100                                                                                                                                         
give 100                                                                                                                                         
give 50                                                                                                                                          

أي أنه قام بالسحب وما زال الرصيد كما هو, هل بإمكانك اكتشاف المشكلة ؟

1 Like

السلام عليكم أستاذي @YaserAlnajjar
الحقيقة لم أنتبه أن عملية خصم المبلغ من الرصيد لم تكن موجودة أصلا في الدرس
لقد تم التعديل في الريبو
إليك الكود:

class ATM:
    def __init__(self, bank_name, balance):
        self.bank_bame = bank_name
        self.balance = balance
        self.withdrawals_list = []

    def withdraw(self, request):

        print("Current balance = "+ str(self.balance))
        result = self.balance

        if request > self.balance:
            print("Can't give you all this money !!")

        elif request < 0:
            print("More than zero plz!")

        else:
            self.withdrawals_list.append(request)
            result = self.balance - request

            notes = [100, 50, 10, 5]
            for note in notes:
                while request >= note:
                    request -= note
                    print("give " + str(note))
            if request % 5 != 0:
                print('give ' + str(request) + ' pieces')
                request = 0
            self.balance -= result
            
        return result

    def show_withdrawals(self):
        for withdrawal in self.withdrawals_list:
            print(withdrawal)


balance1 = 500
balance2 = 1000

atm1 = ATM("Smart Bank", balance1)
atm2 = ATM("Baraka Bank", balance2)

atm1.withdraw(700)
atm1.withdraw(300)

atm2.withdraw(500)
atm2.withdraw(250)

atm2.show_withdrawals()

شكرا ::rose:

1 Like
class ATM:

	def __init__(self,balance,bank_name):
		self.balance = balance
		self.bank_name = bank_name
		self.withdrawals_list = []

	def show_withdrawals(self):
		for withdrawal in self.withdrawals_list:
			print("withdrawal: "+str(withdrawal))
			


	def print_information(self):
		print("Welcome to "+ self.bank_name)
		print("current balance: " + str(self.balance))


	def check_balance(self , request):	
		if self.balance < request:
			print("Can't give you all this money !!")

		elif request < 0:
			print("More than zero plz!")

	

	def withdraw(self, request):
		self.withdrawals_list.append(request)

		if self.balance > request:
			self.balance = self.balance - request

			while request > 0:

				if request >= 100:
					request -= 100
					print("give 100")

				elif request >= 50:
					request -= 50
					print("give 50")

				elif request >= 10:
					request -= 10
					print("give 10")

				else:
					request -= 5
					print("give 5")
		return self.balance			
	 
		    
	
balance1 = 500
balance2 = 1000

atm1 = ATM(balance1, "islamy bank")
atm2 = ATM(balance2, "baraka bank")

atm1.print_information()
atm1.check_balance(300)
atm1.withdraw(300)

atm1.print_information()
atm1.check_balance(250)
atm1.withdraw(250)

atm1.show_withdrawals()

atm2.print_information()
atm2.check_balance(500)
atm2.withdraw(500)

atm2.print_information()
atm2.check_balance(455)
atm2.withdraw(455)

atm2.show_withdrawals()
1 Like
Summary

This text will be hidden

class ATM:
	def __init__(self, balance, bank_name):
		self.balance = balance
		self.bank_name = bank_name
		self.withdrawals_list = []
	def withdraw(self, request):
		print("welcome to ", self.bank_name)
		print("balance = ", self.balance)
		if request <= self.balance:
			if request > 0 :
				self.withdrawals_list.append(request)
				self.balance -= request
				notes = [100, 50, 10, 5]
				for note in notes:
					while request >= note:
						request -= note
						print("give ", str(note))
						if request < note and request != 0:
							print("give ", str(request))
			else:
				print("please try again")
		else:
			print("not enough money")
		return self.balance
	def show_withdrawals(self):
		for withdrawal in self.withdrawals_list:
			print(withdrawal)


atm1 = ATM(500, "Jordan Bank")
atm1.withdraw(700)
atm1.withdraw(300)
atm1.show_withdrawals()

atm2 = ATM(1000, "Islamic Bank")
atm2.withdraw(500)
atm2.withdraw(253)
atm2.show_withdrawals()
2 Likes

الحمد لله اتممت المهمة المطلوبة :star_struck::star_struck:

class ATM:
    def __init__(self, balance, bank_name):
        self.balance = balance
        self.bank_name = bank_name
        self.withdrawals_list = []

    def withdraw(self, request):

        print("the name of bank is :",self.bank_name)
        print("the balance is",self.balance)
        if   request > self.balance:
            print("Can't give you all this money !!")
        elif request < 0:
            print("More than zero plz!")
        else:
            self.withdrawals_list.append(request)
            self.balance -= request
            notes =[100,50,10,5]
            for note in notes :
                while request >= note :
                    request-=note
                    print("give ", str(note))
            if request < 5:
                    print("give " + str(request))
                    request = 0
            return self.balance

    def show_withdrawals(self):
            for withdrawal in self.withdrawals_list:
                print(withdrawal)


balance1=500
balance2=1000
atm1=ATM(balance1,"smart_bank")
atm2=ATM(balance2,"baraka_bank")
atm1.withdraw(700)
atm1.withdraw(300)
print("the rest of balance for smart bank are",atm1.balance)
atm2.withdraw(500)
print("the rest of balance for baraka bank are",atm2.balance)
atm2.withdraw(257)
print("the rest of balance for baraka bank are",atm2.balance)
atm2.show_withdrawals()
1 Like

عمل رائع يا سارة :+1:

بقي تطبيق مفهوم SRP في هذا الجزء:

            self.withdrawals_list.append(request)
            self.balance -= request
            notes =[100,50,10,5]
            for note in notes :
                while request >= note :
                    request-=note
                    print("give ", str(note))
            if request < 5:
                    print("give " + str(request))
                    request = 0
1 Like

شكرا هل اقوم بعمل هذا الكود الذي أشرت اليه داخل دالة منفصلة

1 Like

يعني هكذا لقد قمت بتوظيف دالة أخرى calcul تقوم بعملية السحب ما رأيك

class ATM:
    def __init__(self, balance, bank_name):
        self.balance = balance
        self.bank_name = bank_name
        self.withdrawals_list = []
    def calcul(self,request):
        self.withdrawals_list.append(request)
        self.balance -= request
        notes =[100,50,10,5]
        for note in notes :
            while request >= note :
                request-=note
                print("give ", str(note))
        if request < 5:
            print("give " + str(request))
            request = 0
        return self.balance

    def withdraw(self, request):
        print("the name of bank is :",self.bank_name)
        print("the balance is",self.balance)
        if   request > self.balance:
            print("Can't give you all this money !!")
        elif request < 0:
            print("More than zero plz!")
        else:
            self.calcul(request)

    def show_withdrawals(self):
            for withdrawal in self.withdrawals_list:
                print(withdrawal)


balance1=500
balance2=1000
atm1=ATM(balance1,"smart_bank")
atm2=ATM(balance2,"baraka_bank")
atm1.withdraw(700)
atm1.withdraw(300)
print("the rest of balance for smart bank are",atm1.balance)
atm2.withdraw(500)
print("the rest of balance for baraka bank are",atm2.balance)
atm2.withdraw(257)
print("the rest of balance for baraka bank are",atm2.balance)
atm2.show_withdrawals()







1 Like

عمل مضبوط :+1:

لما الاكواد داخل الدالة لديها ثمان مسافات بدلا من اربع ؟

1 Like

عفوا لقد عدلت المحتوى في الأعلى شكرا

1 Like

ممتاز… انتقلي مباشرة للمهمة التالية :wink:

1 Like

مهمة رفع مشروعي على Git hub ؟

1 Like

نعم بالضبط

1 Like

سلام عليكم وأخيرا عملي على موقع Github شكرا لك أستاذ ياسر على مجهوداتك معنا انا متحمسة جذا معكم
الرابط https://github.com/mofilamamra/My-First-Project-ATM

1 Like

السلام عليكم
أخي ياسر هذا أول مشروع لي أشاركه معكم في مجتمع كورتابز
أرجو منك معاينة مشروعي وإعلامي بأخطائي وهل قمت بكتابة dodgy code أما أنني قد اتبعت قاعدتي الـ SRB & DRY بشكل سليم؟

ملاحظة: لقد جعلت كلاً من الكلاس الأسياسي والأوبجكتس أو الانستنس في ملفين مختلفين:
Check out my code on repl.it
or

bank.py

class ATM ():
	def __init__ (self, your_balance, bank_name,):
		
		self.balance = your_balance
		self.origin_balance = your_balance
		self.name = bank_name
		self.statement = []

	def withdraw (self, request):
		#Available cash paper is: 1000, 500, 200, 100
		print "Welcmome to", self.name

		def multiples_check (n):
			if n % 100 == 0:
				return True
			return False

		def cash_Change (request):
			cash = [1000, 500, 200, 100]
			for paper in cash:
				while request >= paper:
					request -= paper
					print "( Given ==>  1 x", paper,")"

		if request > self.balance:
			print "Sorry!, you don't have enough balance to withdraw in your account."
			print "Your balance is: AED", self.balance, "\nThank you.\n**********\n"

		elif request <100:
			print "Please enter a minimum amount 100 or its multiples!\nThank you.\n**********\n"


		elif multiples_check(request) == False:
			print "you can't withdraw this amount please select a multiples of amount 100!\nThank you.\n**********\n"

		else:
			print "You have", self.balance, "AED in your account.\n"
			print "Requested Amount is AED", request
			self.balance -= request

			give = request
			cash_Change (request); print
			print "Amount AED", give, "has been withdrawen succesfully from your account."
			print "Your balance now is: AED", self.balance, "\nThank you.\n**********\n"
			self.statement.append(request)
			return self.balance

	def transactions (self):
		self.origin_balance
		print "--------------------------------------------"
		print "Your recent transaction statement of", self.name, ":"

		for trans in self.statement:
			self.origin_balance -= trans
			print "-",trans, " / Your new balance is AED", self.origin_balance
		print "\nThank you.\n--------------------------------------------"

2_withdrawal

import bank


noor_atm = bank.ATM (5000, "Noor Bank")
dib_atm = bank.ATM (500, "Dubai Islamic Bank")


noor_atm.withdraw(3800)
noor_atm.withdraw(200) 
noor_atm.withdraw(100) 
noor_atm.withdraw(600) 

print

dib_atm.withdraw(0)
dib_atm.withdraw(90)
dib_atm.withdraw(1000)
dib_atm.withdraw(350)
print
noor_atm.transactions()

بانتظار النتيجة !!

2 Likes

ما شاء الله :+1:

كل شيء رائع, فقط يبقى

1. تخلص من الأكواد الإضافية:

الدالة التالية:

def multiples_check (n):
			if n % 100 == 0:
				return True
			return False

بدل أن تكتب دالة كاملة, بإمكانك اختصارها في سطر بهذا الشكل:

is_100_multiples = n % 100 == 0
elif is_100_multiples:

أيضاً السطر التالي في الدالة (transactions) لا يقوم بعمل شيء:

self.origin_balance

2. تسميه الدوال

  1. الدالة cash_Change الأصح cash_change (كل الحروف small)
  2. الدالة transactions الأصح show_transactions لتوضيح عملها (لأنها ليست property)

بشكل عام حاول أن تجعل الدوال تبدأ بفعل verb, مثل do_something أو change_cash

3 Likes

الاحتفاظ بعمليات السحب

class ATM:

    def __init__(self, balance ,bank_name):
        self.balance= balance
        self.bank_name = bank_name
        self.withdrawals_list=[]

    def withdraw(self,request):
        papers=[100,50,10,5,2]
        while request > 0:
            if request > self.balance :
                print(" can't give you all this money !!")
                break
            self.withdrawals_list.append(request)
            for i in papers:
                while request >= i:
                    print('give' +' '+ str(i))
                    # request+=1
                    self.balance-=i
                    request-=i
                    # if not request==0 :break
            request-=1

        print('Your Balance Is'+' '+ str(self.balance))
        return self.balance

    def show_withdrawals(self):
        for widthdrawals in self.withdrawals_list:
            print(widthdrawals)


balance1 = 1000
balance2 = 1000

atm1=ATM(balance1, 'Smart Bank')
atm2=ATM(balance2, 'Old Bank')

atm1.withdraw(300)
atm1.withdraw(500)
atm1.show_withdrawals()
1 Like

عمل رائع يا احمد :+1:

فقط بالنسبة ل break، هل بامكانك عمل البرنامج بدونها؟

لانه لا ينصح باستعمالها (الا نادرا)