Python:从银行系统中删除客户帐户

时间:2018-01-11 19:13:01

标签: python

已经问过这个问题而且我的回答非常消极,所以我预先告诉你我对Python和整个编程非常,所以我不太了解大多数术语,但无论如何我都会尝试

我必须在customers_list

中插入一个删除bank_system客户帐户的方法

我将在此处放置bank_system的代码示例。 customers_list位于顶部,删除帐户的方法位于底部附近。

from customer import Customer
from admin import Admin
from account import Account




class BankSystem(object):
    def __init__(self):
        self.customers_list = []
        self.admins_list = []
        self.load_bank_data()

    def load_bank_data(self):
        #CUSTOMER LIST
        customer_1 = Customer("Adam", "1234", ["14", "Wilcot Street", "Bath", "B5 5RT"])
        account_no = 1234
        account_1 = Account(5000.00, account_no)
        customer_1.open_account(account_1)
        self.customers_list.append(customer_1)

        customer_2 = Customer("David", "password", ["60", "Holborn Via-duct", "London", "EC1A 2FD"])
        account_no+=1
        account_2 = Account(3200.00, account_no)
        customer_2.open_account(account_2)
        self.customers_list.append(customer_2)

        customer_3 = Customer("Alice", "MoonLight", ["5", "Cardigan Street", "Birmingham", "B4 7BD"])
        account_no+=1
        account_3 = Account(18000.00, account_no)
        customer_3.open_account(account_3)
        self.customers_list.append(customer_3)

        customer_4 = Customer("Ali", "150A", ["44", "Churchill Way West", "Basingstoke", "RG21 6YR"])
        account_no+=1
        account_4 = Account(18000.00, account_no)
        customer_4.open_account(account_4)
        self.customers_list.append(customer_4)

        customer_5 = Customer("Thomas", "15A", ["4", "Churchill West", "Stoke", "ST21 6YR"])
        account_no+=1
        account_5 = Account(0, account_no)
        customer_5.open_account(account_5)
        self.customers_list.append(customer_5)

        #ADMIN LIST
        admin_1 = Admin("Julian", "1441", True, ["12", "London Road", "Birmingham", "B95 7TT"])
        self.admins_list.append(admin_1)

        admin_2 = Admin("Eva", "2222", False, ["47", "Mars Street", "Newcastle", "NE12 6TZ"])
        self.admins_list.append(admin_2)

    def customer_login(self, name, password):
        #step A.1 CUSTOMER LOGIN
        found_customer = self.search_customers_by_name(name)
        if found_customer == None:
            return("\n The customer you are looking for has not been found!\n")
        else:
            if (found_customer.check_password(password) == True):
                self.run_customer_options(found_customer)
            else:
                return("You have input an incorrect password")


    def admin_login(self, name, password):
        #step A.3 ADMIN LOGIN
        found_admin = self.search_admin_by_name(name)
        if found_admin == None:
            return("\n The administrator you are looking for has not been found!\n")
        else:
            if (found_admin.check_password(password) == True):
                self.run_admin_options(found_admin)
            else:
                return("You have input an incorrect password")


    def search_customers_by_name(self, customer_name):
        #step A.2 SEARCHING FOR CUSTOMERS
        found_customer = None
        for a in self.customers_list:
            name = a.get_name()
            if name == customer_name:
                found_customer = a
            if found_customer == None:
                print("\n The customer %s doesn't exist! Please try again...\n" %customer_name)
            else:
                return found_customer


    def main_menu(self):
        #MAIN MENU OPTIONS
        print()
        print()
        print ("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-")
        print()
        print ("Welcome to the Python Bank System")
        print()
        print ("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-")
        print ("1) Admin login")
        print ("2) Customer login")
        print ("3) Quit the Python Bank System")
        print (" ")
        option = int(input("Choose your option: "))
        return option

    def run_main_option(self):
        loop = 1
        while loop == 1:
            choice = self.main_menu()
            if choice == 1:
                #ADMIN LOGIN
                name = input ("\n Please input admin name: ")
                password = input ("\n Please input admin password: ")
                msg = self.admin_login(name, password)
                print(msg)
            elif choice == 2:
                #CUSTOMER LOGIN
                name = input ("\n Please input customer name: ")
                password = input ("\n Please input customer password: ")
                msg = self.customer_login(name, password)
                print(msg)
            elif choice == 3:
                loop = 0
        print ("Thank you for stopping by the bank!")


    def trasnfer_money(self, sender_account, receiver_name, receiver_account_no, amount):
        #TRANSFERRING MONEY TO OTHER ACCOUNTS
        pass


    def customer_menu(self, customer_name):
        #CUSTOMER SPECIFIC MENU
         print (" ")
         print ("Welcome %s : Your transaction options are:" %customer_name)
         print ("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-")
         print ("1) Transfer money")
         print ("2) Other account operations")
         print ("3) profile settings")
         print ("4) Sign out")
         print (" ")
         option = int(input ("Choose your option: "))
         return option


    def run_customer_options(self, customer):
        #CUSTOMER OPTIONS            
        account = customer.get_account()            
        loop = 1
        while loop == 1:
            choice = self.customer_menu(customer.get_name())
            if choice == 1:
                pass
            elif choice == 2:
                account.run_account_options()
            elif choice == 3:
                customer.run_profile_options()
            elif choice == 4:
                loop = 0
        print ("Exit account operations")



    def search_admin_by_name(self, name):
        #step A.4 SEARCHING FOR ADMINS
        found_admin = None
        admin_name = name
        for a in self.admins_list:
            name = a.get_name()
            if name == admin_name:
                found_admin = a
            if found_admin == None:
                print("\n The administrator %s doesn't exist! Please try again...\n" %admin_name)
            else:
                return found_admin


    def admin_menu(self, admin_name):
        #ADMINISTRATOR SPECIFIC MENU
        print (" ")
        print ("Welcome Admin %s : Available options are:" %admin_name)
        print ("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-")
        print ("1) Transfer money")
        print ("2) Customer account operations")
        print ("3) Customer profile settings")
        print ("4) Admin profile settings")
        print ("5) Delete customer")
        print ("6) Print all customers detail")
        print ("7) Sign out")
        print (" ")
        option = int(input ("Choose your option: "))
        return option


    def run_admin_options(self, admin):
        #ADMINISTRATOR OPTIONS
        loop = 1
        while loop == 1:
            choice = self.admin_menu(admin.get_name())
            if choice == 1:
                pass
            elif choice == 2:
                #step A.5 SEARCHING FOR CUSTOMERS
                customer_name = input("\n Please input customer name :\n")
                customer = self.search_customers_by_name(customer_name)
                if customer != None:
                    account = customer.get_account()
                    if account != None:
                        account.run_account_options()
            elif choice == 3:
                #step A.6 UPDATING CUSTOMER NAME
                customer_name = input("\n Please input customer name :\n")
                customer = self.search_customers_by_name(customer_name)
                if customer != None:
                    customer.run_profile_options()
            elif choice == 4:
                #step A.7 UPDATING ADMIN NAME (SELF ONLY)
                admin.run_profile_options()
            elif choice == 5:
                #step A.8 DELETING CUSTOMER
                if admin.has_full_admin_right() == True:
                    name = input("\n Please input customer name you want to delete :\n")
                    customer_account = self.search_customers_by_name(name)
                    if customer_account !=None:
                        print ("The customer does not exist")
                    else: self.customers_list.remove(name)       
                else:
                    print("\n Only administrators with full admin rights can remove a customer from the bank system!\n")
            elif choice == 6:
                #step A.9 PRINTING ALL CUSTOMER DETAILS
                self.print_all_accounts_details()
            elif choice == 7:
                loop = 0
        print ("Exit account operations")


    def print_all_accounts_details(self):
            # list related operation - move to main.py
            i = 0
            for c in self.customers_list:
                i+=1
                print('\n %d. ' %i, end = ' ')
                c.print_details()
                print("------------------------")

