Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Calculation Example

                         

This Example shows, how to calculate using velocity. This code displays the product name and their price with the total amount of all product. 
Here in this code we have used method init() to initialize velocity, then create file template named "Product.vm". 

 

 

 

 

 

 

ProductList.java

package velocity.calculation;

import java.io.*;
import java.util.*;

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

public class ProductList {

    public static void main(String args[]) throws Exception {
        Velocity.init();

        Template template = Velocity.getTemplate("./src/velocity/calculation/Product.vm");

        VelocityContext context = new VelocityContext();

        ArrayList list = new ArrayList();
        list.add(new Product("Product 1"111.99));
        list.add(new Product("Product 2"222.89));
        list.add(new Product("Product 3"333.79));
        list.add(new Product("Product 4"444.69));

        context.put("productList", list);

        Iterator iterator = list.iterator();

        Product p = null;
        double total = 0.00;
        
        while (iterator.hasNext()) {
            p = (Productiterator.next();
            total+=p.getPrice();
        }
        context.put("totalPrice"new Double(total));

        Writer writer = new StringWriter();
        template.merge(context, writer);

        System.out.println(writer);
    }
}

Product.java

package velocity.calculation;

public class Product {

    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }
}

Product.vm

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

Total Price: $$totalPrice

Output:

Product 1    $111.99
Product 2    $222.89
Product 3    $333.79
Product 4    $444.69

Total Price: $1113.36

Download code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Join and Excel yourself with our Online instructor led training sessions
Training Courses
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.