From e3628dfe31378e313583fd061666a8757c3cb33c Mon Sep 17 00:00:00 2001 From: Charlye Tran Date: Fri, 18 Sep 2015 00:20:07 -0400 Subject: [PATCH 1/2] completed exercise --- abcbank/__main__.py | 4 +++ abcbank/account.py | 54 +++++++++++++++++--------------------- abcbank/bank.py | 8 ++---- abcbank/customer.py | 28 +++++++------------- abcbank/transaction.py | 17 ++++++++++-- tests/account_tests.py | 52 ++++++++++++++++++++++++++++++++++++ tests/bank_tests.py | 37 +++++++------------------- tests/customer_tests.py | 45 ++++++++++++++++--------------- tests/transaction_tests.py | 28 ++++++++++++++++---- 9 files changed, 161 insertions(+), 112 deletions(-) create mode 100644 abcbank/__main__.py create mode 100644 tests/account_tests.py diff --git a/abcbank/__main__.py b/abcbank/__main__.py new file mode 100644 index 0000000..bbf300e --- /dev/null +++ b/abcbank/__main__.py @@ -0,0 +1,4 @@ +from account import Account +from bank import Bank +from customer import Customer +from transaction import Transaction \ No newline at end of file diff --git a/abcbank/account.py b/abcbank/account.py index e010009..5db73e9 100644 --- a/abcbank/account.py +++ b/abcbank/account.py @@ -1,43 +1,37 @@ -from abcbank.transaction import Transaction - -CHECKING = 0 -SAVINGS = 1 -MAXI_SAVINGS = 2 - +from transaction import Transaction class Account: def __init__(self, accountType): self.accountType = accountType self.transactions = [] + self.balance = 0 - def deposit(self, amount): - if (amount <= 0): - raise ValueError("amount must be greater than zero") - else: - self.transactions.append(Transaction(amount)) - - def withdraw(self, amount): - if (amount <= 0): - raise ValueError("amount must be greater than zero") + def performTransaction(self, transaction): + self.transactions.append(transaction) + if transaction.type == "Deposit": + self.balance += transaction.amount else: - self.transactions.append(Transaction(-amount)) + self.balance -= transaction.amount + return self def interestEarned(self): - amount = self.sumTransactions() - if self.accountType == SAVINGS: - if (amount <= 1000): - return amount * 0.001 + print(self.balance) + if self.accountType == "SAVINGS": + if (self.balance <= 1000): + return self.balance * 0.001 else: - return 1 + (amount - 1000) * 0.002 - if self.accountType == MAXI_SAVINGS: - if (amount <= 1000): - return amount * 0.02 - elif (amount <= 2000): - return 20 + (amount - 1000) * 0.05 + return 1 + (self.balance - 1000) * 0.002 + if self.accountType == "MAXI_SAVINGS": + if (self.balance <= 1000): + return self.balance * 0.02 + elif (self.balance <= 2000): + return 20 + (self.balance - 1000) * 0.05 else: - return 70 + (amount - 2000) * 0.1 + return 70 + (self.balance - 2000) * 0.1 else: - return amount * 0.001 + return self.balance * 0.001 + + + - def sumTransactions(self, checkAllTransactions=True): - return sum([t.amount for t in self.transactions]) \ No newline at end of file + \ No newline at end of file diff --git a/abcbank/bank.py b/abcbank/bank.py index 44711fe..9395419 100644 --- a/abcbank/bank.py +++ b/abcbank/bank.py @@ -17,9 +17,5 @@ def totalInterestPaid(self): total += c.totalInterestEarned() return total def getFirstCustomer(self): - try: - self.customers = None - return self.customers[0].name - except Exception as e: - print(e) - return "Error" \ No newline at end of file + return self.customers[0].name + \ No newline at end of file diff --git a/abcbank/customer.py b/abcbank/customer.py index 7cfd62a..4a280aa 100644 --- a/abcbank/customer.py +++ b/abcbank/customer.py @@ -1,6 +1,3 @@ -from account import CHECKING, SAVINGS, MAXI_SAVINGS - - class Customer: def __init__(self, name): self.name = name @@ -21,7 +18,7 @@ def getStatement(self): # JIRA-123 Change by Joe Bloggs 29/7/1988 start statement = None # reset statement to null here # JIRA-123 Change by Joe Bloggs 29/7/1988 end - totalAcrossAllAccounts = sum([a.sumTransactions() for a in self.accounts]) + totalAcrossAllAccounts = sum([a.balance for a in self.accounts]) statement = "Statement for %s" % self.name for account in self.accounts: statement = statement + self.statementForAccount(account) @@ -29,27 +26,20 @@ def getStatement(self): return statement def statementForAccount(self, account): - accountType = "\n\n\n" - if account.accountType == CHECKING: + accountType = "account.type" + if account.accountType == "CHECKING": accountType = "\n\nChecking Account\n" - if account.accountType == SAVINGS: + if account.accountType == "SAVINGS": accountType = "\n\nSavings Account\n" - if account.accountType == MAXI_SAVINGS: - accountType = "\n\nMaxi Savings Account\n" - transactionSummary = [self.withdrawalOrDepositText(t) + " " + _toDollars(abs(t.amount)) + if account.accountType == "MAXI_SAVINGS": + accountType = "\n\nMaxi Savings Accounts\n" + + transactionSummary = [t.type + " " + _toDollars(abs(t.amount)) for t in account.transactions] transactionSummary = " " + "\n ".join(transactionSummary) + "\n" - totalSummary = "Total " + _toDollars(sum([t.amount for t in account.transactions])) + totalSummary = "Total " + _toDollars(account.balance) return accountType + transactionSummary + totalSummary - def withdrawalOrDepositText(self, transaction): - if transaction.amount < 0: - return "withdrawal" - elif transaction.amount > 0: - return "deposit" - else: - return "N/A" - def _toDollars(number): return "${:1.2f}".format(number) diff --git a/abcbank/transaction.py b/abcbank/transaction.py index 8e5b5ad..cb6a0e6 100644 --- a/abcbank/transaction.py +++ b/abcbank/transaction.py @@ -2,6 +2,19 @@ class Transaction: - def __init__(self, amount): + def __init__(self, withdrawalOrDeposit, amount): self.amount = amount - self.transactionDate = datetime.now() \ No newline at end of file + self.type = withdrawalOrDeposit + self.transactionDate = datetime.now() + + def deposit(self, amount): + if (amount <= 0): + raise ValueError("You can only deposit an amount greater than 0.") + else: + self.transactions.append(Transaction(amount, "Deposit")) + + def withdraw(self, amount): + if (amount <= 0): + raise ValueError("You can only withdraw an amount greater than 0.") + else: + self.transactions.append(Transaction(amount, "Withdrawal")) diff --git a/tests/account_tests.py b/tests/account_tests.py new file mode 100644 index 0000000..900719d --- /dev/null +++ b/tests/account_tests.py @@ -0,0 +1,52 @@ +from nose.tools import assert_equals, nottest + +from abcbank.account import Account +from abcbank.customer import Customer +from abcbank.transaction import Transaction +from abcbank.bank import Bank + +def test_checking_account(): + bank = Bank() + checkingAccount = Account("CHECKING") + bill = Customer("Bill").openAccount(checkingAccount) + bank.addCustomer(bill) + checkingAccount.performTransaction(Transaction("Deposit", 100)) + assert_equals(checkingAccount.accountType, "CHECKING") + + +def test_savings_account(): + bank = Bank() + savingsAccount = Account("SAVINGS") + bank.addCustomer(Customer("Bill").openAccount(savingsAccount)) + savingsAccount.performTransaction(Transaction("Deposit", 1500)) + assert_equals(savingsAccount.accountType, "SAVINGS") + + +def test_maxi_savings_account(): + bank = Bank() + maxiSavingsAccount = Account("MAXI_SAVINGS") + bank.addCustomer(Customer("Bill").openAccount(maxiSavingsAccount)) + maxiSavingsAccount.performTransaction(Transaction("Deposit", 3000)) + assert_equals(maxiSavingsAccount.accountType, "MAXI_SAVINGS") + +def test_checking_account_interest(): + bank = Bank() + checkingAccount = Account("CHECKING") + bill = Customer("Bill").openAccount(checkingAccount) + bank.addCustomer(bill) + checkingAccount.performTransaction(Transaction("Deposit", 100)) + assert_equals(checkingAccount.interestEarned(), .1) + +def test_savings_account_interest(): + bank = Bank() + savingsAccount = Account("SAVINGS") + bank.addCustomer(Customer("Bill").openAccount(savingsAccount)) + savingsAccount.performTransaction(Transaction("Deposit", 1500)) + assert_equals(savingsAccount.interestEarned(), 2) + +def test_maxi_savings_account_interest(): + bank = Bank() + maxiSavingsAccount = Account("MAXI_SAVINGS") + bank.addCustomer(Customer("Bill").openAccount(maxiSavingsAccount)) + maxiSavingsAccount.performTransaction(Transaction("Deposit", 3000)) + assert_equals(maxiSavingsAccount.interestEarned(), 170) \ No newline at end of file diff --git a/tests/bank_tests.py b/tests/bank_tests.py index 6de98db..faa8310 100644 --- a/tests/bank_tests.py +++ b/tests/bank_tests.py @@ -1,38 +1,21 @@ from nose.tools import assert_equals -from account import Account, CHECKING, MAXI_SAVINGS, SAVINGS -from bank import Bank -from customer import Customer +from abcbank.account import Account +from abcbank.bank import Bank +from abcbank.customer import Customer +from abcbank.transaction import Transaction +def test_add_customer(): + bank = Bank() + john = Customer("John") + bank.addCustomer(john) + assert_equals(bank.getFirstCustomer(), "John") def test_customer_summary(): bank = Bank() - john = Customer("John").openAccount(Account(CHECKING)) + john = Customer("John").openAccount(Account("CHECKING")) bank.addCustomer(john) assert_equals(bank.customerSummary(), "Customer Summary\n - John (1 account)") -def test_checking_account(): - bank = Bank() - checkingAccount = Account(CHECKING) - bill = Customer("Bill").openAccount(checkingAccount) - bank.addCustomer(bill) - checkingAccount.deposit(100.0) - assert_equals(bank.totalInterestPaid(), 0.1) - - -def test_savings_account(): - bank = Bank() - checkingAccount = Account(SAVINGS) - bank.addCustomer(Customer("Bill").openAccount(checkingAccount)) - checkingAccount.deposit(1500.0) - assert_equals(bank.totalInterestPaid(), 2.0) - - -def test_maxi_savings_account(): - bank = Bank() - checkingAccount = Account(MAXI_SAVINGS) - bank.addCustomer(Customer("Bill").openAccount(checkingAccount)) - checkingAccount.deposit(3000.0) - assert_equals(bank.totalInterestPaid(), 170.0) \ No newline at end of file diff --git a/tests/customer_tests.py b/tests/customer_tests.py index 0211a4f..7c6a9b2 100644 --- a/tests/customer_tests.py +++ b/tests/customer_tests.py @@ -1,36 +1,35 @@ from nose.tools import assert_equals, nottest -from account import Account, CHECKING, SAVINGS -from customer import Customer - - -def test_statement(): - checkingAccount = Account(CHECKING) - savingsAccount = Account(SAVINGS) - henry = Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount) - checkingAccount.deposit(100.0) - savingsAccount.deposit(4000.0) - savingsAccount.withdraw(200.0) +from abcbank.account import Account +from abcbank.customer import Customer +from abcbank.transaction import Transaction + + +def test_getStatement(): + checkingAccount = Account("CHECKING") + savingsAccount = Account("SAVINGS") + henry = Customer("Henry") + henry.openAccount(checkingAccount) + henry.openAccount(savingsAccount) + checkingAccount.performTransaction(Transaction("Deposit",100.0)) + savingsAccount.performTransaction(Transaction("Deposit",4000.0)) + savingsAccount.performTransaction(Transaction("Withdrawal",200.0)) + print("Savings Account Balance: " + str(savingsAccount.balance)) + print(henry.getStatement()) assert_equals(henry.getStatement(), + "Statement for Henry" + - "\n\nChecking Account\n deposit $100.00\nTotal $100.00" + - "\n\nSavings Account\n deposit $4000.00\n withdrawal $200.00\nTotal $3800.00" + + "\n\nChecking Account\n Deposit $100.00\nTotal $100.00" + + "\n\nSavings Account\n Deposit $4000.00\n Withdrawal $200.00\nTotal $3800.00" + "\n\nTotal In All Accounts $3900.00") - def test_oneAccount(): - oscar = Customer("Oscar").openAccount(Account(SAVINGS)) + oscar = Customer("Oscar").openAccount(Account("SAVINGS")) assert_equals(oscar.numAccs(), 1) def test_twoAccounts(): - oscar = Customer("Oscar").openAccount(Account(SAVINGS)) - oscar.openAccount(Account(CHECKING)) + oscar = Customer("Oscar").openAccount(Account("SAVINGS")) + oscar.openAccount(Account("CHECKING")) assert_equals(oscar.numAccs(), 2) - -@nottest -def test_threeAccounts(): - oscar = Customer("Oscar").openAccount(Account(SAVINGS)) - oscar.openAccount(Account(CHECKING)) - assert_equals(oscar.numAccs(), 3) \ No newline at end of file diff --git a/tests/transaction_tests.py b/tests/transaction_tests.py index 62caa8a..ea4d870 100644 --- a/tests/transaction_tests.py +++ b/tests/transaction_tests.py @@ -1,8 +1,26 @@ -from nose.tools import assert_is_instance +from nose.tools import assert_equals -from transaction import Transaction +from abcbank.account import Account +from abcbank.customer import Customer +from abcbank.transaction import Transaction +from abcbank.bank import Bank +from abcbank.transaction import Transaction +def test_deposit(): + bank = Bank() + checkingAccount = Account("CHECKING") + bill = Customer("Bill").openAccount(checkingAccount) + bank.addCustomer(bill) + checkingAccount.performTransaction(Transaction("Deposit", 100)) + assert_equals(checkingAccount.balance, 100) -def test_type(): - t = Transaction(5) - assert_is_instance(t, Transaction, "correct type") \ No newline at end of file +def test_withdrawal(): + bank = Bank() + checkingAccount = Account("CHECKING") + bill = Customer("Bill").openAccount(checkingAccount) + bank.addCustomer(bill) + checkingAccount.performTransaction(Transaction("Deposit", 1000)) + checkingAccount.performTransaction(Transaction("Withdrawal", 900)) + assert_equals(checkingAccount.balance, 100) + + \ No newline at end of file From 39c396df8cc49c833cda67042f6d7d5223c2565f Mon Sep 17 00:00:00 2001 From: Charlye Tran Date: Fri, 18 Sep 2015 00:46:48 -0400 Subject: [PATCH 2/2] changed MAXI_SAVINGS interest rate and updated unit test --- abcbank/account.py | 15 +++++++++------ tests/account_tests.py | 2 +- tests/bank_tests.py | 13 +++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/abcbank/account.py b/abcbank/account.py index 5db73e9..f9e2115 100644 --- a/abcbank/account.py +++ b/abcbank/account.py @@ -1,3 +1,4 @@ +from datetime import datetime from transaction import Transaction class Account: @@ -22,13 +23,15 @@ def interestEarned(self): else: return 1 + (self.balance - 1000) * 0.002 if self.accountType == "MAXI_SAVINGS": - if (self.balance <= 1000): - return self.balance * 0.02 - elif (self.balance <= 2000): - return 20 + (self.balance - 1000) * 0.05 + noWithdrawalsInPast10Days = True + for t in self.transactions: + if t.type == "WITHDRAWAL" and (datetime.now - t.transactionDate).days < 10: + noWithdrawalsInPast10Days = False + if noWithdrawalsInPast10Days == True: + return self.balance * 0.05 else: - return 70 + (self.balance - 2000) * 0.1 - else: + return self.balance * 0.001 + if self.accountType == "CHECKING": return self.balance * 0.001 diff --git a/tests/account_tests.py b/tests/account_tests.py index 900719d..b367ee7 100644 --- a/tests/account_tests.py +++ b/tests/account_tests.py @@ -49,4 +49,4 @@ def test_maxi_savings_account_interest(): maxiSavingsAccount = Account("MAXI_SAVINGS") bank.addCustomer(Customer("Bill").openAccount(maxiSavingsAccount)) maxiSavingsAccount.performTransaction(Transaction("Deposit", 3000)) - assert_equals(maxiSavingsAccount.interestEarned(), 170) \ No newline at end of file + assert_equals(maxiSavingsAccount.interestEarned(), 150) \ No newline at end of file diff --git a/tests/bank_tests.py b/tests/bank_tests.py index faa8310..917ac4f 100644 --- a/tests/bank_tests.py +++ b/tests/bank_tests.py @@ -18,4 +18,17 @@ def test_customer_summary(): assert_equals(bank.customerSummary(), "Customer Summary\n - John (1 account)") +def test_total_interest_paid(): + bank = Bank() + checkingAccount = Account("CHECKING") + bill = Customer("Bill").openAccount(checkingAccount) + bank.addCustomer(bill) + checkingAccount.performTransaction(Transaction("Deposit", 1000)) + checking2Account = Account("CHECKING") + john = Customer("John").openAccount(checking2Account) + bank.addCustomer(john) + checkingAccount.performTransaction(Transaction("Deposit", 1000)) + assert_equals(bank.totalInterestPaid(), 2) + +