Answer To: python coding
Ximi answered on Jan 31 2021
cfile_c_636845191878483184_36529_1/testmoneymanager.py
import unittest
from moneymanager import MoneyManager
class TestMoneyManager(unittest.TestCase):
def setUp(self):
# Create a test BankAccount object
self.user = MoneyManager()
# Provide it with some initial balance values
self.user.balance = 1000.0
def test_legal_deposit_works(self):
# Your code here to test that depsositing money using the account's
# 'deposit_funds' function adds the amount to the balance.
def test_illegal_deposit_raises_exception(self):
# Your code here to test that depositing an illegal value (like 'bananas'
# or such - something which is NOT a float) results in an exception being
# raised.
def test_legal_entry(self):
# Your code here to test that adding a new entry with a a legal amount subtracts the
# funds from the balance.
def test_illegal_entry_amount(self):
# Your code here to test that withdrawing an illegal amount (like 'bananas'
# or such - something which is NOT a float) raises a suitable exception.
def test_illegal_entry_type(self):
# Your code here to test that adding an illegal entry type (like 'bananas'
# or such - something which is NOT a float) raises a suitable exception.
def test_insufficient_funds_entry(self):
# Your code here to test that you can only spend funds which are available.
# For example, if you have a balance of 500.00 dollars then that is the maximum
# that can be spent. If you tried to spend 600.00 then a suitable exception
# should be raised and the withdrawal should NOT be applied to the user balance
# or the user's transaction list.
# Run the unit tests in the above test case
unittest.main()
__MACOSX/cfile_c_636845191878483184_36529_1/._testmoneymanager.py
cfile_c_636845191878483184_36529_1/moneymanager.py
class MoneyManager():
def __init__(self, username, password, balance, transaction_list):
'''Constructor to set username to '', pin_number to an empty string,
balance to 0.0, and transaction_list to an empty list.'''
self.username = username
self.password = password
self.balance = balance
self.transaction_list = transaction_list
def add_entry(self, entry_type, amount):
'''Function to add and entry an amount to the tool. Raises an
exception if it receives a value for amount that cannot be cast to float. Raises an exception
if the entry_type is not valid - i.e. not food, rent, bills, entertainment or other'''
entries = ["food", "rent", "bills", "entertainment", "other"]
if entry_type.lower() not in entries:
raise ValueError("Cannot have this entry.")
if float(self.balance)-float(amount) < 0:
raise ValueError("Amount exceeds your balance")
self.transaction_list.append((entry_type, amount))
def deposit_funds(self, amount):
'''Function to deposit an amount to the user balance. Raises an
exception if it receives a value that cannot be cast to float. '''
self.transaction_list.append(("Deposit", amount))
self.balance = str(float(self.balance)+float(amount))
def get_transaction_string(self):
'''Function to create and return a string of the transaction list. Each transaction
consists of two lines - either the word "Deposit" or the entry type - food etc - on
the first line, and then the amount deposited or entry amount on the next line.'''
string = ["\n{}\n{}".format(e, a) for e,a in self.transaction_list]
return string
def save_to_file(self):
'''Function to overwrite the user text file with the current user
details. user number, pin number, and balance (in that
precise order) are the first four lines - there are then two lines
per transaction as outlined in the above 'get_transaction_string'
function.'''
string = self.get_transaction_string()
with open(self.username+".txt", "w") as f:
...