Budget App

JSaw
3 min readNov 21, 2021

--

Project 3 of 5 freeCodeCamp’s Scientific Computing with Python series

For the background of this series, please refer to the first article here.

For this project, we are to create a class (named Category) that would create an object that stores a budget category (e.g. food, clothing & entertainment). The class would also have methods that will store the transactions in the object, mainly deposit, withdrawal & transfer that is related to that budget category. The end result when calling the object would be :

  1. The category surrounded by “*”
  2. Transactions entered into the object using the method in the class
  3. The running total of the budget in the object

We then create a function called create_spend_chart that takes any objects that was created using earlier class and draw out the spending chart using “o”.

Create “Category” class

Basics on creating a class

We start with creating a class named Category that stores all the needed outputs, that is the category of the budget, the running transaction of the object, the running balance of the object and the running total spending so far (for our create_spend_chart function later).

Create the requested methods

The requested methods are listed in the project. One important step is to ensure we create the check_funds method for use at the other 2 methods : withdraw and transfer.

To append transactions to the ledger properties of the object we can use the append method below.

__str__ method to determine the output when the object is called

To limit the length of a string, we can create substring of the string.

To create next line we use /n.

To ensure the amounts shown have a fixed 2 decimal point, we will need to do some python string formatting. I have used :.2f.

Create “create_spend_chart” function

This function involves some pen and paper calculation of the blank spaces needed. To make it simpler, I make each column of the final chart into a string, then flip it 90 degrees and append it into another set of strings to make the chart.

To add blank spaces or any symbols in repeat in a list, I used the extend function. The article below is useful to differentiate between append and extend.

If we use int(), the default will be base 10, which means it will automatically round down.

There are many ways to join all items in list into a string, for this project, I used loops and join() function.

To pass the test, one thing I needed to do was to ensure the last row does not have /n, for which I use pop() for this.

Links to my other project articles in this Scientific Computing with Python series

--

--