Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/main/java/Country.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
98 changes: 82 additions & 16 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,107 @@

public class Parser {
static List<Country> countries = new ArrayList<>();
static List<Elements> htmlElementsList = new ArrayList<>();

public List<Country> sortByName(){
public static List<Country> sortByName(){
List<Country> 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<Country> sortByPopulation(){
public static List<Country> sortByPopulation(){
List<Country> 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<Country> sortByArea(){
public static List<Country> sortByArea(){
List<Country> 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;
}

}
}