Calculation In Java : Calculation « Velocity « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Velocity » CalculationScreenshots 
Calculation In Java


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

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/calculation.vm");

    VelocityContext ctx = new VelocityContext();

    Collection products = new ArrayList();
    products.add(new Product("Product 1"12.99));
    products.add(new Product("Product 2"13.99));
    products.add(new Product("Product 3"11.99));
    ctx.put("productList", products);

    // calculate total
    Iterator itr = products.iterator();
    double total = 0.00;

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

    ctx.put("totalPrice"new Double(total));

    Writer writer = new StringWriter();
    t.merge(ctx, writer);

    System.out.println(writer);
  }
}

-------------------------------------------------------------------------------------
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 $productList)
$product.Name    $$product.Price
#end

Total Price: $$totalPrice
           
       
velocity-CalculationInJava.zip( 876 k)
Related examples in the same category
1.Calculation Based on Model
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.