Bank Konto-Klassen. Wie machen es leistungsfähiger/effizienter? (OOP -)

Möchte ich dieses Programm ohne die Verwendung von globalen Variablen. Grundsätzlich, wenn die person zieht sich zurück und landet gehen in negativen zahlen Sie eine Gebühr in Höhe von 5. Jede Transaktion, die im minus wird auch dazu führen, eine Strafe von 5.

Das Programm scheint zu funktionieren, vorausgesetzt, es gibt nur 2 accounts:

account1 = Bankaccount(20)
account2 = Bankaccount(5)

aber das ist seine Einschränkung. Wie kann ich unendlich accounts? So habe ich nicht bekommen, beschränkt sich auf die beiden globals. Ich hoffe, das macht Sinn, nehme ich das ändern Rückzug-Funktion und get_fees, aber ich bin neu in der OOP, also bin ich stecken. Vielen Dank für Ihre Hilfe!

pen = 0
pen2 = 0


class BankAccount:


 def __init__(self, initial_balance):
        """Creates an account with the given balance."""
        self.money = initial_balance

    def deposit(self, amount):
        """Deposits the amount into the account."""
        self.money += amount
        return self.money

    def withdraw(self, amount):
        """
        Withdraws the amount from the account.  Each withdrawal resulting in a
        negative balance also deducts a penalty fee of 5 dollars from the balance.
        """
        global pen, pen2
        penalty = 5

        if self.money - amount < 0:
            self.money -= (amount + penalty)
            if self == account1:
                pen += 5
            elif self == account2:
                pen2 += 5
        else:
            self.money -= amount
        return self.money


    def get_balance(self):
        """Returns the current balance in the account."""
        return self.money

    def get_fees(self):
        """Returns the total fees ever deducted from the account."""
        global pen, pen2
        if self == account1:
            return pen
        elif self == account2:
            return pen2
  • warum nicht fügen Sie pen Standardtitel zu berücksichtigen?
  • Warum haben Sie nicht ein Wörterbuch, wo die Tasten sind die account-Namen und-Werte sind ein BankAccount-Objekt, dass nur die tracks selbst?
  • In der Regel verwendet man einen list zu speichern, die eine unbestimmte Anzahl von Elementen. E. g. accounts = [BankAccount(100), BankAccount(200), BankAccount(333)]. Ein Wörterbuch ist eine andere Möglichkeit: accounts = {'FooAccount': BankAccount(10), 'BarAccount': BankAccount(20)}.
InformationsquelleAutor | 2013-11-19
Schreibe einen Kommentar