codice:
import java.util.*;
/**
*
* FlavioRocchiTest
* You can find follows objects :
* - two interfaces (StoredBean & StoredValue).
* - two EntityBean which inherated StoreBean.
* - two Objects (Football & Weather) which inherated StoredValue.
* - a Comparer object which implemented Comparator.
* - main object called FlavioRocchiTest
*
* I could do the same software in a lot of different ways for example :
* I could store infomation regarding weather and football
* in two different xml files;reading & sorting them through DOM.
*
*
* @instruction link : http://www.apoapsis.com/careers/exercise.html
* @author : Flavio Rocchi <flaviorocchi@gmail.com>
* @date : 18.05.2006
* @version : 1.0
*/
class FlavioRocchiTest {
public static void main(String[] args)
{
ArrayList temp = new ArrayList();
// Weather.
Weather weather = new Weather();
temp = weather.getStoredValue();
System.out.println("");
System.out.println("DAY : MAX : MIN ::: DIFF " );
System.out.println("");
System.out.println("--------- Stored Order ----------");
weather.printOut(temp);
System.out.println("--------- Difference in temperature ----------");
weather.printOut(sort(temp));
// Football.
Football football = new Football();
temp = football.getStoredValue();
System.out.println("");
System.out.println("ID : TEAM NAME : PLAYED : WON : DREW : LOST : FOR : AGAINST : DIFF (FOR-AGAINST) " );
System.out.println("");
System.out.println("--------- Stored Order ----------");
football.printOut(temp);
System.out.println("--------- Difference in goals ----------");
football.printOut(sort(temp));
}
public static ArrayList sort(ArrayList temp){
Collections.sort(temp, new Comparer());
return temp;
}
}
// Objects Weather and Football.
class Weather extends StoredValue
{
public ArrayList getStoredValue(){
/*
|Day|Max Temp|Min Temp|
|1|28|19| : |2|22|17| : |3|19|15| : |4|25|22| : |5|21|16|
|6|22|17| : |7|18|13| : |8|26|21| : |9|23|19| : |10|22|18|
|11|18|15| : |12|20|17| : |13|20|16| : |14|25|19| : |15|22|17|
|16|24|20| : |17|23|18| : |18|23|20| : |19|21|19| : |20|20|15|
|21|30|25| : |22|27|21| : |23|29|22| : |24|27|23| : |25|22|17|
|26|25|21| : |27|21|16| : |28|18|15| : |29|16|14| : |30|23|19|
*/
ArrayList tempStore = new ArrayList();
tempStore.add(new WeatherBean(1, 28, 19));
tempStore.add(new WeatherBean(2, 22, 17));
tempStore.add(new WeatherBean(3, 19, 15));
tempStore.add(new WeatherBean(4, 25, 22));
tempStore.add(new WeatherBean(5, 21, 16));
tempStore.add(new WeatherBean(6, 22, 17));
tempStore.add(new WeatherBean(7, 18, 13));
tempStore.add(new WeatherBean(8, 26, 21));
tempStore.add(new WeatherBean(9, 23, 19));
tempStore.add(new WeatherBean(10, 22, 18));//10
tempStore.add(new WeatherBean(11, 18, 15));
tempStore.add(new WeatherBean(12, 20, 17));
tempStore.add(new WeatherBean(13, 20, 16));
tempStore.add(new WeatherBean(14, 25, 19));
tempStore.add(new WeatherBean(15, 22, 17));
tempStore.add(new WeatherBean(16, 24, 20));
tempStore.add(new WeatherBean(17, 23, 18));
tempStore.add(new WeatherBean(18, 23, 20));
tempStore.add(new WeatherBean(19, 21, 19));
tempStore.add(new WeatherBean(20, 20, 15));//20
tempStore.add(new WeatherBean(21, 30, 25));
tempStore.add(new WeatherBean(22, 27, 21));
tempStore.add(new WeatherBean(23, 29, 22));
tempStore.add(new WeatherBean(24, 27, 23));
tempStore.add(new WeatherBean(25, 22, 17));
tempStore.add(new WeatherBean(26, 25, 21));
tempStore.add(new WeatherBean(27, 21, 16));
tempStore.add(new WeatherBean(28, 18, 15));
tempStore.add(new WeatherBean(29, 16, 14));
tempStore.add(new WeatherBean(30, 23, 19));//30
return tempStore;
}
public void printOut(ArrayList temp){
try{
for(int it=0;it<temp.size(); it++){
WeatherBean t = (WeatherBean) temp.get(it);
if(t.getIndex()<10)
System.out.println("0"+t.getDay() + " : " + t.getMax() + " : " + t.getMin() + " ::: " +t.getDifference());
else
System.out.println(t.getDay() + " : " + t.getMax() + " : " + t.getMin() + " ::: " +t.getDifference());
}
}catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
}
};
class Football extends StoredValue
{
public ArrayList getStoredValue(){
/*
|No|Team|Played|Won|Drew|Lost|For|Against|
|1|Chelsea|33|16|1|0|41|9| : |2|Man Utd|33|12|3|1|33|8| : |3|Liverpool|34|14|3|1|29|7|
|4|Tottenham|33|11|5|1|29|14| : |5|Blackburn|33|11|3|2|28|16| : |6|Arsenal|32|12|2|2|40|9|
|7|Bolton|32|9|4|2|23|9| : |8|Wigan|33|6|3|8|20|22| : |9|West Ham|33|7|3|6|26|22|
|10|Newcastle|33|8|5|3|21|14| : |11|Everton|33|8|2|6|20|19| : |12|Charlton|33|7|4|6|20|18|
|13|Man City|33|9|2|6|24|15| : |14|Middlesbrough|32|6|5|6|26|29| : |15|Aston Villa|33|4|6|6|15|17|
|16|Fulham|33|10|2|4|27|20| : |17|Birmingham|33|5|4|8|17|19| : |18|West Brom|33|6|1|10|21|23|
|19|Portsmouth|32|3|6|6|12|19| : |20|Sunderland|32|0|4|12|9|29|
*/
ArrayList teamStore = new ArrayList();
teamStore.add(new FootballBean(1, "Chelsea", 33, 16, 1, 0, 41, 9));
teamStore.add(new FootballBean(2, "Man Utd", 33, 12, 3, 1, 33, 8));
teamStore.add(new FootballBean(3, "Liverpool", 34, 14, 3, 1, 29, 7));
teamStore.add(new FootballBean(4, "Tottenham", 33, 11, 5, 1, 29, 14));
teamStore.add(new FootballBean(5, "Blackburn", 33, 11, 3, 2, 28, 16));
teamStore.add(new FootballBean(6, "Arsenal", 32, 12, 2, 2, 40, 9));
teamStore.add(new FootballBean(7, "Bolton", 32, 9, 4, 2, 23, 9));
teamStore.add(new FootballBean(8, "Wingan", 32, 6, 3, 8, 20, 22));
teamStore.add(new FootballBean(9, "West Ham", 33, 7, 3, 6, 26, 22));
teamStore.add(new FootballBean(10, "Newcastle", 33, 8, 5, 3, 21, 14));//10
teamStore.add(new FootballBean(11, "Everton", 33,8,2,6,20,19));
teamStore.add(new FootballBean(12,"Charlton",33,7,4,6,20,18));
teamStore.add(new FootballBean(13,"Man City",33,9,2,6,24,15));
teamStore.add(new FootballBean(14,"Middlesbrough",32,6,5,6,26,29));
teamStore.add(new FootballBean(15,"Aston Villa",33,4,6,6,15,17));
teamStore.add(new FootballBean(16,"Fulham",33,10,2,4,27,20));
teamStore.add(new FootballBean(17,"Birmingham",33,5,4,8,17,19));
teamStore.add(new FootballBean(18,"West Brom",33,6,1,10,21,23));
teamStore.add(new FootballBean(19,"Portsmouth",32,3,6,6,12,19));
teamStore.add(new FootballBean(20,"Sunderland",32,0,4,12,9,29));//20
return teamStore;
}
public void printOut(ArrayList temp){
try{
for(int it=0;it<temp.size(); it++){
FootballBean t = (FootballBean) temp.get(it);
if(t.getIndex()<10)
System.out.println("0"+t.getIndex() + " : " + t.getTeam() + " : " + t.getMatches() + " : " + t.getWon() + " : " + t.getDrew() + " : " + t.getLost() + " : " + t.getScoredFor() + " : " + t.getScoredAgainst() + " : " +t.getDifference());
else
System.out.println(t.getIndex() + " : " + t.getTeam() + " : " + t.getMatches() + " : " + t.getWon() + " : " + t.getDrew() + " : " + t.getLost() + " : " + t.getScoredFor() + " : " + t.getScoredAgainst() + " : " +t.getDifference());
}
}catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
}
};
/**
* Implemented Comparator to sort my StoreBean objects from smallest to largest difference.
*/
class Comparer implements Comparator {
public int compare(Object obj1, Object obj2) {
try{
int x = ((StoredBean)obj1).getDifference();
int y = ((StoredBean)obj2).getDifference();
if ( (x-y) > 0 ) return 1;
if ( (x-y) < 0 ) return -1;
else return 0;
}catch(ClassCastException e){
System.out.println("Exception : " + e.getMessage());
}
return 0;
}
}
// Entity Beans.
/**
* I use this entity bean to store temperature item.
*/
class WeatherBean extends StoredBean
{
WeatherBean(int day, int max, int min){
super(day, max, min);
}
public int getDay(){return getIndex();}
};
/**
* I use this entity bean to store team item.
*/
class FootballBean extends StoredBean
{
FootballBean(int index, String team, int matches, int won, int drew, int lost, int scoredFor, int scoredAgainst){
super(index, scoredFor, scoredAgainst);
this.team = team;
this.matches = matches;
this.won = won;
this.drew = drew;
this.lost = lost;
}
public String getTeam(){return team;}
public int getMatches(){return matches;}
public int getWon(){return won;}
public int getDrew(){return drew;}
public int getLost(){return lost;}
public int getScoredFor(){return super.getMax();}
public int getScoredAgainst(){return super.getMin();}
private int matches = 0, won = 0, drew = 0, lost = 0, scoredFor = 0, scoredAgainst = 0;
private String team = null;
};
// Abstract Classes.
/**
* I use this abstract class to make common methods of Weather & Football Objects.
*/
abstract class StoredValue{
public abstract ArrayList getStoredValue();
public abstract void printOut(ArrayList list);
};
/**
* I use this abstact class to make common methods of WeatherBean & FotballBean.
*/
abstract class StoredBean
{
StoredBean(int index, int max, int min){
this.index = index;
this.max = max;
this.min = min;
}
public int getDifference(){return max-min;}
public int getIndex(){return index;}
public int getMax(){return max;}
public int getMin(){return min;}
private int index = 0, difference = 0, max = 0, min = 0;
};