
// MakeTable.java
// Andrew Davison, ad@coe.psu.ac.th, July 2023

/* Generate an HTML page (called ProdsTable.html) containing 
   an HTML table of products read from a list of Product objects.

  Usage:
    > compile MakeTable.java
    > run MakeTable
*/

import java.io.*;
import java.util.*;

import org.apache.velocity.app.*;
import org.apache.velocity.*;


public class MakeTable
{
  private static final String TEMPLATE_FNM = "prodsTable.vm";

  public static void main(String args[]) throws Exception
  {
    Velocity.init();
    System.out.println("Loading template: " + TEMPLATE_FNM);
    Template template = Velocity.getTemplate(TEMPLATE_FNM);
     
    VelocityContext vc = new VelocityContext();
     
    // build a products list
    vc.put("header", "Gadgets Table");
    List<Product> prods =  Arrays.asList(
      new Product("Dell Laptop", 31000.00),
      new Product("Samsung Mobile", 16000.00),
      new Product("Samsung Tablet", 15000.00),
      new Product("Sony Camera", 23000.00));
    vc.put("products", prods);

    String fnm = "ProdsTable.html";
    System.out.println("\nGenerating " + fnm);
    Writer w = new FileWriter(new File(fnm));
    template.merge(vc, w);
    w.flush();
    w.close();
  } /* main */

}  // end of MakeTable class



