Would我的代码被认为是相当优化和有效的?如果没有,我在哪里可以改进代码?程序运行得很好。
如果包含银行帐户数目的字典增加了?,则Would此方法的扩展性很好。
<#>设计:我在main()中只设计和保存了一个对象。即bank_1,包含一个有关多个银行帐户的信息字典。我选择只在需要时在open_bank_account()和check_bank_blanance()中创建对象D3和D4,这些对象在函数完成后就消失了。
意图:这里的目的是在main()中使用尽可能少的对象。
输出:程序运行良好;只需输入1,然后输入2,然后输入777,然后是777。它将打印帐户id 777的1.00美元的银行帐户余额。
class Bank_Account:
def __init__(self, account_id, account_pin, account_balance):
self._account_id = account_id
self._account_pin = account_pin
self._account_balance = account_balance
self._individual_bank_account_dicitionary = {}
self._individual_bank_account_dicitionary[account_id] = {"Pin": account_pin, "Balance": account_balance}
def get_bank_account_balance(self, account_id):
return "Balance: ${:.2f}".format(self._individual_bank_account_dicitionary[account_id]["Balance"])
def __str__(self):
return "{}".format(self._individual_bank_account_dicitionary)
class Bank:
def __init__(self, bank_name):
self._bank_name = bank_name
self._bank_dicitionary = {}
def update_bank_dictionary(self, bank_account):
self._bank_dicitionary[bank_account._account_id] = {"Pin": bank_account._account_pin, "Balance": bank_account._account_balance}
# 1. A method to return the dictionary of bank accounts of the object from class Bank.
# 2. In this case, only bank_1 from main() is the object argument.
def get_bank_dictionary(self):
return self._bank_dicitionary
# 1. Method to create object bank_account only when needed.
def get_bank_account(self, account_id):
account_pin = self._bank_dicitionary[account_id]["Pin"]
account_balance = self._bank_dicitionary[account_id]["Balance"]
bank_account = Bank_Account(account_id, account_pin, account_balance)
return bank_account
# 1. This is used to convert the dictionary into a string for printing purposes.
def string_bank_dictionary(self):
string_list = ["account ID: {} \naccount Pin: {} \nBank Balance: {} \n".format(key, self._bank_dicitionary[key]["Pin"], self._bank_dicitionary[key]["Balance"])\
for key, value in self._bank_dicitionary.items()]
return "\n".join(string_list)
def __str__(self):
return "{}".format(self.string_bank_dictionary())
def open_bank_account(bank):
# # Uncomment out when running actual program.
# account_id = input("Enter account id here: ")
# account_pin = input("Enter account pin here: ")
# account_balance = input("Enter account initial balance here: ")
# Comment out when running actual program.
# Currently in used for testing purposes.
account_id = 455
account_pin = 123
account_balance = 888
bank_account = Bank_Account(account_id, account_pin, account_balance)
bank.update_bank_dictionary(bank_account)
# Comment out when running actual program.
# Currently in used for testing purposes.
account_id = 777
account_pin = 777
account_balance = 1
bank_account = Bank_Account(account_id, account_pin, account_balance)
bank.update_bank_dictionary(bank_account)
# Comment out when running actual program.
# Currently in used for testing purposes.
account_id = 631
account_pin = 222
account_balance = 50
bank_account = Bank_Account(account_id, account_pin, account_balance)
bank.update_bank_dictionary(bank_account)
return bank
def check_bank_blanance(bank):
valid_id_password = False
temporary_dictionary = bank.get_bank_dictionary()
while True:
account_id = int(input("Enter account id here: "))
account_pin = int(input("Enter account pin here: "))
for key in temporary_dictionary.keys():
if account_id == key and temporary_dictionary[account_id]["Pin"] == account_pin:
valid_id_password = True
bank_account = bank.get_bank_account(account_id)
print(bank_account.get_bank_account_balance(account_id))
break
if valid_id_password == True:
break
else:
print("Invalid account id/password. Please try again.")
def main():
bank_1 = Bank("ABC Bank")
while True:
print("Menu \n1. Open bank account \n2. Check balance")
while True:
account_choice = int(input("Enter option here: "))
if account_choice <= 0 and account_choice >= 7:
account_choice = int(input("Enter option here: "))
else:
break
if account_choice == 6:
break
elif account_choice == 1:
bank_1 = open_bank_account(bank_1)
elif account_choice == 2:
balance = check_bank_blanance(bank_1)
main()发布于 2019-09-23 16:08:45
如果包含银行账户数量的字典增加,这种方法会扩展得很好吗?
是的,我认为字典访问速度快,O(1)或“恒定时间”。
check_bank_blanance应该是check_bank_balance,或者更可能是check_balance。
_individual_bank_account_dicitionary应该是_individual_bank_account_dictionary。那就是说-为什么这家伙根本不存在?据我所见,它只包含应该是类成员的属性。
只需调用get_bank_account_balance get_balance --或者,如果您将其称为@property,只需balance即可。
return "{}".format(self._individual_bank_account_dicitionary)应该只是
return str(self._individual_bank_account_dictionary)Bank_Account应该被命名为BankAccount。
方法get_bank_dictionary可能不应该存在。您正在公开应该由类本身处理的数据。此代码:
for key in temporary_dictionary.keys():
if account_id == key and temporary_dictionary[account_id]["Pin"] == account_pin:应该存在于Bank类中,可能作为get_account()存在。另外,不要在dict中循环查找键--只需使用[]进行丁查找即可。
https://codereview.stackexchange.com/questions/229488
复制相似问题