Struts を利用した Web アプリケーションの練習問題(解答編・その 1)

みなさん、書初めはしましたか ? プログラマ書初めと言えばソースコードですね。それでは昨年末に出した問題の解答を少しずつ公開していきますよ。

SQL

INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (1,'札幌市','北海道','1972-04-01',1121.12,1921245,1);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (2,'仙台市','宮城県','1989-04-01',785.85,1049493,2);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (3,'さいたま市','埼玉県','2003-04-01',217.49,1229479,3);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (4,'千葉市','千葉県','1992-04-01',272.08,963120,3);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (5,'横浜市','神奈川県','1956-09-01',437.38,3691693,3);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (6,'川崎市','神奈川県','1972-04-01',142.7,1430773,3);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (7,'相模原市','神奈川県','2010-04-01',328.83,719412,3);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (8,'新潟市','新潟県','2007-04-01',726.1,812458,4);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (9,'静岡市','静岡県','2005-04-01',1411.85,714513,4);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (10,'浜松市','静岡県','2007-04-01',1558.04,798924,4);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (11,'名古屋市','愛知県','1956-09-01',326.43,2266517,4);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (12,'京都市','京都府','1956-09-01',827.9,1473416,5);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (13,'大阪市','大阪府','1956-09-01',223.0,2670579,5);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (14,'堺市','大阪府','2006-04-01',149.99,842685,5);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (15,'神戸市','兵庫県','1956-09-01',552.26,1544496,5);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (16,'岡山市','岡山県','2009-04-01',789.92,710913,6);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (17,'広島市','広島県','1980-04-01',905.41,1177711,6);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (18,'北九州市','福岡県','1963-04-01',488.78,974287,8);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (19,'福岡市','福岡県','1972-04-01',341.7,1479433,8);
INSERT INTO major_city ( city_id, city_name, pref_name, designated_day, area, population, dst_id ) VALUES (20,'熊本市','熊本県','2012-04-01',389.54,736010,8);

使用するデータは 2012 年 4 月 1 日現在のものを使用すること、と指定しましたので上記データが正しいものになります。

Model 編

City.java

package jp.mydns.akanekodou.model;

import java.util.Date;

public class City {
    private int cityId;
    private String cityName;
    private String prefName;
    private Date designatedDay;
    private double area;
    private int population;
    private String dstName;

    public City() { }

    public int getCityId() {
        return cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public String getPrefName() {
        return prefName;
    }

    public Date getDesignatedDay() {
        return designatedDay;
    }

    public double getArea() {
        return area;
    }

    public int getPopulation() {
        return population;
    }

    public String getDstName() {
        return dstName;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public void setPrefName(String prefName) {
        this.prefName = prefName;
    }

    public void setDesignatedDay(Date designatedDay) {
        this.designatedDay = designatedDay;
    }

    public void setArea(double area) {
        this.area = area;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public void setDstName(String dstName) {
        this.dstName = dstName;
    }
}

CityList.java

package jp.mydns.akanekodou.model;

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

public class CityList {
    private List<City> list;

    public CityList() {
        list = new ArrayList<City>();
    }

    public List<City> getList() {
        return list;
    }

    public void setList(List<City> list) {
        this.list = list;
    }

    public void add(City city) {
        list.add(city);
    }
}

次回は DAO 編と View 編の予定。