Day 3: Java Console Based Expense Tracker

AyushAyush
3 min read

What I Built Today

Today, I built a console-based Expense Tracker using Java. This project uses an ArrayList and CSV file to track expenses. Users can perform the following operations:

  • Add expenses (category, amount, note, date).

  • View all expenses.

  • Show total amount.

  • Save to and load from CSV.

What I Struggled With

  1. Learning Java I/O: Working on this project made me realize that my knowledge on other fundamental concepts like Java Collection, Java Exception Handling, etc is shallow. I need to work on them and create notes or at least have some reference sheet for quick revisions.

  2. Git Commit Messages: Writing meaningful commit message is a skill in itself. I struggled with keeping it concise and following the Commit Conventions. I was hand holed yet I struggled with it. It was much better than yesterday, but definitely something I need to work this week and create my own reference sheet.

  3. Remember it All Habit: Being in college & conditioned to remember the syntax, names, ready to go formal definitions, etc still jeopardizes my learning speed. It is also taking the joy out of just Engineering and having fun.

How I Challenged Myself

  1. Using Official Documentation as a Resource: Again, I referred to the official Oracle Java Documentation. At the end, I had to rely on Abdul Bari’s Java Course on Udemy, but I realized a few things.

    1. Information Overload: Official Docs have way more info that one needs, so you need to be selective and develop a habit or system to skim through them and find what you need.

    2. Basic Layout: Getting familiar with basic layout is advantageous.

  2. Keeping a To Do: I tried to keep a to do list of what I have to do next and add checkpoints for Git commits. It helped immensely in keeping me focused and managed. Knowing the things I have to do, when to do and what to do post completion was rewarding.

The Final Output

  1. Project Structure

     expense-tracker/
     ├─ src/
     │  └─ dev/
     │     └─ ayush/
     │        └─ expenses/
     │           ├─ Expense.java
     │           ├─ ExpenseManager.java
     │           ├─ FileStore.java
     │           └─ ExpenseApp.java
     ├─ data/
     │  └─ expenses.csv  
     ├─ .gitignore
     └─ README.md
    
  1. Key Code Snippets

     public List<Expense> loadFromCsv(String path) {
             List<Expense> expenseList = new ArrayList<>();
             try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))){
                 String line;
                 while ((line = bufferedReader.readLine()) != null){
                     expenseList.add(Expense.fromCsv(line));
                 }
             }catch (IOException e){
                 e.printStackTrace();
             }
             System.out.println("Loaded " + expenseList.size() + " expenses from " + path);
             return expenseList;
     }
    
     public void saveToCsv(List<Expense> expenses, String path) {
             int i = 0;
             try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path))) {
                 for (Expense expens : expenses) {
                     String expenseInString = expens.toCsv();
                     bufferedWriter.write(expenseInString);
                     i++;
                 }
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
             System.out.println("Saved " + i + " expenses to " + path);
     }
    

What's Next

  1. Personal Reference & Resource Sheet: I plan on becoming a Backend Engineer. So I will start working on having a reference sheet for all sort of things. For instance:

    1. Dev Ops Sheet: For Dev Ops Articles & Resources Link. Like this Git Commit Convention or this excellent article on Git from The Odin Project.

    2. Core Java Notes: Following the 80 20 Rule, keep the most important topics with parts you struggle with.

  2. Fundamentals: I will have a fixed time and reference point to revisit the Fundamentals and again create my reference sheet from scratch.

0
Subscribe to my newsletter

Read articles from Ayush directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayush
Ayush