ProLab-Project_1/src/main/java/Main.java

93 lines
3.3 KiB
Java

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String peselStr;
System.out.print("Please enter your PESEL number: ");
peselStr = scan.nextLine();
int result = checkPesel(peselStr);
while(result != 0 ){
if(result == 1){
System.out.println("Pesel number must be 11 digit!");
}else if(result == 2){
System.out.println("Your entry is not number!");
} else {
System.out.println("Pesel number is not valid!");
}
System.out.print("Please enter valid 11 digit number: ");
peselStr = scan.nextLine();
result = checkPesel(peselStr);
}
System.out.println("Your input: " + peselStr + " satisfies conditions. You can continue...\n");
int pageIndex = 1;
HTMLParser parser;
for (pageIndex = 1; pageIndex < 2; pageIndex++){
parser = new HTMLParser("src/main/resources/page_" + String.valueOf(pageIndex) + ".html");
System.out.println("Title of the page: " + parser.getTitle());
System.out.println("Links in the page: ");
Elements links = parser.getElements();
for (Element link :links) {
System.out.println("URL: " + link.attr("href"));
System.out.println("Text of the link element: " + link.text());
System.out.println();
}
List<String> filtered = links.stream()
.map(link -> link.attr("href"))
.filter (url -> url.startsWith("http"))
.collect (Collectors.toList());
for (String linkStr :filtered) {
System.out.println("Out going URL: " + linkStr);
System.out.println();
}
}
}
public static int checkPesel(String peselStr){
try {
long pesel = Long.valueOf(peselStr);
if(peselStr.length() != 11){
return 1;
}else{
int checkSum = 0;
int lastDigit = (int)( pesel % 10);
pesel /= 10;
checkSum += 3 * (pesel % 10);
pesel /= 10;
checkSum += 1 * (pesel % 10);
pesel /= 10;
checkSum += 9 * (pesel % 10);
pesel /= 10;
checkSum += 7 * (pesel % 10);
pesel /= 10;
checkSum += 3 * (pesel % 10);
pesel /= 10;
checkSum += 1 * (pesel % 10);
pesel /= 10;
checkSum += 9 * (pesel % 10);
pesel /= 10;
checkSum += 7 * (pesel % 10);
pesel /= 10;
checkSum += 3 * (pesel % 10);
pesel /= 10;
checkSum += 1 * (pesel % 10);
/*System.out.println(checkSum);
System.out.println(lastDigit);*/
if((10 - (checkSum % 10)) % 10 != lastDigit ){
return 3;
}
}
}catch (NumberFormatException e){
return 2;
}
return 0;
}
}