mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): Rewrote budget app description (#61737)
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
dbdb6944cd
commit
91608a165d
@@ -7,76 +7,88 @@ dashedName: build-a-budget-app
|
||||
|
||||
# --description--
|
||||
|
||||
Complete the `Category` class. 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:
|
||||
In this lab, you will build a simple budget app that tracks spending in different categories and can show the relative spending percentage on a graph.
|
||||
|
||||
- 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 greater than the balance of the budget category and returns `True` otherwise. This method should be used by both the `withdraw` method and `transfer` method.
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
When the budget object is printed it should display:
|
||||
**User Stories:**
|
||||
|
||||
- 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.
|
||||
1. You should have a `Category` class that accepts a name as the argument.
|
||||
1. The `Category` class should have an instance attribute `ledger` that is a list, and contains the list of transactions.
|
||||
1. The `Category` class should have the following methods:
|
||||
|
||||
Here is an example usage:
|
||||
- A `deposit` method 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 the `ledger` list in the form of `{'amount': amount, 'description': description}`.
|
||||
- A `withdraw` method that accepts an amount and an optional description (default to an empty string). The method should store in `ledger` the amount passed in as a negative number, and should return `True` if the withdrawal succeeded and `False` otherwise.
|
||||
- A `get_balance` method that returns the current category balance based on `ledger`.
|
||||
- A `transfer` method that accepts an amount and another `Category` instance, withdraws the amount with description `Transfer to [Destination]`, deposits it into the other category with description `Transfer from [Source]`, where `[Destination]` and `[Source]` should be replaced by the name of destination and source categories. The method should return `True` when the transfer is successful, and `False` otherwise.
|
||||
- A `check_funds` method that accepts an amount and returns `False` if it exceeds the balance or `True` otherwise. This method must be used by both the `withdraw` and `transfer` methods.
|
||||
|
||||
```py
|
||||
food = Category('Food')
|
||||
food.deposit(1000, '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)
|
||||
```
|
||||
1. When a `Category` object is printed, it should:
|
||||
- Display a title line of 30 characters with the category name centered between `*` characters.
|
||||
- List each `ledger` entry 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:
|
||||
|
||||
```py
|
||||
food = Category('Food')
|
||||
food.deposit(1000, '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:
|
||||
And here is an example of the output:
|
||||
|
||||
```bash
|
||||
*************Food*************
|
||||
initial deposit 1000.00
|
||||
groceries -10.15
|
||||
restaurant and more foo -15.89
|
||||
Transfer to Clothing -50.00
|
||||
Total: 923.96
|
||||
```
|
||||
```bash
|
||||
*************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 (outside 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.
|
||||
1. You should have a function outside the `Category` class named `create_spend_chart(categories)` that returns a bar-chart string. To build the 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, and it should be the percentage of the amount spent for each category to the total spent for all categories. 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 written vertically below the bar. There should be a title at the top that says 'Percentage spent by category'.
|
||||
- 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 to the nearest 10).
|
||||
- Label the y-axis from `100` down to `0` in steps of 10.
|
||||
- Use `o` characters 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.
|
||||
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.
|
||||
Make sure to match the spacing of the example output exactly:
|
||||
|
||||
```bash
|
||||
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
|
||||
```
|
||||
```bash
|
||||
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
|
||||
```
|
||||
|
||||
Note: open the browser console with F12 to see a more verbose output of the tests.
|
||||
NOTE: open the browser console with F12 to see a more verbose output of the tests.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
Reference in New Issue
Block a user