From 7ea4cd61e72ab397495dd26046e04f0eda1b5f8d Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Wed, 24 Apr 2024 09:13:03 +0330 Subject: [PATCH 1/5] working ob setUp method --- src/main/java/Parser.java | 43 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 64fb022..dfeceab 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -4,22 +4,27 @@ import org.jsoup.select.Elements; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.*; +import static org.jsoup.nodes.Document.OutputSettings.Syntax.html; + public class Parser { static List countries = new ArrayList<>(); - public List sortByName(){ + public List sortByName() throws Exception{ List sortedByName = new ArrayList<>(countries); // Sort countries alphabetically (least) //TODO - return sortedByName; + Collections.sort(sortedByName , Comparator.comparing(Country::getName)); + return sortedByName; } public List sortByPopulation(){ List sortedByPopulation = new ArrayList<>(countries); // Sort countries by population (most) //TODO + Collections.sort(sortedByPopulation , Comparator.comparing(Country::getPopulation).reversed()); return sortedByPopulation; } @@ -27,6 +32,7 @@ public List sortByArea(){ List sortedByArea = new ArrayList<>(countries); // Sort countries by area (most) //TODO + Collections.sort(sortedByArea , Comparator.comparing(Country::getArea).reversed()); return sortedByArea; } @@ -34,15 +40,46 @@ public void setUp() throws IOException { //Parse the HTML file using Jsoup //TODO + File file = new File("src\\Resources\\country-list.html"); + Document doc = Jsoup.parse(file , null); // Extract data from the HTML //TODO - + Element country = doc.select("section#countries").first(); + Elements divs = country.select("div.col-md-4.country"); // Iterate through each country div to extract country data //TODO +// for (Element div : divs) { +// System.out.println(div.text()); +// } + for(Element div : divs) { + String name = div.select("h3.country-name").text(); + String capital = div.select("span.country-capital").text(); + int population = Integer.parseInt(div.select("span.country-population").text()); + double area = Double.parseDouble(div.select("span.country-area").text()); + Country country1 = new Country(name , capital , population , area); + countries.add(country1); + } } public static void main(String[] args) { + try { + Parser obj = new Parser(); + obj.setUp(); + System.out.println("hello"); + System.out.println(countries.size()); + for (int i = 0 ; i < countries.size() ; i++) { + System.out.println(countries.get(i)); + } + for (int i = 0 ; i < obj.sortByName().size() ; i++) { + System.out.println(obj.sortByName().get(i)); + } + System.out.println("hello"); + } catch (Exception e) { + System.out.println("----------------"); + e.getStackTrace(); + System.out.println("----------------"); + } //you can test your code here before you run the unit tests ;) } } From c90cae8d88cf52de7d9c19019e3e71830c938ab7 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Wed, 24 Apr 2024 09:13:35 +0330 Subject: [PATCH 2/5] extracting the html file --- src/main/java/Country.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Country.java b/src/main/java/Country.java index 9741748..b4b6490 100644 --- a/src/main/java/Country.java +++ b/src/main/java/Country.java @@ -6,7 +6,7 @@ public class Country { private int population; private double area; - public Country(String name, String capital, int population, double area) { + public Country(String name, String capital, int population, double area) { //constructor //TODO } From bf1b339f881e6f97849d1065fbb8a1849233947e Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Wed, 24 Apr 2024 09:19:17 +0330 Subject: [PATCH 3/5] trying to extract the country-name and the country-capital and the country-population and the country-area from the html file --- src/main/java/Parser.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index dfeceab..f25563a 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -52,11 +52,11 @@ public void setUp() throws IOException { // for (Element div : divs) { // System.out.println(div.text()); // } - for(Element div : divs) { - String name = div.select("h3.country-name").text(); - String capital = div.select("span.country-capital").text(); - int population = Integer.parseInt(div.select("span.country-population").text()); - double area = Double.parseDouble(div.select("span.country-area").text()); + for (Element div : divs) { + String name = div.select("country-name").text(); + String capital = div.select("country-capital").text(); + int population = Integer.parseInt(div.select("country-population").text()); + double area = Double.parseDouble(div.select("country-area").text()); Country country1 = new Country(name , capital , population , area); countries.add(country1); } From 0732c20ebffe2d1db13e317fe29ce04b09b5c7a7 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Thu, 25 Apr 2024 19:59:25 +0330 Subject: [PATCH 4/5] i added menu option for interface between with user --- src/main/java/Parser.java | 62 +++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index f25563a..0e8257a 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -3,7 +3,10 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; +import java.lang.reflect.WildcardType; import java.nio.file.Files; import java.util.*; @@ -53,28 +56,61 @@ public void setUp() throws IOException { // System.out.println(div.text()); // } for (Element div : divs) { - String name = div.select("country-name").text(); - String capital = div.select("country-capital").text(); - int population = Integer.parseInt(div.select("country-population").text()); - double area = Double.parseDouble(div.select("country-area").text()); + String name = div.select(".country-name").text(); + String capital = div.select(".country-capital").text(); + int population = Integer.parseInt(div.select(".country-population").text()); + double area = Double.parseDouble(div.select(".country-area").text()); Country country1 = new Country(name , capital , population , area); countries.add(country1); } } + public void menu() throws Exception{ + for (int i = 0 ; i < countries.size() ; i++ ){ + System.out.println(i + "- Country: " + countries.get(i).getName() + ", capital: " + countries.get(i).getCapital() + ", population: " + countries.get(i).getPopulation() + ", area: " + countries.get(i).getArea()); + } + int stop = 1; + while (stop != 0) { + System.out.println("Please Enter The number Of What you Want : "); + System.out.println("1.Sorted By Population\n2.Sorted By Area\n3.Sorted By Name\n4.Back"); + Scanner input = new Scanner(System.in); + int userInput = input.nextInt(); + while (userInput > 4 || userInput < 1) { + System.out.println("Invalid Data !!"); + System.out.print("Try Again : "); + userInput = input.nextInt(); + } + switch (userInput) { + case 1 : + Parser parser = new Parser(); + for (int i = 0 ; i < parser.sortByPopulation().size() ; i++) { + System.out.println(i + "- Country: " + parser.sortByPopulation().get(i).getName() + ", capital: " + parser.sortByPopulation().get(i).getCapital() + ", population: " + parser.sortByPopulation().get(i).getPopulation() + ", area: " + parser.sortByPopulation().get(i).getArea()); + } + + break; + case 2 : + Parser parser1 = new Parser(); + for (int i = 0 ; i < parser1.sortByArea().size() ; i++) { + System.out.println(i + "- Country: " + parser1.sortByArea().get(i).getName() + ", capital: " + parser1.sortByArea().get(i).getCapital() + ", population: " + parser1.sortByArea().get(i).getPopulation() + ", area: " + parser1.sortByArea().get(i).getArea()); + } + break; + case 3 : + Parser parser2 = new Parser(); + for (int i = 0 ; i < parser2.sortByName().size() ; i++) { + System.out.println(i + "- Country: " + parser2.sortByName().get(i).getName() + ", capital: " + parser2.sortByName().get(i).getCapital() + ", population: " + parser2.sortByName().get(i).getPopulation() + ", area: " + parser2.sortByName().get(i).getArea()); + } + break; + case 4 : + stop = 0; + break; + } + } + } public static void main(String[] args) { try { Parser obj = new Parser(); obj.setUp(); - System.out.println("hello"); - System.out.println(countries.size()); - for (int i = 0 ; i < countries.size() ; i++) { - System.out.println(countries.get(i)); - } - for (int i = 0 ; i < obj.sortByName().size() ; i++) { - System.out.println(obj.sortByName().get(i)); - } - System.out.println("hello"); + obj.menu(); } catch (Exception e) { System.out.println("----------------"); e.getStackTrace(); From 278d0e4fa1c4dac08958847f44d70fae58bf5bea Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Thu, 25 Apr 2024 20:00:47 +0330 Subject: [PATCH 5/5] I added a new optional feature each time the user want a special list store this file for the user --- src/main/java/Parser.java | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 0e8257a..e5885e9 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -85,19 +85,69 @@ public void menu() throws Exception{ for (int i = 0 ; i < parser.sortByPopulation().size() ; i++) { System.out.println(i + "- Country: " + parser.sortByPopulation().get(i).getName() + ", capital: " + parser.sortByPopulation().get(i).getCapital() + ", population: " + parser.sortByPopulation().get(i).getPopulation() + ", area: " + parser.sortByPopulation().get(i).getArea()); } - + File file = new File("sorted-by-population.txt"); + try { + if(file.createNewFile()) { + System.out.println("A new file with this name : " + file.getName() + " created for you :)"); + } + else { + System.out.println("the file is exist : " + file.getName()); + } + FileWriter Writer = new FileWriter("sorted-by-population.txt"); + for (int i = 0 ; i < sortByPopulation().size() ; i++) { + Writer.write((i + "- Country: " + parser.sortByPopulation().get(i).getName() + ", capital: " + parser.sortByPopulation().get(i).getCapital() + ", population: " + parser.sortByPopulation().get(i).getPopulation() + ", area: " + parser.sortByPopulation().get(i).getArea()).toString() + '\n'); + } + Writer.close(); + }catch (Exception e) { + System.out.println("Error"); + e.printStackTrace(); + } break; case 2 : Parser parser1 = new Parser(); for (int i = 0 ; i < parser1.sortByArea().size() ; i++) { System.out.println(i + "- Country: " + parser1.sortByArea().get(i).getName() + ", capital: " + parser1.sortByArea().get(i).getCapital() + ", population: " + parser1.sortByArea().get(i).getPopulation() + ", area: " + parser1.sortByArea().get(i).getArea()); } + File file1 = new File("sorted-by-area.txt"); + try { + if(file1.createNewFile()) { + System.out.println("A new file with this name : " + file1.getName() + " created for you :)"); + } + else { + System.out.println("the file is exist : " + file1.getName()); + } + FileWriter Writer = new FileWriter("sorted-by-area.txt"); + for (int i = 0 ; i < sortByArea().size() ; i++) { + Writer.write((i + "- Country: " + parser1.sortByArea().get(i).getName() + ", capital: " + parser1.sortByArea().get(i).getCapital() + ", population: " + parser1.sortByArea().get(i).getPopulation() + ", area: " + parser1.sortByArea().get(i).getArea()).toString() + '\n'); + } + Writer.close(); + }catch (Exception e) { + System.out.println("Error"); + e.printStackTrace(); + } break; case 3 : Parser parser2 = new Parser(); for (int i = 0 ; i < parser2.sortByName().size() ; i++) { System.out.println(i + "- Country: " + parser2.sortByName().get(i).getName() + ", capital: " + parser2.sortByName().get(i).getCapital() + ", population: " + parser2.sortByName().get(i).getPopulation() + ", area: " + parser2.sortByName().get(i).getArea()); } + File file2 = new File("sorted-by-name.txt"); + try { + if(file2.createNewFile()) { + System.out.println("A new file with this name : " + file2.getName() + " created for you :)"); + } + else { + System.out.println("the file is exist : " + file2.getName()); + } + FileWriter Writer = new FileWriter("sorted-by-name.txt"); + for (int i = 0 ; i < sortByName().size() ; i++) { + Writer.write((i + "- Country: " + parser2.sortByName().get(i).getName() + ", capital: " + parser2.sortByName().get(i).getCapital() + ", population: " + parser2.sortByName().get(i).getPopulation() + ", area: " + parser2.sortByName().get(i).getArea()).toString() + "\n"); + } + Writer.close(); + }catch (Exception e) { + System.out.println("Error"); + e.printStackTrace(); + } break; case 4 : stop = 0;