This project is part of the freeCodeCamp Python Certification.
It implements a simple budgeting system using object-oriented programming. The application allows users to create budget categories, record deposits and withdrawals, transfer funds between categories, and generate a visual spending chart.
- Create budget categories
- Record deposits with optional descriptions
- Withdraw funds with balance validation
- Transfer money between categories
- Check category balance
- Display a formatted ledger for each category
- Generate a text-based bar chart showing percentage spent per category
- Python 3
-
You should have a
Categoryclass that accepts a name as the argument. -
The
Categoryclass should have an instance attributeledgerthat is a list, and contains the list of transactions. -
The
Categoryclass should have the following methods:- A
depositmethod that accepts an amount and an optional description. If no description is given, it should default to an empty string. The method should append an object to theledgerlist in the form of{'amount': amount, 'description': description}. - A
withdrawmethod that accepts an amount and an optional description (default to an empty string). The method should store inledgerthe amount passed in as a negative number, and should returnTrueif the withdrawal succeeded andFalseotherwise. - A
get_balancemethod that returns the current category balance based onledger. - A
transfermethod that accepts an amount and anotherCategoryinstance, withdraws the amount with descriptionTransfer to [Destination], deposits it into the other category with descriptionTransfer from [Source], where[Destination]and[Source]should be replaced by the name of destination and source categories. The method should returnTruewhen the transfer is successful, andFalseotherwise. - A
check_fundsmethod that accepts an amount and returnsFalseif it exceeds the balance orTrueotherwise. This method must be used by both thewithdrawandtransfermethods.
- A
-
When a
Categoryobject is printed, it should:- Display a title line of 30 characters with the category name centered between
*characters. - List each
ledgerentry with up to 23 characters of its description left-aligned and the amount right-aligned (two decimal places, max 7 characters). - Show a final line
Total: [balance], where[balance]should be replaced by the category total.
Here is an example usage:
food = Category('Food') food.deposit(1000, 'initial deposit') food.withdraw(10.15, 'groceries') food.withdraw(15.89, 'restaurant and more food for dessert') clothing = Category('Clothing') food.transfer(50, clothing) print(food)
And 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 - Display a title line of 30 characters with the category name centered between
-
You should have a function outside the
Categoryclass namedcreate_spend_chart(categories)that returns a bar-chart string. To build the chart:- Start with the title
Percentage spent by category. - Calculate percentages from withdrawals only and not from deposits. The percentage should be the percentage of the amount spent for each category to the total spent for all categories (rounded down to the nearest 10).
- Label the y-axis from
100down to0in steps of 10. - Use
ocharacters for the bars. - Include a horizontal line two spaces past the last bar.
- Write category names vertically below the bar.
This function will be tested with up to four categories.
Make sure to match the spacing of the example output 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 - Start with the title
Example test cases are included as commented lines at the bottom of main.py.
Uncomment them to run and see the output.
- This project was developed to meet the certification requirements.
- The implementation focuses on correctness and clarity rather than extended functionality.