Skip to the content.
package com.bookshop.behavioral.templatemethod;

import com.bookshop.domain.Book;
import java.math.BigDecimal;

/**
 * CSV variant: supplies only the formatting steps, inherits the algorithm.
 *
 * <p>Deliberately simplified: real CSV output must quote fields containing commas or
 * quotes (RFC 4180) -- use a CSV library for that. The lesson here is the Template
 * Method shape, not CSV serialization.
 */
public class CsvSalesReport extends SalesReport {

    @Override
    protected String header() {
        return "title,author,price\n";
    }

    @Override
    protected String line(Book sale) {
        return sale.title() + "," + sale.author() + "," + sale.price() + "\n";
    }

    @Override
    protected String footer(BigDecimal total) {
        return "total,," + total + "\n";
    }
}

Raw file