Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

4. Java classes

4.1. Overview

The report will display stock data. To demonstrate BIRT we use a Mock object for providing the data. The appendix demonstrates how to get real stock data via a Yahoo service.

4.2. Domain Model

Create package "de.vogella.birt.stocks.model" and then the following class. This class will represent the domain model.

			
package de.vogella.birt.stocks.model;

import java.util.Date;

/**
 * Domain model for stock data
 * @author Lars Vogel
 */

public class StockData {
	private Date date;
	private double open;
	private double high;
	private double low;
	private double close;
	private long volume;

	public double getClose() {
		return close;
	}

	public void setClose(double close) {
		this.close = close;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public double getHigh() {
		return high;
	}

	public void setHigh(double high) {
		this.high = high;
	}

	public double getLow() {
		return low;
	}

	public void setLow(double low) {
		this.low = low;
	}

	public double getOpen() {
		return open;
	}

	public void setOpen(double open) {
		this.open = open;
	}

	public long getVolume() {
		return volume;
	}

	public void setVolume(long volume) {
		this.volume = volume;
	}

}
		

4.3. Data Access Object - Mock

Create the package "de.vogella.birt.stocks.daomock" and then the following class. This will only mock the data, if you want to use real data have a look at Java Networking

			
package de.vogella.birt.stocks.daomock;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import de.vogella.birt.stocks.model.StockData;

public class StockDaoMock {

	public List<StockData> getStockValues(String company) {
		// Ignore the company and always return the data
		// A real implementation would of course use the company string
		List<StockData> history = new ArrayList<StockData>();
		// We fake the values, we will return fake value for 01.01.2009 -
		// 31.01.2009
		double begin = 2.5;
		for (int i = 1; i <= 31; i++) {
			Calendar day = Calendar.getInstance();
			day.set(Calendar.HOUR, 0);
			day.set(Calendar.MINUTE, 0);
			day.set(Calendar.SECOND, 0);
			day.set(Calendar.MILLISECOND, 0);
			day.set(Calendar.YEAR, 2009);
			day.set(Calendar.MONTH, 0);
			day.set(Calendar.DAY_OF_MONTH, i);
			StockData data = new StockData();
			data.setOpen(begin);
			double close = Math.round(begin + Math.random() * begin * 0.1);
			data.setClose(close);
			data.setLow(Math.round(Math.min(begin, begin - Math.random() * begin * 0.1)));
			data.setHigh(Math.round(Math.max(begin, close) + Math.random() * 2));
			data.setVolume(1000 + (int) (Math.random() * 500));
			begin = close;
			data.setDate(day.getTime());
			history.add(data);
		}
		return history;
	}
}