CodeBlanks Logo Icon CodeBlanks

Project design

Design the advanced final skeleton.

Project: Event log

In this 5-lesson project you will build an event log 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 a concurrent total
  • reflection and @interface for custom marking

The model class is Event: it holds a type and a severity level. Each lesson includes the previous lesson's solution as initial code.

Project flow

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

  1. Model: the Event class and an ArrayList.
  2. Stream: filtering and groupingBy.
  3. Concurrent: total severity computed with ExecutorService.
  4. Reflection: calling a method marked with @Logged.

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 Event model and the list

Define an Event class with String type and int severity fields and a constructor. In Main, declare ArrayList<Event> log; add two events (info 1, error 5). Use a for-each loop to print each event as type + " " + severity.

Starter code

Main.java
import java.util.ArrayList;

// Define the Event class (String type, int severity and a constructor)

public class Main {
    public static void main(String[] args) {
        // Declare ArrayList<Event> log
        // Add two events (info 1, error 5)
        // Print with for-each
    }
}

Sample solution

Main.java
import java.util.ArrayList;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Event> log = new ArrayList<>();
        log.add(new Event("info", 1));
        log.add(new Event("error", 5));
        for (Event e : log) {
            System.out.println(e.type + " " + e.severity);
        }
    }
}

Expected output

info 1
error 5

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 event log project?

  • Write the reflection layer
  • Define the Event model and the list Correct answer
  • Add the concurrent layer
  • Group with Stream

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

2 Complete the Event constructor call

Question
class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

Event e = new Event("info", 1____;
Answer
class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

Event e = new Event("info", 1);

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

3 Match the project layers

Term Meaning
Model Event class
Stream Filtering and grouping
Concurrent Total via ExecutorService
Reflection Marking with @Logged

All lessons in Advanced