014-007-008

Template Method Pattern with final

Medium

Problem Description

Template method Pattern with final

In this problem, you will create a program that reads a report name from standard input, implements the SalesReport class extending AbstractReport, uses a final method to protect the algorithm skeleton via the Template Method Pattern, calls collectData(), processData(), and formatOutput() in sequence inside generateReport(), and displays the result to standard output.

Learning Objective: Understand Template Method Pattern using final methods

Overview

Implement the Template Method Pattern where the algorithm skeleton is protected with a final method while details are implemented in child classes.

Input

Report name (e.g. Sales, Inventory, Customer)

Specifications

  • AbstractReport class has a name field received via constructor
  • Define final void generateReport() in AbstractReport class
    • Internally calls collectData(), processData(), formatOutput() in order
    • These are defined as abstract methods
  • SalesReport class extends AbstractReport and implements each method using name
  • Main reads the report name with Scanner and tests report generation

Output Format

Collecting [name] data...
Processing [name] data...
Formatting [name] report...
Report generated!

Sample Input/Output

Input:

Sales

Output:

Collecting Sales data...
Processing Sales data...
Formatting Sales report...
Report generated!

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up