Tuesday, April 28, 2009

Housing Price Simulation using Java Swing

School
? Catchment area of school defines the area of houses influenced by the presence of the school.
? Catchment area for schools is 3km.
? The degree to which a school is considered good or bad is its influence level.
? Influence level is randomly assigned to each school in neighbourhood.
? Influence level: -10 being the worst (negatively influenced), 10 being the best (positively influenced).
? Houses within the 3km proximity radius will show an increase in price of 15%
? However “bad” schools will have no negative effect on house price.

Industrial Estates
? Catchment area of industrial estates defines the area of houses influence by its presence.
? Catchment area for industrial estates is 0.5km
? Industrial estate agents will group together to from an “industrial estate.”
? Industrial estates will have an influence level of -6.

Topography
? Topography defined as scenic areas, countryside, lakes, parks.
? Catchment area of topography defines the area of houses influence by its presence.
? Catchment area for topography 0.5km.
? Influence level for topography range from +5 to +10


Pubs
? Catchment area for pubs ranges from 0.5km to 4km
? Houses within 0.5km of the pub will negatively influence house prices by 8%
? Houses within 0.5km of pubs will carry the negative influence level of
? Houses outside 0.5km of the pub will positively influence house prices by 3%
? Houses outside 0.5km of pubs will carry the positive influence level of
? Influence level for topography range from +5 to +10

Town Centre
? Catchment area for town centre is 1.5km
? Houses prices within this catchment area will be positively influenced by 20%


Interest rates
? When interest rates increase, demand for housing decreases, therefore decrease in price.
? Interest rate ranges from 0 to 15.
? Demand for housing will be measured as a percentage change of the interest rate.
? E.g. if interest rate increases from 4.0 to 4.6 (change being +0.6) then demand decreases by 15% as does house price.

Policing Level
? If level of policing is 0-30% reduce all agents influence level by 5.
? If level of policing is 30-60% reduce all agents influence level by 3.
? If level of policing is 60-100% reduce all agents influence level by 1.




Inputs
Before the simulation is run the user will be able to set up the neighbourhood.

Interest rates
? Interest rates will range from 0 to 15.
? User will have the option to set default value for simulation (5)

Population
? Users will also be able to set the population density for the neighbourhood.
? Population density will range from 0 to 100%
? And will directly effect demand e.g. when population is at 100%, demand is at its highest (therefore price will be equally as high).
? User will have the option to set default value for simulation (75%)
?
Level of policing

General Inputs
? Users will be able to set default values for everything to allow the system run a controlled simulation.
? Users can set the catchment area and influence level for each amenity
? All houses start off with a default price of £100,000


Outputs
The outputs will be shown on a GUI that will help analyse the simulation as a whole.
Each agent will have its own colour, for example houses will be in blue, topography in green and so on.
To represent the difference in house price within the actual simulation, differeing house prices will be represented by different shades of blue. For example houses prices between 0 and 50,000 will show as a light blue, 50,000 to 100,000 a darker shade, 100,000 to 150,000 an even darker shade, until it reaches its maximum category 300,000+.


? Display all input values including interest rate, population
? The number of houses in the simulation
? The number of houses in each price category
? The average house price will be calculated
? The highest and lowest house price will be displayed
? Display the number of each amenity-school pubs topography etc


Source code:
------------

