Home > Software design >  How to make a transfer function in oop
How to make a transfer function in oop

Time:04-14

Hello guys i want to ask you about how should i make a transfer function for example i want to transfer money .I have two variables bank = BankAccount and i want to transfer money to them if accountNumber for example is not correct I want to print this account number didn't exist can you help me about this idea thank you a lot.Here's what i tried by far and tell me if code is good or i could change something ,Thank you a lot!

import time
import datetime
class BankSystem:
    total_deposit = 0
    total_withdraw = 0
    def __init__(self,name,accountNumber,born,salary):
        self.name = name
        self.accountNumber = accountNumber
        self.born = born
        self.salary = salary
        self.withdraw_history = []
        self.deposit_history = []
        self.account_list = []
    def description(self):
        print("Name is: " , self.name)
        print("AccountNumber: " , self.accountNumber)
        print("Salary: " , self.salary)
    def deposit(self,deposit):
        self.salary = self.salary   deposit 
        self.deposit_history.append(deposit)
        self.total_deposit  = 1
    def withdraw(self,withdraw):
        self.salary = self.salary - withdraw
        self.withdraw_history.append(deposit)
        self.total_withdraw  = 1
    def transaction_history(self):
        print("You have withdraw", self.withdraw_history , "On date:" , datetime.datetime.now())
        print("You have deposit" , self.deposit_history , "On date:" , datetime.datetime.now())
    def take_loan(self):
        answer = int(input("Enter the amount of loan who would you like to take between - 100Euros and 300 Euros: "))
        if answer > 300:
            print("Choose between 100 - 300 Euros not more")
        else:
            print("You have taken out for loan, you will pay extra 1.5 from that sum" , answer * 1.5)
    def open_account(self):
        self.account_list.append(self.name)
        self.account_list.append(self.accountNumber)
        self.account_list.append(self.born)
    def see_account(self):
            print("New Account list" , self.account_list)
    
        
        
bank = BankSystem("Bill" , 42919502 , "Massachutes" , 4000)
bank = BankSystem("John" , 30503202104 , "Cardiff" , 4000)
bank.open_account()
bank.see_account()

CodePudding user response:

To make transfer you have to assign every person to separated variable

account1 = BankSystem("Bill", 42919502, "Massachutes", 4000)

account2 = BankSystem("John", 30503202104, "Cardiff", 4000)

# transfer 100 from Bill to John
value = 100
account1.withdraw(value)
account2.deposit(value)

print('---')

account1.description()

print('---')

account2.description()

print('---')

Result:

---
Name is:  Bill
AccountNumber:  42919502
Salary:  3900
---
Name is:  John
AccountNumber:  30503202104
Salary:  4100
---

But I would rather named this class Account and I would remove account_list, open_account, see_account or move it to class Bank.

import datetime

class Account:

    def __init__(self, name, number, born, money=0):
        self.number = number
        self.name = name
        self.born = born
        self.money = money

        self.number_of_deposits = 0
        self.number_of_withdraws = 0
        
        self.history = []
        
    def description(self):
        print("Name is: " , self.name)
        print("Account Number: " , self.number)
        print("Money: " , self.money)
        
    def deposit(self, value):
        self.money  = value
        self.history.append(value)
        self.number_of_deposits  = 1
        return True
    
    def withdraw(self, value):
        if self.money < value:
            print(f'{self.name} no enought money')
            return False
        
        self.money -= value
        self.history.append(-value)
        self.number_of_withdraws  = 1
        return True
    
    def transaction_history(self):
        today = datetime.datetime.now()
        print("You have withdraw", self.number_of_withdraws , "On date:" , today)
        print("You have deposit" , self.number_of_deposits  , "On date:" , today)
        
    def take_loan(self):
        answer = int(input("Enter the amount of loan who would you like to take between - 100Euros and 300 Euros: "))
        if answer > 300:
            print("Choose between 100 - 300 Euros not more")
        else:
            print("You have taken out for loan, you will pay extra 1.5 from that sum" , answer * 1.5)
    
class Bank:
    
    def __init__(self):
        self.all_accounts = {}
        
    def open_account(self, name, account_number, born, money):
        account = Account(name, account_number, born, money)
        self.all_accounts[account_number] = account
    
    def show_accounts(self, show_history=False):
        for number, account in self.all_accounts.items():
            print('\n---', number, '---\n')
            account.description()
            if show_history:
                print('History:', account.history)
            
    def transfer(self, account_number1, account_number2, value):
        # check if it can withdraw
        if self.all_accounts[account_number1].withdraw(value):
            self.all_accounts[account_number2].deposit(value)
     
    def get_account(self, number):
        return self.all_accounts[number]
                                      
