exercício 2
package SENHA;
public class SenhaInvalidaException extends RuntimeException {
public SenhaInvalidaException(String mensagem) {
super(mensagem);
}
}
package SENHA;
import java.util.Scanner;
public class Senha {
public static void main(String[] args) {
Scanner leitura = new Scanner(System.in);
System.out.println("\n--------Sistema de Senhas--------");
System.out.println("\nDigite a sua senha, lembrando que ela deve ter 8 caracteres numéricos:");
String senha = leitura.nextLine();
try{
senhaValida(senha);
System.out.println("SENHA.Senha válida! Acesso Permitido!");
}catch (SenhaInvalidaException e) {
System.out.println("Erro: " + e.getMessage());
}
}
private static void senhaValida(String senha) {
if (senha.length() <8) {
throw new SenhaInvalidaException("A senha deve ter pelo menos 8 caracteres.");
}
if(!senha.matches("\\d+")) {
throw new SenhaInvalidaException("A senha deve conter apenas números.");
}
}
}