HouseMarketGUI.java
-------------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Graphics;
import java.awt.Color;
public class HouseMarketGUI extends JFrame implements ActionListener{

int population ;
int interestRate ;
int policingLevel ;

double topographyCatchment;
double industrialsCatchment;
double towncentreCatchment;
double pubsCatchment;
double schoolCatchment;

static HouseMarketGUI gui;
//Page1 GUI Variables
private JLabel p1labelt;
private JLabel p1labeltSub;
private JFrame p1frame;
private JLabel p1popLable;
private JLabel p1intLable;
private JLabel p1poliLable;
private JTextField p1popValue;
private JTextField p1intValue;
private JTextField p1poliValue;

private JButton p1button;
//end
// Page2 GUI Variables
private JLabel p2labelt;
private JLabel p2labeltSub;
private JFrame p2frame;
private JLabel p2nameLable;
private JLabel p2numberLable;
private JLabel p2salaryLable;
private JLabel p2pubLable;
private JLabel p2schoolLable;

private JLabel p2topdefLable;
private JLabel p2inddefLable;
private JLabel p2towndefLable;
private JLabel p2pubdefLable;
private JLabel p2schooldefLable;

private JTextField p2numberValue;
private JTextField p2namevalue;
private JTextField p2salaryValue;
private JTextField p2pubValue;
private JTextField p2schoolValue;

private JButton p2topdefValue;
private JButton p2inddefValue;
private JButton p2towndefValue;
private JButton p2pubdefValue;
private JButton p2schooldefValue;


private JButton p2button;
private JButton p2button2;
// end
// Page3 GUI Variables
private JLabel p3labelt;
private JLabel p3labeltSub;
private JLabel p3labeltSubs;
private JLabel p3labeltrSub;
private JLabel p3labeltrSubs;

static JFrame p3frame;

private JLabel p3housesLable;
private JLabel p3topLable;
private JLabel p3indLable;
private JLabel p3townLable;
private JLabel p3pubLable;
private JLabel p3schoolLable;


private JLabel p3to100Lable;
private JLabel p3to250Lable;
private JLabel p3to500Lable;
private JLabel p3to1000Lable;
private JLabel p3to1001Lable;
private JLabel p3AvgPriceLable;

public static JTextField p3housesValue;
public static JTextField p3topValue;
public static JTextField p3indValue;
public static JTextField p3townValue;
public static JTextField p3pubValue;
public static JTextField p3schoolValue;


private JTextField p3to100Value;
private JTextField p3to250Value;
private JTextField p3to500Value;
private JTextField p3to1000Value;
private JTextField p3to1001Value;
private JTextField p3AvgPriceValue;
private JTextField p3XYearSimValue;


private JLabel p3popLable;
private JLabel p3intLable;
private JLabel p3poliLable;
private JTextField p3popValue;
private JTextField p3intValue;
private JTextField p3poliValue;


private JButton p3button;
private JButton p3button2;
private JButton p3button3;
//end


HousePriceFactors housePriceFactor;




public HouseMarketGUI(){

housePriceFactor = new HousePriceFactors();

// size = new Dimension(0, 0);
// stats = new FieldStats();
// JPanel cp = new JPanel(new GridLayout(1, 1));
// JPanel cp1 = new JPanel();
//Page1 Create Object
p1frame= new JFrame(" Housing Market Simulation ");
p1frame.getContentPane().setLayout(null);

p1labelt = new JLabel("Housing Market Simulation");
p1labelt.setFont(new Font("Serif", Font.BOLD, 20));
p1labelt.setBounds(150,0,350,70);

p1labeltSub = new JLabel("Setup Environment");
p1labeltSub.setFont(new Font("Serif", Font.BOLD, 18));
p1labeltSub.setBounds(50,40,350,70);

p1popLable = new JLabel("Population(0-100)%");
p1popValue = new JTextField(35);
p1popValue.setText("75");
p1intLable = new JLabel("Interest Rate(0-15)");
p1intValue = new JTextField(35);
p1intValue.setText("5");

p1poliLable = new JLabel("Policing Level(0-100)%");
p1poliValue = new JTextField(35);
p1poliValue.setText("50");

p1button = new JButton("NEXT");
//end

// Page2 Create Object
p2frame= new JFrame(" Housing Market Simulation ");
p2frame.getContentPane().setLayout(null);

p2labelt = new JLabel("Housing Market Simulation");
p2labelt.setFont(new Font("Serif", Font.BOLD, 20));
p2labelt.setBounds(150,0,350,70);

p2labeltSub = new JLabel("Setup Neighborhood");
p2labeltSub.setFont(new Font("Serif", Font.BOLD, 18));
p2labeltSub.setBounds(50,40,350,70);

p2nameLable = new JLabel("Topography CatchmentArea(km)");
p2namevalue = new JTextField(35);

p2numberLable = new JLabel("Industrial CatchmentArea(km)");
p2numberValue = new JTextField(35);

p2salaryLable = new JLabel("Town Centre CatchmentArea(km)");
p2salaryValue = new JTextField(35);

p2pubLable = new JLabel("Pubs & Bars CatchmentArea(km)");
p2pubValue = new JTextField(35);

p2schoolLable = new JLabel("Schools CatchmentArea(km)");
p2schoolValue = new JTextField(35);

//default
p2topdefLable = new JLabel("Default(0.5km)");
p2topdefValue = new JButton(" X ");

p2inddefLable = new JLabel("Default(0.5km)");
p2inddefValue = new JButton(" X ");

p2towndefLable = new JLabel("Default(1.5km)");
p2towndefValue = new JButton(" X ");

p2pubdefLable = new JLabel("Default(0.5km)");
p2pubdefValue = new JButton(" X ");

p2schooldefLable = new JLabel("Default(3.0km)");
p2schooldefValue = new JButton(" X ");

p2button = new JButton("BACK");
p2button2 = new JButton("Simulate");
//end

// Page3 Create Object
p3frame= new JFrame(" Housing Market Simulation ");
//p3frame.getContentPane().setLayout(null);

p3labelt = new JLabel("Housing Market Simulation");
p3labelt.setFont(new Font("Serif", Font.BOLD, 20));
p3labelt.setBounds(150,0,350,70);

p3labeltSub = new JLabel("Statistics");
p3labeltSub.setFont(new Font("Serif", Font.BOLD, 19));
p3labeltSub.setBounds(50,40,350,60);

p3labeltSubs = new JLabel("Legend");
p3labeltSubs.setFont(new Font("Serif", Font.BOLD, 14));
p3labeltSubs.setBounds(50,80,250,30);

p3labeltrSub = new JLabel("House Prices");
p3labeltrSub.setFont(new Font("Serif", Font.BOLD, 19));
p3labeltrSub.setBounds(600,40,250,30);

p3labeltrSubs = new JLabel("$(000)");
p3labeltrSubs.setFont(new Font("Serif", Font.BOLD, 14));
p3labeltrSubs.setBounds(600,80,250,30);



p3housesLable = new JLabel("Houses");
p3housesValue = new JTextField(35);

p3topLable = new JLabel("Topography");
p3topValue = new JTextField(35);

p3indLable = new JLabel("Industrial");
p3indValue = new JTextField(35);

p3townLable = new JLabel("Town Centre");
p3townValue = new JTextField(35);

p3pubLable = new JLabel("Pubs");
p3pubValue = new JTextField(35);

p3schoolLable = new JLabel("School");
p3schoolValue = new JTextField(35);

//default
p3to100Lable = new JLabel("0-100");
p3to100Value = new JTextField(35);

p3to250Lable = new JLabel("100-250");
p3to250Value = new JTextField(35);

p3to500Lable = new JLabel("250-500");
p3to500Value = new JTextField(35);

p3to1000Lable = new JLabel("500-1,000");
p3to1000Value = new JTextField(35);

p3to1001Lable = new JLabel("1,000 +");
p3to1001Value = new JTextField(35);

p3AvgPriceLable = new JLabel("Average price");
p3AvgPriceValue = new JTextField(35);

p3XYearSimValue = new JTextField(20);

p3popLable = new JLabel("Population(0-100)%");
p3popValue = new JTextField(35);
p3intLable = new JLabel("Interest Rate(0-15)");
p3intValue = new JTextField(35);
p3poliLable = new JLabel("Policing Level(0-100)%");
p3poliValue = new JTextField(35);


p3button = new JButton("Simulate 1 Year");
p3button2 = new JButton("Simulate X Year");
p3button3 = new JButton("Close");
//end

//Page1 Position
p1popLable.setBounds(50,100,150,70);
p1intLable.setBounds(50,150,150,70);

p1popValue.setBounds(220,120,50,30);
p1intValue.setBounds(220,170,50,30);

p1poliLable.setBounds(50,200,150,70);
p1poliValue.setBounds(220,220,50,30);

p1button.setBounds(370,330,80,27);
//end
//Page2 Position
p2nameLable.setBounds(50,100,220,70);
p2namevalue.setBounds(250,120,50,30);

p2numberLable.setBounds(50,150,220,70);
p2numberValue.setBounds(250,170,50,30);

p2salaryLable.setBounds(50,200,220,70);
p2salaryValue.setBounds(250,220,50,30);

p2pubLable.setBounds(50,250,220,70);
p2pubValue.setBounds(250,270,50,30);

p2schoolLable.setBounds(50,300,220,70);
p2schoolValue.setBounds(250,320,50,30);

//default
p2topdefLable.setBounds(350,100,150,70);
p2topdefValue.setBounds(450,120,50,30);

p2inddefLable.setBounds(350,150,150,70);
p2inddefValue.setBounds(450,170,50,30);

p2towndefLable.setBounds(350,200,150,70);
p2towndefValue.setBounds(450,220,50,30);

p2pubdefLable.setBounds(350,250,150,70);
p2pubdefValue.setBounds(450,270,50,30);

p2schooldefLable.setBounds(350,300,150,70);
p2schooldefValue.setBounds(450,320,50,30);

p2button.setBounds(30,400,80,27);
p2button2.setBounds(370,400,100,27);
//end
//Page3 Position
p3housesLable.setBounds(50,100,80,70);
p3housesValue.setBounds(140,120,50,30);

p3topLable.setBounds(50,150,80,70);
p3topValue.setBounds(140,170,50,30);

p3indLable.setBounds(50,200,80,70);
p3indValue.setBounds(140,220,50,30);

p3townLable.setBounds(50,250,80,70);
p3townValue.setBounds(140,270,50,30);

p3pubLable.setBounds(50,300,80,70);
p3pubValue.setBounds(140,320,50,30);

p3schoolLable.setBounds(50,350,80,70);
p3schoolValue.setBounds(140,370,50,30);

//default
p3to100Lable.setBounds(600,100,150,70);
p3to100Value.setBounds(660,120,90,30);
p3to100Value.setEditable(false);

p3to250Lable.setBounds(600,150,150,70);
p3to250Value.setBounds(660,170,90,30);
p3to250Value.setEditable(false);

p3to500Lable.setBounds(600,200,150,70);
p3to500Value.setBounds(660,220,90,30);
p3to500Value.setEditable(false);

p3to1000Lable.setBounds(600,250,150,70);
p3to1000Value.setBounds(660,270,90,30);
p3to1000Value.setEditable(false);

p3to1001Lable.setBounds(600,300,150,70);
p3to1001Value.setBounds(660,320,90,30);
p3to1001Value.setEditable(false);

p3AvgPriceLable.setBounds(560,350,170,70);
p3AvgPriceValue.setBounds(650,370,90,30);
p3AvgPriceValue.setEditable(false);

p3XYearSimValue.setBounds(460,520,30,30);

p3popLable.setBounds(50,450,150,70);
p3popValue.setBounds(220,470,50,30);

p3intLable.setBounds(50,500,150,70);
p3intValue.setBounds(220,520,50,30);

p3poliLable.setBounds(50,550,150,70);
p3poliValue.setBounds(220,570,50,30);

//button.setBounds(80,450,150,27);
p3button.setBounds(520,480,140,27);
p3button2.setBounds(520,520,140,27);
p3button3.setBounds(520,560,140,27);
//end

//page1 add contentPanel
p1frame.getContentPane().add(p1labelt);
p1frame.getContentPane().add(p1labeltSub);

p1frame.getContentPane().add(p1popLable);
p1frame.getContentPane().add(p1popValue);
p1frame.getContentPane().add(p1intLable);
p1frame.getContentPane().add(p1intValue);

p1frame.getContentPane().add(p1poliLable);
p1frame.getContentPane().add(p1poliValue);

p1frame.getContentPane().add(p1button);
//end

//page2 add contentPanel
p2frame.getContentPane().add(p2labelt);
p2frame.getContentPane().add(p2labeltSub);

p2frame.getContentPane().add(p2nameLable);
p2frame.getContentPane().add(p2namevalue);

p2frame.getContentPane().add(p2numberLable);
p2frame.getContentPane().add(p2numberValue);

p2frame.getContentPane().add(p2salaryLable);
p2frame.getContentPane().add(p2salaryValue);

p2frame.getContentPane().add(p2pubLable);
p2frame.getContentPane().add(p2pubValue);

p2frame.getContentPane().add(p2schoolLable);
p2frame.getContentPane().add(p2schoolValue);

//default
p2frame.getContentPane().add(p2topdefLable);
p2frame.getContentPane().add(p2topdefValue);

p2frame.getContentPane().add(p2inddefLable);
p2frame.getContentPane().add(p2inddefValue);

p2frame.getContentPane().add(p2towndefLable);
p2frame.getContentPane().add(p2towndefValue);

p2frame.getContentPane().add(p2pubdefLable);
p2frame.getContentPane().add(p2pubdefValue);

p2frame.getContentPane().add(p2schooldefLable);
p2frame.getContentPane().add(p2schooldefValue);

p2frame.getContentPane().add(p2button);
p2frame.getContentPane().add(p2button2);
//end
//page3 add contentPanel
p3frame.getContentPane().add(p3labelt);
p3frame.getContentPane().add(p3labeltSub);
p3frame.getContentPane().add(p3labeltSubs);
p3frame.getContentPane().add(p3labeltrSub);
p3frame.getContentPane().add(p3labeltrSubs);

p3frame.getContentPane().add(p3housesLable);
p3frame.getContentPane().add(p3housesValue);

p3frame.getContentPane().add(p3topLable);
p3frame.getContentPane().add(p3topValue);

p3frame.getContentPane().add(p3indLable);
p3frame.getContentPane().add(p3indValue);

p3frame.getContentPane().add(p3townLable);
p3frame.getContentPane().add(p3townValue);

p3frame.getContentPane().add(p3pubLable);
p3frame.getContentPane().add(p3pubValue);

p3frame.getContentPane().add(p3schoolLable);
p3frame.getContentPane().add(p3schoolValue);

//default
p3frame.getContentPane().add(p3to100Lable);
p3frame.getContentPane().add(p3to100Value);

p3frame.getContentPane().add(p3to250Lable);
p3frame.getContentPane().add(p3to250Value);

p3frame.add(p3to500Lable);
p3frame.add(p3to500Value);

p3frame.getContentPane().add(p3to1000Lable);
p3frame.getContentPane().add(p3to1000Value);

p3frame.getContentPane().add(p3to1001Lable);
p3frame.getContentPane().add(p3to1001Value);

p3frame.getContentPane().add(p3AvgPriceLable);
p3frame.getContentPane().add(p3AvgPriceValue);
p3frame.getContentPane().add(p3XYearSimValue);

p3frame.getContentPane().add(p3popLable);
p3frame.getContentPane().add(p3popValue);
p3frame.getContentPane().add(p3intLable);
p3frame.getContentPane().add(p3intValue);
p3frame.getContentPane().add(p3poliLable);
p3frame.getContentPane().add(p3poliValue);





p3frame.getContentPane().add(p3button);
p3frame.getContentPane().add(p3button2);
p3frame.getContentPane().add(p3button3);
//end

//page1 actionListener
p1button.addActionListener(this);
p1frame.setSize(550,550);
p1frame.setVisible(true);
//end

//page2 actionListener
p2button.addActionListener(this);
p2button2.addActionListener(this);
p2topdefValue.addActionListener(this);
p2inddefValue.addActionListener(this);
p2towndefValue.addActionListener(this);
p2pubdefValue.addActionListener(this);
p2schooldefValue.addActionListener(this);

// X button actions implementation
// X button actions implementation
// X button actions implementation
// X button actions implementation
// X button actions implementation

p2frame.setSize(550,550);
p2frame.setVisible(false);
//end

Rectangles rects = new Rectangles();
p3frame.getContentPane().add(rects);

//page3 actionListener
p3button.addActionListener(this);
p3button2.addActionListener(this);
p3button3.addActionListener(this);
p3frame.setSize(780,650);
p3frame.setVisible(false);
//end



}

public void actionPerformed(ActionEvent event){

if(event.getSource() == p1button){
p1frame.setVisible(false);
p2frame.setVisible(true);
p3frame.setVisible(false);

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2namevalue.setEditable(false);
p2numberValue.setEditable(false);
p2salaryValue.setEditable(false);
p2pubValue.setEditable(false);
p2schoolValue.setEditable(false);

population = Integer.parseInt(p1popValue.getText());
interestRate = Integer.parseInt(p1intValue.getText());
policingLevel = Integer.parseInt(p1poliValue.getText());

housePriceFactor.setPopulation(population);
housePriceFactor.setInterestRate(interestRate);
housePriceFactor.setPolicingLevel(policingLevel);



}

if(event.getSource() == p2topdefValue){

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2namevalue.setEditable(true);

}

if(event.getSource() == p2inddefValue){

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2numberValue.setEditable(true);


}

if(event.getSource() == p2towndefValue){

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2salaryValue.setEditable(true);


}
if(event.getSource() == p2pubdefValue){

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2pubValue.setEditable(true);


}
if(event.getSource() == p2schooldefValue){

p2namevalue.setText("0.5");
p2numberValue.setText("0.5");
p2salaryValue.setText("1.5");
p2pubValue.setText("0.5");
p2schoolValue.setText("3.0");

p2schoolValue.setEditable(true);

}
if(event.getSource() == p2button){
p1frame.setVisible(true);
p2frame.setVisible(false);
p3frame.setVisible(false);

}




if(event.getSource() == p2button2){
p1frame.setVisible(false);
p2frame.setVisible(false);
p3frame.setVisible(true);

p3popValue.setText(population+"");
p3intValue.setText(interestRate+"");
p3poliValue.setText(policingLevel+"");
p3popValue.setEditable(false);
p3intValue.setEditable(false);
p3poliValue.setEditable(false);

topographyCatchment = Double.parseDouble(p2namevalue.getText());
industrialsCatchment = Double.parseDouble(p2numberValue.getText());
towncentreCatchment = Double.parseDouble(p2salaryValue.getText());
pubsCatchment = Double.parseDouble(p2pubValue.getText());
schoolCatchment = Double.parseDouble(p2schoolValue.getText());

housePriceFactor.setTopographyCatchmentArea(topographyCatchment);
housePriceFactor.setIndustrialsCatchmentArea(industrialsCatchment);
housePriceFactor.setTowncentreCatchmentArea(towncentreCatchment);
housePriceFactor.setPubsCatchmentArea(pubsCatchment);
housePriceFactor.setSchoolCatchmentArea(schoolCatchment);

Simulator s = new Simulator(50,50,gui);
}

if(event.getSource() == p3button){

SimulateHousePrice simulateHousePrice = new SimulateHousePrice();
long housePrice = simulateHousePrice.housePrice(housePriceFactor);
long housePri = (long)(housePrice );

p3to100Value.setText((housePri*100)+"");
p3to250Value.setText((housePri*250)+"");
p3to500Value.setText((housePri*500)+"");
p3to1000Value.setText((housePri*1000)+"");
p3to1001Value.setText((housePri*1500)+"");

p3AvgPriceValue.setText(housePri+"");
}

if(event.getSource() == p3button2){

SimulateHousePrice simulateHousePrice = new SimulateHousePrice();
long housePrice = simulateHousePrice.housePrice(housePriceFactor);
int yesrs = Integer.parseInt(p3XYearSimValue.getText());

long housePri = (long)((housePrice) * yesrs);

p3to100Value.setText((housePri*100)+"");
p3to250Value.setText((housePri*250)+"");
p3to500Value.setText((housePri*500)+"");
p3to1000Value.setText((housePri*1000)+"");
p3to1001Value.setText((housePri*1500)+"");

p3AvgPriceValue.setText(housePri+"");
}

if(event.getSource() == p3button3){

System.exit(0);
}


}



public static void main(String arg[]){


gui = new HouseMarketGUI();

}

}

