Skip to content

Commit

Permalink
Merge pull request #4943 from jorgenavarroenamoradotokio/main
Browse files Browse the repository at this point in the history
Retos #23, #24, #25, #26, #27, #28, #29 Java
  • Loading branch information
Roswell468 authored Sep 9, 2023
2 parents 4459add + dfacd49 commit 6fb4cbb
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.retos.ej23;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class jorgenavarroenamoradotokio {

private static final String DB_URL = "jdbc:mysql://mysql-5707.dinaserver.com:3306/moure_test";
private static final String DB_USER = "mouredev_read";
private static final String DB_PASSWORD = "mouredev_pass";

public static void main(String[] args) {

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

String strSql = "SELECT COUNT(*) as total FROM challenges";
Statement statement = conexion.createStatement();
ResultSet rs = statement.executeQuery(strSql);

if (rs.next()) {
System.out.println(rs.getString("total"));
}

rs.close();
statement.close();
conexion.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.retos.ej24;

import java.util.ArrayList;
import java.util.List;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
caesarCipher("Mi nombre es MoureDev.", false, 3);
caesarCipher("ol proeuh hv orxuhghy.", true, 3);
caesarCipher("Mi nombre es MoureDev.", false, 5);
caesarCipher("qn rtqgwj jx qtzwjija.", true, 5);
}

public static void caesarCipher(String text, boolean decrypt, int shift) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
List<Character> charList = new ArrayList<>();
for (char c : alphabet.toCharArray()) {
charList.add(c);
}
charList.add(charList.indexOf('n') + 1, 'ñ');

StringBuilder caesarText = new StringBuilder();

text = text.toLowerCase();

for (char value : text.toCharArray()) {
if (charList.contains(value)) {
int index = (charList.indexOf(value) + (decrypt ? -shift : shift)) % charList.size();
if (index < 0) {
index += charList.size();
}
caesarText.append(charList.get(index));
} else {
caesarText.append(value);
}
}
System.out.println(caesarText.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.retos.ej25;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class jorgenavarroenamoradotokio {

private static final int[] KONAMI_CODE = { KeyEvent.VK_UP, KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_DOWN,
KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_B, KeyEvent.VK_A };

private static int keyPosition = 0;
private static int lastKey = KeyEvent.VK_ESCAPE;

public static void main(String[] args) {
JFrame frame = new JFrame("Konami Code Detector");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// No se utiliza en este ejemplo
}

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_ESCAPE) {
System.out.println("Exit");
System.exit(0);
}

if (key == KONAMI_CODE[keyPosition]) {
keyPosition++;
} else if (key == KONAMI_CODE[0]) {
if (lastKey == KONAMI_CODE[0]) {
keyPosition = 2;
} else {
keyPosition = 1;
}
} else {
keyPosition = 0;
}

if (keyPosition == KONAMI_CODE.length) {
System.out.println("\n" + "╦╔═╔═╗╔╗╔╔═╗╔╦╗╦ ╔═╗╔═╗╔╦╗╔═╗\n" + "╠╩╗║ ║║║║╠═╣║║║║ ║ ║ ║ ║║║║╣\n"
+ "╩ ╩╚═╝╝╚╝╩ ╩╩ ╩╩ ╚═╝╚═╝═╩╝╚═╝\n" + "\n");
System.exit(0);
}

lastKey = key;
}

@Override
public void keyReleased(KeyEvent e) {
// No se utiliza en este ejemplo
}
});

frame.setSize(200, 200);
frame.setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.retos.ej26;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.DayOfWeek;
import java.time.LocalDate;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class jorgenavarroenamoradotokio {

@Test
void whenDiaNoEsViernes() {
assertFalse(existeViernesTreceLocalDate(9, 2023));
}

@Test
void whenDiaEsViernes() {
assertTrue(existeViernesTreceLocalDate(1, 2023));
}

@Test
void whenDiaEsCero() {
Exception thrown = Assertions.assertThrows(Exception.class, () -> {
existeViernesTreceLocalDate(0, 2023);
}, "Exception was expected");

Assertions.assertEquals("Invalid value for MonthOfYear (valid values 1 - 12): 0", thrown.getMessage());
}

private boolean existeViernesTreceLocalDate(int mes, int anio) {
LocalDate localDate = LocalDate.of(anio, mes, 13);
return localDate.getDayOfWeek() == DayOfWeek.FRIDAY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.retos.ej27;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
countdown(10, 1);
}

private static void countdown(int start, int interval) {
if (start <= 0 || interval <= 0) {
System.out.println("Los parámetros deben ser números enteros positivos.");
return;
}

for (; start >= 0;start = switch (start) {
case 0 -> -1;
default -> {
System.out.println(start);
yield start - 1;
}
}) {
try {
Thread.sleep(interval * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.retos.ej28;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
System.out.println(checkMathExp("3 + 5"));
System.out.println(checkMathExp("3 a 5"));
System.out.println(checkMathExp("-3 + 5"));
System.out.println(checkMathExp("- 3 + 5"));
System.out.println(checkMathExp("-3 a 5"));
System.out.println(checkMathExp("-3+5"));
System.out.println(checkMathExp("3 + 5 - 1 / 4 % 8"));
}

private static boolean checkMathExp(String expression) {
String[] components = expression.split(" ");

if (components.length < 3 || components.length % 2 == 0) {
return false;
}

boolean check = true;
for (int index = 0; index < components.length; index++) {
String component = components[index];
if (index % 2 == 0) {
try {
Double.parseDouble(component);
} catch (NumberFormatException e) {
check = false;
}
} else {
check = "+".equals(component) || "-".equals(component) || "*".equals(component) || "/".equals(component)
|| "%".equals(component);
}

if (!check) {
return false;
}
}
return check;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.retos.ej29;

import java.util.ArrayList;
import java.util.List;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
System.out.println(infiltratedCharacters("Me llamo mouredev", "Me llemo mouredov"));
System.out.println(infiltratedCharacters("Me llamo.Brais Moure", "Me llamo brais moure"));
System.out.println(infiltratedCharacters("Me llamo.Brais Moure", "Me llamo brais moure "));
System.out.println(infiltratedCharacters("", ""));
}

private static List<Character> infiltratedCharacters(String firstText, String secondText) {
List<Character> characters = new ArrayList<>();

if (firstText.length() == secondText.length()) {
for (int index = 0; index < firstText.length(); index++) {
if (firstText.charAt(index) != secondText.charAt(index)) {
characters.add(secondText.charAt(index));
}
}
}

return characters;
}
}

0 comments on commit 6fb4cbb

Please sign in to comment.