如何在oop中制作传递函数
How to make a transfer function in oop
大家好,我想问一下我应该如何制作转账功能,例如我想转账。我有两个变量 bank = BankAccount,如果 accountNumber 不正确,我想转账给他们我想打印这个帐号不存在你能帮我解决这个问题谢谢你 lot.Here 是我迄今为止尝试过的,告诉我代码是否好或者我可以改变一些东西,非常感谢!
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()
要进行转移,您必须将每个人分配给单独的变量
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('---')
结果:
---
Name is: Bill
AccountNumber: 42919502
Salary: 3900
---
Name is: John
AccountNumber: 30503202104
Salary: 4100
---
但我更愿意将其命名为 class Account
并且我会删除 account_list
、open_account
、see_account
或将其移至 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)
结果:
===== 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]
编辑:
最终你可以使用正常的 dictionary
而不是 Account
但是你必须将函数从 Account
移动到 Bank
并使用 account_number
作为第一个参数
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)
结果:
===== 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]
大家好,我想问一下我应该如何制作转账功能,例如我想转账。我有两个变量 bank = BankAccount,如果 accountNumber 不正确,我想转账给他们我想打印这个帐号不存在你能帮我解决这个问题谢谢你 lot.Here 是我迄今为止尝试过的,告诉我代码是否好或者我可以改变一些东西,非常感谢!
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()
要进行转移,您必须将每个人分配给单独的变量
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('---')
结果:
---
Name is: Bill
AccountNumber: 42919502
Salary: 3900
---
Name is: John
AccountNumber: 30503202104
Salary: 4100
---
但我更愿意将其命名为 class Account
并且我会删除 account_list
、open_account
、see_account
或将其移至 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)
结果:
===== 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]
编辑:
最终你可以使用正常的 dictionary
而不是 Account
但是你必须将函数从 Account
移动到 Bank
并使用 account_number
作为第一个参数
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)
结果:
===== 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]