OUTPUTS











Read more...

Thursday, April 16, 2009

Adding logging in Java Applications using LOG4J

Step1: Create Log4J Properties File





# this logger is for application logging

log4j.rootLogger=INFO, STDOUT



# *****************************************************************

# Appender A1 - Rolling File

# *****************************************************************

# Define the actual Appender to use that is associated with our string; in this

# case we select the RollingFileAppender which writes to a file

log4j.appender.STDOUT=org.apache.log4j.RollingFileAppender


# Set properties for the RollingFileAppender

log4j.appender.STDOUT.File=.../log/Application.log


# Set the layout for the Appender

log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout

log4j.appender.STDOUT.layout.ConversionPattern=%d %-5p -[%c] %m%n


# Set max size (25 MB each file) and backup index (save 4000 files) that's 100GB!

log4j.appender.STDOUT.MaxFileSize=15MB

log4j.appender.STDOUT.MaxBackupIndex=10





Step2: PropertyConfigurator



This line in your startup java class(Main method).


PropertyConfigurator.configure(".../log/Application.log");



Step3: Declar a static variable



Declar a static variable for the class will use the log


private static final Logger LOG = Logger.getLogger(Sample.class);




Step4: write a log in your program



LOG.debug("");


or


LOG.error("");


or


LOG.warn("");


