Home > Enterprise >  How to check owner of an Account?
How to check owner of an Account?

Time:09-29

Here is the task:

Person class

Attributes:

person_id: int - unique number of the person in the system

name: str - name of the person

age: int - age of the person

accounts: List [Account] - list of owned accounts (Account objects)

Methods:

check_integrity (self) -> bool - checks integrity according to the following rules:

1. Bank client must be an adult (18 )
2. The client name cannot be an empty string
3. For each owned account in the list, the person must be set as owner (see below). If everything is as it should, it returns True, otherwise False.

Account class

Attributes:

account_id: int - a unique bank account number

password: str - password for working with the account (in the real world it would never be stored without encryption, but it will be enough for us)

balance: int - an integer account balance

limit: int - an integer maximum that the balance must not exceed

owner: Person - the person (Person object) who owns the account

here is my programme(i need to change check intengrity function because this function is bad) and i dont know how to implement the function.

from typing import List

class Person():

    def __init__(self,person_id: int,name: str,age: int, accounts: List['Account']): 
        self.person_id = person_id
        self.name = name
        self.age = age
        self.accounts = accounts
    
    def check_integrity(self):
        for v in self.accounts:
            if v.owner == Person() and self.age >= 18 and self.name != '':
                return True
            else:
                return False
class Account():

    def __init__(self,account_id: int,password: str,balance: int,limit:int,owner:Person):
        self.account_id = account_id
        self.password = password
        self.balance = balance 
        self.limit = limit 
        self.owner = owner

CodePudding user response:

You want to check that v.owner is the person object for whom check_integrity() is being called (i.e. self), not a new person object like you currently do.

Also, your check_integrity() returns True / False after checking a single account. You want to do this after checking all accounts. You can use the all() function to do this.

def check_integrity(self):
    return 
          self.age >= 18  # Condition 1
      and self.name != '' # Condition 2
      and all(acc.owner == self for acc in self.accounts) # Condition 3

If you don't want to use the all() function This doesn't make sense because why reinvent the wheel when you have one, but anyway...

def check_integrity(self):
    all_accts_valid = True # Assume all accounts are valid
    for acc in self.accounts:
        if acc.owner != self:
            all_accts_valid = False # We found an invalid account
            # Since one is invalid, we don't need to check any more accounts
            # So we can break out of this loop
            break

    return 
          self.age >= 18  # Condition 1
      and self.name != '' # Condition 2
      and all_accts_valid # Condition 3
  • Related