// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ListAfter5 {
    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        List<Asset> res = new ArrayList<Asset>();
        Connection con = DriverManager.getConnection("jdbc:sqlite:asset.db");
        Statement stmt = con.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT asset.id,asset.description,asset.assettype,truckasset.model,truckasset.license,truckasset.towcapacity FROM asset JOIN truckasset ON asset.id = truckasset.id");
        while(rs.next()) {
            int id = rs.getInt(1);
            String description = rs.getString(2);
            int assettype = rs.getInt(3);
            String model = rs.getString(4);
            String license = rs.getString(5);
            int towcapacity = rs.getInt(6);
            res.add(new TruckAsset(id, description, model, license, towcapacity));
        }
        rs.close();
        rs = stmt.executeQuery("SELECT asset.id,asset.description,asset.assettype,trailerasset.ownweight,trailerasset.maxcargo,trailerasset.roof FROM asset JOIN trailerasset ON asset.id = trailerasset.id");
        while(rs.next()) {
            int id = rs.getInt(1);
            String description = rs.getString(2);
            int assettype = rs.getInt(3);
            int ownweight = rs.getInt(4);
            int maxcargo = rs.getInt(5);
            int roof = rs.getInt(6);
            res.add(new TrailerAsset(id, description, ownweight, maxcargo, roof));
        }
        rs.close();
        rs = stmt.executeQuery("SELECT asset.id,asset.description,asset.assettype,toolasset.tooltype,toolasset.name,toolasset.weight FROM asset JOIN toolasset ON asset.id = toolasset.id");
        while(rs.next()) {
            int id = rs.getInt(1);
            String description = rs.getString(2);
            int assettype = rs.getInt(3);
            String tooltype = rs.getString(4);
            String name = rs.getString(5);
            int weight = rs.getInt(6);
            res.add(new ToolAsset(id, description, tooltype, name, weight));
        }
        rs.close();
        stmt.close();
        con.close();
        for(Asset o : res) {
            if(o instanceof TruckAsset) {
                TruckAsset o2 = (TruckAsset)o;
                System.out.printf("%d %s %s %s %d\n", o2.getId(), o2.getDescription(), o2.getModel(), o2.getLicense(), o2.getTowcapacity());
            }
            if(o instanceof TrailerAsset) {
                TrailerAsset o2 = (TrailerAsset)o;
                System.out.printf("%d %s %d %d %d\n", o2.getId(), o2.getDescription(), o2.getOwnWeight(), o2.getMaxCargo(), o2.getRoof());
            }
            if(o instanceof ToolAsset) {
                ToolAsset o2 = (ToolAsset)o;
                System.out.printf("%d %s %s %s %d\n", o2.getId(), o2.getDescription(), o2.getToolType(), o2.getName(), o2.getWeight());
            }
        }
    }
}
