Skip to content

Commit 0f74442

Browse files
authored
Merge pull request mouredev#7035 from Josegs95/main
#15 y #16 - Java
2 parents d1e8479 + 0a59d00 commit 0f74442

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.time.LocalTime;
2+
import java.util.concurrent.BrokenBarrierException;
3+
import java.util.concurrent.CyclicBarrier;
4+
5+
public class Josegs95 {
6+
public static void main(String[] args) {
7+
//Ejercicio
8+
Thread thread = new Thread(new Task("Tarea 1", 5, false));
9+
thread.start();
10+
11+
//Reto
12+
System.out.println("\n");
13+
retoFinal();
14+
System.out.println("Finaliza el programa");
15+
}
16+
17+
private static CyclicBarrier barrier;
18+
19+
public static void retoFinal(){
20+
Thread thread1 = new Thread(new Task("Función C", 3, true));
21+
Thread thread2 = new Thread(new Task("Función B", 2, true));
22+
Thread thread3 = new Thread(new Task("Función A", 1, true));
23+
Thread thread4 = new Thread(new Task("Función D", 1, false));
24+
25+
barrier = new CyclicBarrier(3, thread4);
26+
27+
thread1.start();
28+
thread2.start();
29+
thread3.start();
30+
}
31+
32+
public static class Task implements Runnable{
33+
34+
private String name;
35+
private int duration;
36+
private boolean useBarrier;
37+
38+
public Task(String name, int duration, boolean useBarrier){
39+
this.name = name;
40+
this.duration = duration;
41+
this.useBarrier = useBarrier;
42+
}
43+
44+
@Override
45+
public void run() {
46+
System.out.println(name + " empieza a las " + LocalTime.now() + " y durará " + duration + " segundos");
47+
try {
48+
Thread.sleep(duration * 1000);
49+
} catch (InterruptedException e) {
50+
throw new RuntimeException(e);
51+
}
52+
System.out.println(name + " finaliza a las " + LocalTime.now());
53+
54+
if (useBarrier){
55+
try {
56+
barrier.await();
57+
} catch (InterruptedException e) {
58+
throw new RuntimeException(e);
59+
} catch (BrokenBarrierException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class Josegs95 {
7+
public static void main(String[] args) {
8+
//Ejercicio
9+
Pattern pattern = Pattern.compile("\\d");
10+
Matcher matcher = pattern.matcher("En la calle ví 2 coches, 2 perros, 1 señora y 6 farolas");
11+
while(matcher.find()){
12+
System.out.println("Match: " + matcher.group());
13+
}
14+
15+
//Reto
16+
System.out.println("\n");
17+
retoFinal();
18+
}
19+
20+
public static void retoFinal(){
21+
/*Email
22+
He decidido que los emails son validos si tienen:
23+
Cadena(1-n) + '.'||'-'||'_'(opcional) + Cadena(1-n)
24+
@
25+
Cadena(1-n) + '-'(opcional) + '.' + Cadena(2-3)
26+
*/
27+
List<String> emailList = getEmailExamples();
28+
29+
String emailPattern = "^\\w+([.\\-_]?\\w+)*@\\w+(\\-\\w+)?\\.\\w{2,3}$";
30+
31+
for (String email : emailList){
32+
System.out.println("\""+ email + "\" es correcto: " + Pattern.matches(emailPattern, email));
33+
}
34+
35+
//Número de telefono
36+
/*He decidido que los número de teléfonos son validos si tienen:
37+
9 dígitos o
38+
'+' + 11 dígitos
39+
*/
40+
List<String> phoneList = getPhoneExamples();
41+
42+
String phonePattern = "^\\d{9}|\\+\\d{11}$";
43+
44+
for (String phoneNumber : phoneList){
45+
System.out.println("\""+ phoneNumber + "\" es correcto: " + Pattern.matches(phonePattern, phoneNumber));
46+
}
47+
48+
//URL
49+
/*He decidido que las URL son validas si tienen:
50+
'http'|'https''://'(opcional) + (Cadena(1-n) + '.')(opcional) +
51+
Cadena(1-n) + ('-' + Cadena(1-n))(opcional) + '.' + Cadena(2-3)
52+
*/
53+
List<String> urlList = getURLExamples();
54+
55+
//String urlPattern = "^((http|https)?://)?(\\w+\\.)?\\w+\\.\\w{2,3}";
56+
String urlPattern = "^((http|https)?://)?(\\w+\\.)?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*\\.\\w{2,3}";
57+
58+
for (String url : urlList){
59+
System.out.println("\""+ url + "\" es correcta: " + Pattern.matches(urlPattern, url));
60+
}
61+
}
62+
63+
private static List<String> getEmailExamples(){
64+
List<String> emailList = new ArrayList<>();
65+
emailList.add("[email protected]");
66+
emailList.add("[email protected]");
67+
emailList.add("[email protected]");
68+
emailList.add("[email protected]");
69+
emailList.add("[email protected]");
70+
71+
emailList.add("[email protected]");
72+
emailList.add("[email protected]");
73+
emailList.add("[email protected]");
74+
emailList.add("[email protected]");
75+
emailList.add("juan.carlos@gmailcom.");
76+
emailList.add("[email protected]");
77+
emailList.add("juan.carlos@gmail-com");
78+
emailList.add("[email protected]");
79+
80+
return emailList;
81+
}
82+
83+
private static List<String> getPhoneExamples(){
84+
List<String> phoneList = new ArrayList<>();
85+
phoneList.add("666554433");
86+
phoneList.add("+34666554433");
87+
88+
phoneList.add("666554433808");
89+
phoneList.add("66+6554433808");
90+
phoneList.add("66655");
91+
phoneList.add("66655plko");
92+
93+
return phoneList;
94+
}
95+
96+
private static List<String> getURLExamples(){
97+
List<String> urlList = new ArrayList<>();
98+
urlList.add("https://www.google.es");
99+
urlList.add("www.google.es");
100+
urlList.add("videos.google.es");
101+
urlList.add("google.es");
102+
urlList.add("www.google-new.es");
103+
104+
urlList.add("hptts://www.google.es");
105+
urlList.add(".google.es");
106+
urlList.add("google.");
107+
urlList.add("www.goo_gle.es");
108+
urlList.add("www.google-.es");
109+
urlList.add("www.goo gle.es");
110+
urlList.add("www.google.españa");
111+
112+
return urlList;
113+
}
114+
115+
}

0 commit comments

Comments
 (0)