# --- main ---        
        
bank = Bank()
bank.open_account("Bill", 42919502, "Massachutes", 4000)
bank.open_account("John", 30503202104, "Cardiff", 4000)

print('\n===== before =====\n')
bank.show_accounts(show_history=True)

bank.transfer(42919502, 30503202104, 100)
bank.transfer(42919502, 30503202104, 300)
bank.transfer(30503202104, 42919502, 500)

print('\n===== after =====\n')
bank.show_accounts(show_history=True)

Result:

===== before =====


--- 42919502 ---

Name is:  Bill
Account Number:  42919502
Money:  4000
History: []

--- 30503202104 ---

Name is:  John
Account Number:  30503202104
Money:  4000
History: []

===== after =====


--- 42919502 ---

Name is:  Bill
Account Number:  42919502
Money:  4100
History: [-100, -300, 500]

--- 30503202104 ---

Name is:  John
Account Number:  30503202104
Money:  3900
History: [100, 300, -500]

EDIT:

Eventually you could use normal dictionary instead of Account but then you would have to move functions from Account to Bank and use account_number as first argument

import datetime

class Bank:
    
    def __init__(self):
        self.all_accounts = {}
        
    def open_account(self, name, account_number, born, money):
        account = {
            'name': name,
            'number': account_number,
            'born': born,
            'money': money,
            'number_of_deposits': 0,
            'number_of_withdraws': 0,
            'history': []
        }
        self.all_accounts[account_number] = account
    
    def show_accounts(self, show_history=False):
        for number, account in self.all_accounts.items():
            print('\n---', number, '---\n')
            self.description(number)
            if show_history:
                print('History:', account['history'])
            
    def transfer(self, account_number1, account_number2, value):
        # check if it can withdraw
        if self.withdraw(account_number1, value):
            self.deposit(account_number2, value)
     
    def get_account(self, number):
        return self.all_accounts[number]

    # -- from Account, need account_number as first argument ---
    
    def description(self, number):
        account = self.get_account(number)
        print("Name is: " , account['name'])
        print("Account Number: " , account['number'])
        print("Money: " , account['money'])
        
    def deposit(self, number, value):
        account = self.get_account(number)
        account['money']  = value
        account['history'].append(value)
        account['number_of_deposits']  = 1
        return True
    
    def withdraw(self, number, value):
        account = self.get_account(number)
        if account['money'] < value:
            print(f'{self.name} no enought money')
            return False
        
        account['money'] -= value
        account['history'].append(-value)
        account['number_of_withdraws']  = 1
        return True
    
    def transaction_history(self, number):
        account = self.get_account(number)
        today = datetime.datetime.now()
        print("You have withdraw", account['number_of_withdraws'] , "On date:" , today)
        print("You have deposit" , account['number_of_deposits']  , "On date:" , today)
        
    def take_loan(self, number):
        account = self.get_account(number)
        answer = int(input("Enter the amount of loan who would you like to take between - 100Euros and 300 Euros: "))
        if answer > 300:
            print("Choose between 100 - 300 Euros not more")
        else:
            print("You have taken out for loan, you will pay extra 1.5 from that sum" , answer * 1.5)

# --- main ---        
        
bank = Bank()
bank.open_account("Bill", 42919502, "Massachutes", 4000)
bank.open_account("John", 30503202104, "Cardiff", 4000)

bank.deposit(42919502, 500)
bank.withdraw(30503202104, 1200)

print('\n===== before =====\n')
bank.show_accounts(show_history=True)

bank.transfer(42919502, 30503202104, 100)
bank.transfer(42919502, 30503202104, 300)
bank.transfer(30503202104, 42919502, 500)

print('\n===== after =====\n')
bank.show_accounts(show_history=True)

Result:

===== before =====


--- 42919502 ---

Name is:  Bill
Account Number:  42919502
Money:  4500
History: [500]

--- 30503202104 ---

Name is:  John
Account Number:  30503202104
Money:  2800
History: [-1200]

===== after =====


--- 42919502 ---

Name is:  Bill
Account Number:  42919502
Money:  4600
History: [500, -100, -300, 500]

--- 30503202104 ---

Name is:  John
Account Number:  30503202104
Money:  2700
History: [-1200, 100, 300, -500]
  • Related