Files
HW-02-git-and-java-practice/src/main/java/BonusExercises.java
T

182 lines
5.0 KiB
Java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BonusExercises {
/*
complete the method below, so it will validate an email address
1. Must have exactly one @ (no more, no less)
2. Split into local-part and domain (before @ and after @)
3. Local-part rules:
- Can't be empty
- Can't start or end with dot
- Can't have two dots in a row
4. Domain rules:
- Can't be empty
- Can't start or end with hyphen
- Can't have underscores
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
if (email.contains(" "))
return false;
int count = 0;
int index = 0;
// rule 1:
for (int i = 0; i < email.length() -1; i++) {
if (email.charAt(i) == '@') {
count++;
index = i;
}
}
if (count != 1)
return false;
// rule 2:
String localPart = email.substring(0,index);
String domainPart = email.substring(index + 1);
if (localPart.isEmpty() || domainPart.isEmpty())
return false;
if (localPart.startsWith(".") || localPart.endsWith(".") ||
domainPart.startsWith(".") || domainPart.endsWith("."))
return false;
if (localPart.contains(".."))
return false;
if (domainPart.contains("_"))
return false;
return true;
}
/*
This method should find and return the first date in a string.
Supported formats:
- American: MM/DD/YYYY (e.g., 12/09/2023)
- British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters)
- ISO: YYYY-MM-DD (e.g., 2024-07-15)
- Slash variant: YYYY/MM/DD (e.g., 2025/01/01)
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
return null;
}
/*
given a string, implement the method to detect all valid passwords
then, it should return the count of them
a valid password has the following properties:
- at least 8 characters
- has to include at least one uppercase letter, and at least a lowercase
- at least one number and at least a special char "!@#$%^&*"
- has no white-space in it
*/
public int findValidPasswords(String string) {
if (string.length() < 8)
return 0;
int validPassWords = 0;
List<String> subStrings = List.of(string.split("\\s+"));
for (String newPart : subStrings) {
//at least 8 characters
if (newPart.length() < 8)
continue;
// at least one uppercase letter
if (!newPart.matches(".*[A-Z].*"))
continue;
// and at least a lowercase
if (!newPart.matches(".*[a-z].*"))
continue;
// at least one number
if (!newPart.matches(".*\\d.*"))
continue;
// at least one spacial character
if (!newPart.matches(".*[!@#$%^&*].*"))
continue;
validPassWords++;
}
return validPassWords;
}
/*
you should return a list of *words* which are palindromic
by word we mean at least 3 letters with no whitespace in it
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
Pattern words = Pattern.compile("[a-zA-Z]+");
Matcher matcher = words.matcher(string);
while (matcher.find()) {
String word = matcher.group();
if (word.length() < 2)
continue;
String reversed = new StringBuilder(word).reverse().toString();
if (reversed.equalsIgnoreCase(word))
list.add(word);
}
return list;
}
public static void main(String[] args) {
// you can test your code here
BonusExercises kir = new BonusExercises();
System.out.println(kir.findValidPasswords("""
[09:15] Dev1: Just changed my password to CodeMaster@2025. \s
[09:17] Dev2: Haha, mine's still qwerty123, no special chars. \s
[09:19] Dev3: I use GitHubSuper#1 but need a better one. \s
[09:21] Dev4: AdminPass42! is good, right? \s
[09:23] Dev5: No, too simple. I switched to UltraSecure$99 last week. \s
[09:25] Dev6: Wait, are we sharing passwords here? \uD83D\uDE02 \s
"""));
}
}