Calculation Based on Model : Calculation « Velocity « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Velocity » CalculationScreenshots 
Calculation Based on Model


import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class ProductList {

    public static void main(String[] argsthrows Exception {
        Velocity.init();
        
        Template t = Velocity.getTemplate("./src/model.vm");
        
        VelocityContext ctx = new VelocityContext();
       
        Collection products = new ArrayList();
        products.add(new Product("Product 1"142.919));
        products.add(new Product("Product 2"133.199));
        products.add(new Product("Product 3"112.991));

        ProductListModel model = new ProductListModel(products);
        ctx.put("model", model);
        
        Writer writer = new StringWriter();
        t.merge(ctx, writer);
        
        System.out.println(writer);
    }
}
-------------------------------------------------------------------------------------

import java.util.Collection;
import java.util.Iterator;

public class ProductListModel {

  private Collection productList = null;

  private double totalPrice = Double.MIN_VALUE;

  public ProductListModel(Collection aProductList) {
    productList = aProductList;
  }

  public Collection getProductList() {
    return productList;
  }

  public void setProductList(Collection aProductList) {
    productList = aProductList;
  }

  public double getTotalPrice() {
    if (totalPrice == Double.MIN_VALUE) {

      totalPrice = 0;
      Iterator itr = productList.iterator();

      while (itr.hasNext()) {
        Product p = (Productitr.next();
        totalPrice += p.getPrice();
      }
    }

    return totalPrice;
  }
}

-------------------------------------------------------------------------------------

public class Product {

    private String name;
    private double price;
    
    public Product(String aName, double aPrice) {
        name = aName;
        price = aPrice;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

-------------------------------------------------------------------------------------
#foreach($product in $model.productList)
$product.Name    $$product.Price
#end

Total Price: $$model.totalPrice

           
       
velocity-CalculationBasedonModel.zip( 876 k)
Related examples in the same category
1. Calculation In Java
ww___w_.___j_av_a2__s.__c__o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.