or

LOG.info("");




Step5: import Jar file for LOG4J



log4j-1.2.14.jar






LOG4J example in Java Application:



1.log4j.prperties (Use above Properties File in step1)



2.Sample.java



import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;

public class Sample

{

private static final Logger LOG = Logger.getLogger(Sample.class);


public void display(){

LOG.info("Display method");

}


public static void main(String[] args)

{

PropertyConfigurator.configureAndWatch(".../src/log4j.properties",5000);

System.out.println("Hello World!");

Sample sample = new Sample();

sample.display();

}

}


3.Download jar file (log4j-1.2.14.jar)then add it.



4.Run your application.

Read more...

Saturday, April 11, 2009

SpellChecker using Java

SpellChecker.java




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

public class SpellChecker {


public static void main(String[] args) throws Exception {

String read = null;
String filename1 = "dictionary.txt";
FileReader fr1 = new FileReader(filename1);//read the file

BufferedReader br1 = new BufferedReader(fr1);
ArrayList al = new ArrayList();
while((read = br1.readLine()) != null){//count the line number and print the error message
al.add(read.trim());
}


String filename2 = "file.txt";
FileReader fr2 = new FileReader(filename2);//read the file

BufferedReader br2 = new BufferedReader(fr2);
ArrayList result = new ArrayList();
int line = 1;
while((read = br2.readLine()) != null){//count the line number and print the error message

StringTokenizer st = new StringTokenizer(read," ");

while(st.hasMoreTokens()){
String str = st.nextToken();
str = str.replace('(',' ');
str = str.replace(')',' ');
str = str.replace(',',' ');
str = str.replace('.',' ');
str = str.replace('/',' ');
str = str.trim();
Iterator it = al.iterator();
boolean flag = false;
while(it.hasNext()){
String dictStr = (String) it.next();

if(str.equals(dictStr)){
flag = true;
}
}

if(!flag){
result.add(str+" "+line);
}
}

line++;
}


Collections.sort(result);
Iterator it1 = result.iterator();

while(it1.hasNext()){
System.out.println(" "+it1.next());
}
}
}

dictionary.txt


enter your words one by one
example:

java
j2ee
solution

file.txt



enter your paragraph

Read more...

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP