CodeBlanks Logo Icon CodeBlanks

Project design

Design the advanced project skeleton.

Project: Ledger

In this 5-lesson project you will build a ledger application. Each lesson adds one piece; the final lesson combines them all.

You will use advanced topics together:

  • Stream for filtering and grouping
  • ExecutorService for concurrent total
  • @Test for a unit test

The model class is Transaction: it holds a category and an amount. Each lesson includes the previous lesson's solution as initial code, so the app grows step by step.

Project flow

The project has four layers. Each layer uses the previous one's solution:

  1. Model: the Transaction class and an ArrayList.
  2. Stream: filtering and groupingBy.
  3. Concurrent: a total computed with ExecutorService.
  4. Test: LedgerServiceTest with @Test.

In the final lesson all layers run together in main. The outputs are deterministic, so the expected output is the same every time.

Practice Define the model and the list

Define a Transaction class with String category and int amount fields and a constructor. In Main, declare ArrayList<Transaction> ledger; add two records (market 120, fuel 80). Use a for-each loop to print each record as category + " " + amount.

Starter code

Main.java
import java.util.ArrayList;

// Define the Transaction class (String category, int amount and a constructor)

public class Main {
    public static void main(String[] args) {
        // Declare ArrayList<Transaction> ledger
        // Add two records (market 120, fuel 80)
        // Print with for-each
    }
}

Sample solution

Main.java
import java.util.ArrayList;

class Transaction {
    String category;
    int amount;
    Transaction(String category, int amount) {
        this.category = category;
        this.amount = amount;
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Transaction> ledger = new ArrayList<>();
        ledger.add(new Transaction("market", 120));
        ledger.add(new Transaction("fuel", 80));
        for (Transaction t : ledger) {
            System.out.println(t.category + " " + t.amount);
        }
    }
}

Expected output

market 120
fuel 80

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What should be the first step of the ledger project?

  • Define the model and the base list Correct answer
  • Add the concurrent layer
  • Write the test layer
  • Group with Stream

Explanation: Start with the model and the base list; other layers build on it.

2 Complete the model constructor call

Question
class Transaction {
    String category;
    int amount;
    Transaction(String category, int amount) {
        this.category = category;
        this.amount = amount;
    }
}

Transaction t = new Transaction("market", 120____;
Answer
class Transaction {
    String category;
    int amount;
    Transaction(String category, int amount) {
        this.category = category;
        this.amount = amount;
    }
}

Transaction t = new Transaction("market", 120);

Explanation: The constructor call ends with a parenthesis and a semicolon.

3 Match the project layers

Term Meaning
Model Transaction class
Stream Filtering and grouping
Concurrent Total via ExecutorService
Test Unit test with @Test

All lessons in Advanced