Please who can correct this code?

import unittest
class AccountBalanceTestCases(unittest.TestCase):
  def setUp(self):
    self.my_account = BankAccount(90)
    
  def test_balance(self):
    self.assertEqual(self.my_account.balance, 90, msg='Account Balance Invalid')
    
  def test_deposit(self):
    self.my_account.deposit(90)
    self.assertEqual(self.my_account.balance, 180, msg='Deposit method inaccurate')
    
  def test_withdraw(self):
    self.my_account.withdraw(40)
    self.assertEqual(self.my_account.balance, 50, msg='Withdraw method inaccurate')
    
  def test_invalid_operation(self):
    self.assertEqual(self.my_account.withdraw(1000), "invalid transaction", msg='Invalid transaction')
  
  def test_sub_class(self):
    self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='No true subclass of BankAccount')

HERE’S THE QUESTION
Create a class called BankAccount

Create a constructor that takes in an integer and assigns this to a balance property.Create a method called deposit that takes in cash deposit amount and updates the balance accordingly.Create a method called withdraw that takes in cash withdrawal
amount and updates the balance accordingly. if amount is greater than
balance return "invalid transaction"Create a subclass MinimumBalanceAccount of the BankAccount class

@devsolver85255 a possible way to implement it and no I did not do everything. Hence you need to work on it and complete it as well as improve it

class BankAccount():

	"""docstring for BankAccount"""
	def __init__(self, balance):
		self.balance = balance

	"""docstring for method """
	def deposit(self, amount):
		self.__checkinput(amount)
		#update self.balance below
		

	"""docstring for method """
	def withdraw(self, amount):
		self.__checkinput(amount)
		if(amount > self.balance):
			raise Exception("invalid transaction")
		#update self.balance below

	"""docstring for method """	
	def __repr__(self):
		#how to represent every instance
		#implement it
		pass

	"private method to validate input"
	def __checkinput(self, value):
		if value < 0:
			return ("Enter a valid amount")

class MinimumBalanceAccount(BankAccount):
	"""docstring for ClassName"""
	def __init__(self,arg):
		#implement

mine = BankAccount(200)
print (mine.balance) #200

mine.deposit(400)
print (mine.balance) #600

mine.withdraw(500)
print (mine.balance) #100

mine.withdraw(200)
print (mine.balance) #Exception
		

with the test cases you provided you just need to add this at the end to run it

if __name__ == '__main__':
	unittest.main()

or you can just call it

unittest.main()

Can you report back with your update?

Hello Folade did you later come up with a solution?

@rydan Hi!

Here’s a version of the code i worked on.

class BankAccount:
  def __init__(self, balance):
    self.balance = balance
    return balance
    
  def deposit(self, amount):
    self.__checkinput(amount)
    self.balance += amount
    return self.balance
    
    
  def withdraw(self, amount):
    self.__checkinput(amount)
    if(amount > self.balance):
      raise RuntimeError("Amount greater than available balance.")
    else:
		self.balance -= amount
    return self.balance
    
    def __checkinput(self, value):
      if value < 0:
        return ("Enter a valid amount")
    
    class MinimumBalanceAccount(BankAccount):
      def __init__(self, minimum_balance):
        BankAccount.__init__(self)
        self.minimum_balance = minimum_balance
      
  act = BankAccount(200)
  act = deposit(400)
  act = withdraw(500)
  act = withdraw(200)
  
  
  unittest.main()

But it still doesn’t seem to run correctly.

1 Like

Hi jeffery did you get the solution?

Try removing the return clause from your constructor.