9 Ağustos 2011 Salı

F1Racing JSF App (part 1: create database)




Let me guess, your brother-in-law is a F1 supporter and he wanted you develop a small F1Racing application that gives basic info about drivers and teams.
You visualised your app GUI and now start to design database.

As you can see, we have 3 simple tables. Lets create using sql codes:

create table drivers
(
id int not null auto_increment,
primary key(id),
driver varchar(50) not null,
point float

);
create table constructors
(
id int not null auto_increment,
primary key(id),
team varchar(50)
)

create table constructor_driver
(
id int not null auto_increment,
primary key(id),
constructor int,
driver int,
foreign key(constructor) references constructors(id),
foreign key(driver) references drivers(id)
);



now insert some data into tables.

insert into drivers (driver, point) values ('Kimi Raikkonen',50);
insert into constructors (team) values ('Ferrari');


Finally we have our tables as shown:



8 Ağustos 2011 Pazartesi

Develop a Simple JSF App (Population Calculator)





Let's say we want to develp a very simple web-based JSF app that is for calculation countries' population ratios.
Let's open a new Web Application on our favorite IDE Netbeans. I just named it as "BolgeOylamalari" in Turkish.
At beginning we should have look to our requirements and visualize GUI in our brain. We should be able to input populations to inputboxes and system calculates nd whenever result is ready they must be shown. As you can see on picture, I designed a very user-friendly one.
NOW, let the hack begin....

What we really need is to develop a ManagedBean and manipulate entities in that bean.


Here you can see our program hierarchy. Our Entity Class is Population.java, managed bean is BolgecontrollerBean.

And here we have how does it works :

Additionally here is the working url on local machine:



Finally we have source codes:



------------------------
BolgecontrollerBean.java
------------------------

@ManagedBean(name="bolgecontrollerBean")
@Dependent
public class BolgecontrollerBean {

private List populationList = new ArrayList ();
private boolean calculationFinished;

/** Creates a new instance of BolgecontrollerBean */
public BolgecontrollerBean() {

populationList.add(new Population("Turkey"));
populationList.add(new Population("Iraq"));
populationList.add(new Population("Kurdistan"));
populationList.add(new Population("Great Britain"));
populationList.add(new Population("USA"));
populationList.add(new Population("Norway"));
populationList.add(new Population("India"));
populationList.add(new Population("Israel"));
populationList.add(new Population("Pakistan"));
populationList.add(new Population("Japan"));
}

public boolean isCalculationFinished() {
return calculationFinished;
}

public void setCalculationFinished(boolean calculationFinished) {
this.calculationFinished = calculationFinished;
}

public List getPopulationList() {
return populationList;
}

public void setListe(List liste) {
this.populationList = liste;
}

public void cleanForm(){
for(Population p : populationList){
p.setPopulationCount(0);
p.setPopulationRatio(0);
}
}
public void calculateRatios(){
//let's first calculate the total population
double total = 0;
for(Population p : populationList){
total += p.getPopulationCount();
}

for(Population p : populationList){
p.setPopulationRatio(p.getPopulationCount() / total * 100);
}
setCalculationFinished(true);
}

}

---------------------
Population.java
---------------------


public class Population {

private String country;
private double populationCount;
private double populationRatio;

public Population() {
}
}

Finally you can download full source codes here. Have fun guys ! :):)