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


#1

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

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

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

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

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


#2

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

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()


#3

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

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

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


#4

السلام عليكم أستاذي @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:


#5
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()

#6
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()

#7

الحمد لله اتممت المهمة المطلوبة :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()

#8

عمل رائع يا سارة :+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

#9

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


#10

يعني هكذا لقد قمت بتوظيف دالة أخرى 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()








#11

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

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


#12

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


#13

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


#14

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


#15

نعم بالضبط


#16

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


#18

السلام عليكم
أخي ياسر هذا أول مشروع لي أشاركه معكم في مجتمع كورتابز
أرجو منك معاينة مشروعي وإعلامي بأخطائي وهل قمت بكتابة 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()

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


#19

ما شاء الله :+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