app = BankSystem()
app.run_main_option()

使用我目前的代码,Python shell返回:

Please input customer name you want to delete :
Adam
The customer does not exist

Welcome Admin Julian : Available options are:
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
1) Transfer money
2) Customer account operations
3) Customer profile settings
4) Admin profile settings
5) Delete customer
6) Print all customers detail
7) Sign out

Choose your option: 

如您所见,当我输入'Adam'时,Python只返回' The customer does not exist'

考虑到这一点,然后回顾我的代码以及删除帐户的方法,这意味着search_customers_by_name方法无法正常工作。但是,使用此方法的所有其他选项都可以正常工作。

我该怎么做才能纠正这个问题?

1 个答案:

答案 0 :(得分:0)

在删除选项中,如果找到客户,则显示客户不存在的消息(如果customer_account != 无:)。通过颠倒这个(如果customer_account == 无:)它会提醒您是否找不到客户,我认为这是理想的行为。更正后的代码如下:

elif choice == 5:
                #step A.8 DELETING CUSTOMER
                if admin.has_full_admin_right() == True:
                    name = input("\n Please input customer name you want to delete :\n")
                    customer_account = self.search_customers_by_name(name)
                    if customer_account == None:
                        print ("The customer does not exist")
                    else: self.customers_list.remove(name)       
                else:
                    print("\n Only administrators with full admin rights can remove a customer from the bank system!\n")