From 14312e441e24b8f115708af44e9e34586e055f1d Mon Sep 17 00:00:00 2001 From: Randell Dawson Date: Tue, 29 Sep 2020 10:11:26 -0700 Subject: [PATCH] initial commit --- README.md | 70 ++++++++++++++++++++++++++++++++++++ budget.py | 7 ++++ main.py | 25 +++++++++++++ test_module.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 README.md create mode 100644 budget.py create mode 100644 main.py create mode 100644 test_module.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ddbf0c --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +### Assignment + +Complete the `Category` class in `budget.py`. It should be able to instantiate objects based on different budget categories like *food*, *clothing*, and *entertainment*. When objects are created, they are passed in the name of the category. The class should have an instance variable called `ledger` that is a list. The class should also contain the following methods: + +* A `deposit` method that accepts an amount and description. If no description is given, it should default to an empty string. The method should append an object to the ledger list in the form of `{"amount": amount, "description": description}`. +* A `withdraw` method that is similar to the `deposit` method, but the amount passed in should be stored in the ledger as a negative number. If there are not enough funds, nothing should be added to the ledger. This method should return `True` if the withdrawal took place, and `False` otherwise. +* A `get_balance` method that returns the current balance of the budget category based on the deposits and withdrawals that have occurred. +* A `transfer` method that accepts an amount and another budget category as arguments. The method should add a withdrawal with the amount and the description "Transfer to [Destination Budget Category]". The method should then add a deposit to the other budget category with the amount and the description "Transfer from [Source Budget Category]". If there are not enough funds, nothing should be added to either ledgers. This method should return `True` if the transfer took place, and `False` otherwise. +* A `check_funds` method that accepts an amount as an argument. It returns `False` if the amount is less than the balance of the budget category and returns `True` otherwise. This method should be used by both the `withdraw` method and `transfer` method. + +When the budget object is printed it should display: +* A title line of 30 characters where the name of the category is centered in a line of `*` characters. +* A list of the items in the ledger. Each line should show the description and amount. The first 23 characters of the description should be displayed, then the amount. The amount should be right aligned, contain two decimal places, and display a maximum of 7 characters. +* A line displaying the category total. + +Here is an example of the output: +``` +*************Food************* +initial deposit 1000.00 +groceries -10.15 +restaurant and more foo -15.89 +Transfer to Clothing -50.00 +Total: 923.96 +``` + +Besides the `Category` class, create a function (ouside of the class) called `create_spend_chart` that takes a list of categories as an argument. It should return a string that is a bar chart. + +The chart should show the percentage spent in each category passed in to the function. The percentage spent should be calculated only with withdrawals and not with deposits. Down the left side of the chart should be labels 0 - 100. The "bars" in the bar chart should be made out of the "o" character. The height of each bar should be rounded down to the nearest 10. The horizontal line below the bars should go two spaces past the final bar. Each category name should be vertacally below the bar. There should be a title at the top that says "Percentage spent by category". + +This function will be tested with up to four categories. + +Look at the example output below very closely and make sure the spacing of the output matches the example exactly. + +``` +Percentage spent by category +100| + 90| + 80| + 70| + 60| o + 50| o + 40| o + 30| o + 20| o o + 10| o o o + 0| o o o + ---------- + F C A + o l u + o o t + d t o + h + i + n + g +``` + +The unit tests for this project are in `test_module.py`. + +### Development + +Write your code in `budget.py`. For development, you can use `main.py` to test your `Category` class. Click the "run" button and `main.py` will run. + +### Testing + +We imported the tests from `test_module.py` to `main.py` for your convenience. The tests will run automatically whenever you hit the "run" button. + +### Submitting + +Copy your project's URL and submit it to freeCodeCamp. diff --git a/budget.py b/budget.py new file mode 100644 index 0000000..933cc4e --- /dev/null +++ b/budget.py @@ -0,0 +1,7 @@ +class Category: + + + + + +def create_spend_chart(categories): \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b2a7b91 --- /dev/null +++ b/main.py @@ -0,0 +1,25 @@ +# This entrypoint file to be used in development. Start by reading README.md +import budget +from budget import create_spend_chart +from unittest import main + +food = budget.Category("Food") +food.deposit(1000, "initial deposit") +food.withdraw(10.15, "groceries") +food.withdraw(15.89, "restaurant and more food for dessert") +print(food.get_balance()) +clothing = budget.Category("Clothing") +food.transfer(50, clothing) +clothing.withdraw(25.55) +clothing.withdraw(100) +auto = budget.Category("Auto") +auto.deposit(1000, "initial deposit") +auto.withdraw(15) + +print(food) +print(clothing) + +print(create_spend_chart([food, clothing, auto])) + +# Run unit tests automatically +main(module='test_module', exit=False) \ No newline at end of file diff --git a/test_module.py b/test_module.py new file mode 100644 index 0000000..da47544 --- /dev/null +++ b/test_module.py @@ -0,0 +1,97 @@ +import unittest +import budget +from budget import create_spend_chart + + +class UnitTests(unittest.TestCase): + def setUp(self): + self.food = budget.Category("Food") + self.entertainment = budget.Category("Entertainment") + self.business = budget.Category("Business") + + def test_deposit(self): + self.food.deposit(900, "deposit") + actual = self.food.ledger[0] + expected = {"amount": 900, "description": "deposit"} + self.assertEqual(actual, expected, 'Expected `deposit` method to create a specific object in the ledger instance variable.') + + def test_deposit_no_description(self): + self.food.deposit(45.56) + actual = self.food.ledger[0] + expected = {"amount": 45.56, "description": ""} + self.assertEqual(actual, expected, 'Expected calling `deposit` method with no description to create a blank description.') + + def test_withdraw(self): + self.food.deposit(900, "deposit") + self.food.withdraw(45.67, "milk, cereal, eggs, bacon, bread") + actual = self.food.ledger[1] + expected = {"amount": -45.67, "description": "milk, cereal, eggs, bacon, bread"} + self.assertEqual(actual, expected, 'Expected `withdraw` method to create a specific object in the ledger instance variable.') + + def test_withdraw_no_description(self): + self.food.deposit(900, "deposit") + good_withdraw = self.food.withdraw(45.67) + actual = self.food.ledger[1] + expected = {"amount": -45.67, "description": ""} + self.assertEqual(actual, expected, 'Expected `withdraw` method with no description to create a blank description.') + self.assertEqual(good_withdraw, True, 'Expected `transfer` method to return `True`.') + + def test_get_balance(self): + self.food.deposit(900, "deposit") + self.food.withdraw(45.67, "milk, cereal, eggs, bacon, bread") + actual = self.food.get_balance() + expected = 854.33 + self.assertEqual(actual, expected, 'Expected balance to be 54.33') + + def test_transfer(self): + self.food.deposit(900, "deposit") + self.food.withdraw(45.67, "milk, cereal, eggs, bacon, bread") + good_transfer = self.food.transfer(20, self.entertainment) + actual = self.food.ledger[2] + expected = {"amount": -20, "description": "Transfer to Entertainment"} + self.assertEqual(actual, expected, 'Expected `transfer` method to create a specific ledger item in food object.') + self.assertEqual(good_transfer, True, 'Expected `transfer` method to return `True`.') + actual = self.entertainment.ledger[0] + expected = {"amount": 20, "description": "Transfer from Food"} + self.assertEqual(actual, expected, 'Expected `transfer` method to create a specific ledger item in entertainment object.') + + def test_check_funds(self): + self.food.deposit(10, "deposit") + actual = self.food.check_funds(20) + expected = False + self.assertEqual(actual, expected, 'Expected `check_funds` method to be False') + actual = self.food.check_funds(10) + expected = True + self.assertEqual(actual, expected, 'Expected `check_funds` method to be True') + + def test_withdraw_no_funds(self): + self.food.deposit(100, "deposit") + good_withdraw = self.food.withdraw(100.10) + self.assertEqual(good_withdraw, False, 'Expected `withdraw` method to return `False`.') + + def test_transfer_no_funds(self): + self.food.deposit(100, "deposit") + good_transfer = self.food.transfer(200, self.entertainment) + self.assertEqual(good_transfer, False, 'Expected `transfer` method to return `False`.') + + def test_to_string(self): + self.food.deposit(900, "deposit") + self.food.withdraw(45.67, "milk, cereal, eggs, bacon, bread") + self.food.transfer(20, self.entertainment) + actual = str(self.food) + expected = f"*************Food*************\ndeposit 900.00\nmilk, cereal, eggs, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33" + self.assertEqual(actual, expected, 'Expected different string representation of object.') + + def test_create_spend_chart(self): + self.food.deposit(900, "deposit") + self.entertainment.deposit(900, "deposit") + self.business.deposit(900, "deposit") + self.food.withdraw(105.55) + self.entertainment.withdraw(33.40) + self.business.withdraw(10.99) + actual = create_spend_chart([self.business, self.food, self.entertainment]) + expected = "Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t " + self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.') + +if __name__ == "__main__": + unittest.main() \ No newline at end of file