9504b94630a7 — Andrew@edwin8 2 years ago
Convert trails to POJOs, use those to print out info about them.

Since I'll be combining data from multiple pages, this is a necessary step.
M src/main/java/com/ajtjp/trailaggregator/Main.java +16 -5
@@ 2,6 2,7 @@ 
 package com.ajtjp.trailaggregator;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 import java.util.function.Predicate;

          
@@ 21,9 22,9 @@ public class Main {
     public static void main(String[] args) {
         try {
             Document ohio = Jsoup.connect("https://www.traillink.com/state/oh-trails/").get();
-            Elements trails = ohio.select(".search-result-card");
+            Elements trailResults = ohio.select(".search-result-card");
             
-            Object longTrails = trails.stream().filter(new LengthFilter()).filter(new TrailConglomerationFilter()).collect(Collectors.toList());
+            Object longTrails = trailResults.stream().filter(new LengthFilter()).filter(new TrailConglomerationFilter()).collect(Collectors.toList());
             List<Element> longTrailList = (List<Element>)longTrails;
             
             longTrailList.sort(new Comparator(){

          
@@ 42,9 43,19 @@ public class Main {
                 }
             });
             
-            System.out.println("There are " + longTrailList.size() + " long trails in Ohio");
-            for (Element trail : longTrailList) {
-                System.out.println("  " + trail.selectFirst(".info h3").text() + " (" + trail.selectFirst(".length").text().split(" ")[0] + " miles)");
+            List<Trail> trails = new ArrayList<>();
+            longTrailList.iterator().forEachRemaining((elementTrail) -> {
+                String name = elementTrail.selectFirst(".info h3").text();
+                float length = Float.parseFloat(elementTrail.selectFirst(".length").text().split(" ")[0]);
+                float initialRating = elementTrail.select(".rating i").size();
+                
+                Trail trail = new Trail(name, length, initialRating);
+                trails.add(trail);
+            });
+            
+            System.out.println("There are " + trails.size() + " long trails in Ohio");
+            for (Trail trail : trails) {
+                System.out.println("  " + trail.getName() + " (" + trail.getLength() + " miles), " + trail.getRating());
             }
             
         } catch (IOException ex) {

          
A => src/main/java/com/ajtjp/trailaggregator/Trail.java +42 -0
@@ 0,0 1,42 @@ 
+
+package com.ajtjp.trailaggregator;
+
+/**
+ * Represents a Trail, and its various attributes.
+ * @author Andrew
+ */
+public class Trail {
+    private String name;
+    private float length;
+    private float rating;
+
+    public Trail(String name, float length, float rating) {
+        this.name = name;
+        this.length = length;
+        this.rating = rating;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public float getLength() {
+        return length;
+    }
+
+    public void setLength(float length) {
+        this.length = length;
+    }
+
+    public float getRating() {
+        return rating;
+    }
+
+    public void setRating(float rating) {
+        this.rating = rating;
+    }
+}