From a349a043480eb6008f8925c524ee95810fd1f178 Mon Sep 17 00:00:00 2001 From: Ailin Ghoreishi Date: Thu, 25 Apr 2024 15:06:43 +0330 Subject: [PATCH] done --- src/main/java/Country.java | 29 ++++++----- src/main/java/Parser.java | 98 +++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 27 deletions(-) diff --git a/src/main/java/Country.java b/src/main/java/Country.java index 9741748..ed5d1a8 100644 --- a/src/main/java/Country.java +++ b/src/main/java/Country.java @@ -1,36 +1,43 @@ import java.util.Objects; public class Country { - private String name; - private String capital; - private int population; - private double area; + private static String name; + private static String capital; + private static int population; + private static double area; public Country(String name, String capital, int population, double area) { - //TODO + this.name = name; + this.capital = capital; + this.population = population; + this.area = area; } - public String getName() { + public static String getName() { return name; } - public String getCapital() { + public static String getCapital() { return capital; } - public int getPopulation() { + public static int getPopulation() { return population; } - public double getArea() { + public static double getArea() { return area; } @Override public String toString() { - //TODO - return ""; + String string = ""; + string = string + this.name; + string = string + " " + this.capital; + string = string + " " + this.population; + string = string + this.area; + return string; } @Override diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 64fb022..70a5b0c 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -8,41 +8,107 @@ public class Parser { static List countries = new ArrayList<>(); + static List htmlElementsList = new ArrayList<>(); - public List sortByName(){ + public static List sortByName(){ List sortedByName = new ArrayList<>(countries); - // Sort countries alphabetically (least) - //TODO + Collections.sort(countries, Comparator.comparingDouble(country -> (double) Country.getArea()).reversed()); + // Sort countries by population (most) + for(Country theCountry: countries){ + System.out.println(theCountry); + } return sortedByName; } - public List sortByPopulation(){ + public static List sortByPopulation(){ List sortedByPopulation = new ArrayList<>(countries); + Collections.sort(countries, Comparator.comparingDouble(country -> (double) Country.getArea()).reversed()); // Sort countries by population (most) - //TODO + for(Country theCountry: countries){ + System.out.println(theCountry); + } + return sortedByPopulation; } - public List sortByArea(){ + public static List sortByArea(){ List sortedByArea = new ArrayList<>(countries); - // Sort countries by area (most) - //TODO + Collections.sort(countries, Comparator.comparingDouble(country -> (double) Country.getArea()).reversed()); + // Sort countries by population (most) + for(Country theCountry: countries){ + System.out.println(theCountry); + } + return sortedByArea; } - public void setUp() throws IOException { +// public static void areaSorting(){ +// Collections.sort(htmlElementsList, Comparator.comparingInt(country -> (int) Country.getArea()).reversed()); +// for(Elements element:htmlElementsList){ +// System.out.print("Country: " + Country.getName() + " "); +// System.out.print("Capital: " + Country.getCapital() + " "); +// System.out.print("Population: " + Country.getPopulation() + " "); +// System.out.print("Area (km2): " + Country.getArea()); +// System.out.println(); +// } +// } + + public static void setUp() { - //Parse the HTML file using Jsoup - //TODO + try { + //Parse the HTML file using Jsoup + Document doc = Jsoup.parse(new File("src/Resources/country-list.html"),"utf-8"); - // Extract data from the HTML - //TODO + // Extract data from the HTML + Elements countryDivs = doc.select(".country"); - // Iterate through each country div to extract country data - //TODO + // Iterate through each country div to extract country data + for (Element countryDiv : countryDivs) { + // getting attributes + String name = countryDiv.select(".country-name").text(); + String capital = countryDiv.select(".country-capital").text(); + int population = Integer.parseInt(countryDiv.select(".country-population").text()); + double area = Double.parseDouble(countryDiv.select(".country-area").text()); + //creating instance + countries.add(new Country(name, capital, population, area)); + } + } + catch (IOException e) { + e.printStackTrace(); + } } - public static void main(String[] args) { + public static void main(String[] args) throws IOException { //you can test your code here before you run the unit tests ;) + Country.getName(); + Country.getArea(); + Country.getPopulation(); + Country.getCapital(); + setUp(); + System.out.println("Hello. please choose what do you wanna see"); + System.out.println("1: see countries sorted by Area."); + System.out.println("2: see countries sorted by Population."); + System.out.println("3: see countries sorted by Name."); + + Scanner scanner = new Scanner(System.in); + int num = scanner.nextInt(); + + switch(num){ + case 1: + sortByArea(); + break; + + case 2: + sortByPopulation(); + break; + + case 3: + sortByName(); + break; + + default: + break; + } + } }