feat: enhance symbol input validation and improve console messaging

This commit is contained in:
2025-01-04 16:51:12 +01:00
parent 7c4fa8cbe8
commit 3e6aa821fc
5 changed files with 95 additions and 84 deletions

View File

@@ -2,7 +2,6 @@ package sudoku;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import sudoku.core.Console; import sudoku.core.Console;
@@ -45,18 +44,19 @@ public class Grille {
/** /**
* Crée un bloc à partir des positions spécifiées * Crée un bloc à partir des positions spécifiées
*
Exemple : * Exemple :
sudoku.getGrille().createBloc(Arrays.asList( * sudoku.getGrille().createBloc(Arrays.asList(
new int[] { 0, 0 }, * new int[] { 0, 0 },
new int[] { 0, 1 }, * new int[] { 0, 1 },
new int[] { 0, 2 }, * new int[] { 0, 2 },
new int[] { 1, 0 }, * new int[] { 1, 0 },
new int[] { 1, 1 }, * new int[] { 1, 1 },
new int[] { 1, 2 }, * new int[] { 1, 2 },
new int[] { 2, 0 }, * new int[] { 2, 0 },
new int[] { 2, 1 }, * new int[] { 2, 1 },
new int[] { 2, 2 })); * new int[] { 2, 2 }));
*
* @param positions * @param positions
* @return * @return
*/ */
@@ -93,12 +93,12 @@ public class Grille {
// TO MOVE TO FUTUR CONSTRAINTS // TO MOVE TO FUTUR CONSTRAINTS
// public boolean isValid() { // public boolean isValid() {
// for (Bloc bloc : blocs) { // for (Bloc bloc : blocs) {
// if (!bloc.isValid()) { // if (!bloc.isValid()) {
// return false; // return false;
// } // }
// } // }
// return true; // return true;
// } // }
public void printBlocs() { public void printBlocs() {
@@ -109,92 +109,76 @@ public class Grille {
public void askSetSymbolesPossibles() { public void askSetSymbolesPossibles() {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
try{ try {
System.out.println(("Choisissez le type de symboles :")); Console.infoln("Choisissez le type de symboles :");
System.out.println(("1. Nombres")); Console.infoln("1. Nombres");
System.out.println(("2. Caractères")); Console.infoln("2. Lettres");
System.out.println(("3. Texte/Emoji")); Console.infoln("3. Texte/Emoji");
int choix = 0; int choix = 0;
try { try {
choix = scanner.nextInt(); choix = Integer.parseInt(scanner.nextLine());
scanner.nextLine(); if (choix < 1 || choix > 3) {
throw new NumberFormatException("Choix invalide");
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Console.errorln("Choix invalide"); Console.errorln("Choix invalide");
return; return;
} }
for (int i = 0; i < taille; i++) { for (int i = 0; i < taille; i++) {
System.out.println(("Entrez le symbole " + (i + 1) + "/" + taille + " :")); System.out.println(("Entrez le symbole " + (i + 1) + "/" + taille + " :"));
String input = scanner.nextLine(); String input = scanner.nextLine();
switch (choix) { switch (choix) {
case 1: // Nombres case 1: // Nombres
try { Symbole intTemp = Symbole.of(input);
Symbole intTemp = Symbole.of(Integer.parseInt(input)); if (intTemp.isInt()) {
//Evaluation de la du caractère qui vient d'être saisit if (symbolesPossibles.contains(intTemp)) {
for (Symbole symbole : symbolesPossibles) { Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
if (Objects.equals(intTemp.toString(), symbole.toString())){ i--;
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole"); } else {
throw new NumberFormatException("Ce symbole existe déja"); symbolesPossibles.add(intTemp);
}
} }
symbolesPossibles.add(Symbole.of(Integer.parseInt(input))); } else {
} catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un nombre valide"); Console.errorln("Veuillez entrer un nombre valide");
i--; i--;
} }
break; break;
case 2: // Caractères case 2: // Lettres
if (input.length() == 1) { if (input.length() == 1 && Symbole.of(input.charAt(0)).isLetter()) {
try { Symbole charTemp = Symbole.of(input.charAt(0));
Symbole charTemp = Symbole.of(input); if (symbolesPossibles.contains(charTemp)) {
//Evaluation de la du caractère qui vient d'être saisit Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
for (Symbole symbole : symbolesPossibles) {
if (Objects.equals(charTemp.toString(), symbole.toString())) {
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole");
throw new NumberFormatException("Ce symbole existe déja");
}
}
symbolesPossibles.add(Symbole.of(input.charAt(0)));
}catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un caractère valide");
i--; i--;
} else {
symbolesPossibles.add(charTemp);
} }
} else { } else {
Console.errorln("Veuillez entrer un seul caractère"); Console.errorln("Veuillez entrer une lettre valide");
i--; i--;
} }
break; break;
case 3: // Texte/Emoji case 3: // Texte/Emoji
if (!input.isEmpty()) { Symbole stringTemp = Symbole.of(input);
try { if (symbolesPossibles.contains(stringTemp)) {
Symbole textTemp = Symbole.of(input); Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
//Evaluation de la du caractère qui vient d'être saisit
for (Symbole symbole : symbolesPossibles) {
if (Objects.equals(textTemp.toString(), symbole.toString())) {
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole");
throw new NumberFormatException("Ce symbole existe déja");
}
}
symbolesPossibles.add(Symbole.of(input));
}catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un texte ou un emoji valide");
i--;
}
} else {
Console.errorln("Le symbole ne peut pas être vide");
i--; i--;
} else {
symbolesPossibles.add(stringTemp);
} }
break; break;
default: default:
Console.errorln("Type non supporté"); Console.errorln("Type non supporté");
return; return;
} }
} }
}catch (Exception e) { } catch (Exception e) {
System.out.println("Une erreur est survenue : " + e.getMessage()); System.out.println("Une erreur est survenue : " + e.getMessage());
}finally { } finally {
scanner.close(); scanner.close();
} }
} }

View File

@@ -20,6 +20,19 @@ public class Symbole {
return new Symbole(String.valueOf(c)); return new Symbole(String.valueOf(c));
} }
public boolean isInt() {
try {
Integer.parseInt(valeur);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public boolean isLetter() {
return valeur.length() == 1 && Character.isLetter(valeur.charAt(0));
}
@Override @Override
public String toString() { public String toString() {
return valeur; return valeur;
@@ -27,9 +40,11 @@ public class Symbole {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass()) return false; if (obj == null || getClass() != obj.getClass())
return false;
Symbole symbole = (Symbole) obj; Symbole symbole = (Symbole) obj;
return valeur.equals(symbole.valeur); return valeur.equals(symbole.valeur);
} }

View File

@@ -11,14 +11,18 @@ public class Console {
static final String ANSI_BOLD = "\u001B[1m"; static final String ANSI_BOLD = "\u001B[1m";
public static void errorln(String message) { public static void errorln(String message) {
System.err.println(ANSI_RED + ANSI_BOLD + message + ANSI_RESET); System.err.println(ANSI_RED + ANSI_BOLD + message + ANSI_RESET + "\n");
} }
public static void successln(String message) { public static void successln(String message) {
System.out.println(ANSI_GREEN + ANSI_BOLD + message + ANSI_RESET); System.out.println(ANSI_GREEN + ANSI_BOLD + message + ANSI_RESET + "\n");
} }
public static void warnln(String message) { public static void warnln(String message) {
System.out.println(ANSI_YELLOW + ANSI_BOLD+ message + ANSI_RESET); System.out.println(ANSI_YELLOW + ANSI_BOLD + message + ANSI_RESET + "\n");
}
public static void infoln(String message) {
System.out.println(message + "\n");
} }
} }

View File

@@ -1,10 +1,8 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package sudoku; package sudoku;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -13,13 +11,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class AppTest { public class AppTest {
@Test @Test
public void testExample() { public void testExample() {
System.out.println("DOING TEST"); // Simulate user input
System.out.println(new App().getGreeting()); String simulatedInput = "1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes()));
// Create a new Sudoku // Create a new Sudoku
Sudoku sudoku = new Sudoku(9); Sudoku sudoku = new Sudoku(9);
sudoku.getGrille().askSetSymbolesPossibles(); sudoku.getGrille().askSetSymbolesPossibles();
System.out.println("DONE TEST"); // Verify the symbols
ArrayList<Symbole> expectedSymbols = new ArrayList<>(Arrays.asList(
Symbole.of(1),
Symbole.of(2),
Symbole.of(3),
Symbole.of(4),
Symbole.of(5),
Symbole.of(6),
Symbole.of(7),
Symbole.of(8),
Symbole.of(9)));
assertEquals(expectedSymbols, sudoku.getGrille().getSymbolesPossibles());
} }
} }

View File

@@ -5,8 +5,6 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestColonne { public class TestColonne {
@Test @Test
public void testColonne() { public void testColonne() {
@@ -34,7 +32,7 @@ public class TestColonne {
sudoku.getGrille().setCase(5, 6, Symbole.of(7)); sudoku.getGrille().setCase(5, 6, Symbole.of(7));
sudoku.getGrille().setCase(0, 7, Symbole.of(8)); sudoku.getGrille().setCase(0, 7, Symbole.of(8));
sudoku.getGrille().setCase(4, 8, Symbole.of(9)); sudoku.getGrille().setCase(4, 8, Symbole.of(9));
//doublon colonne // doublon colonne
sudoku.getGrille().setCase(4, 0, Symbole.of(1)); sudoku.getGrille().setCase(4, 0, Symbole.of(1));
sudoku.getGrille().createSquareBlocs(); sudoku.getGrille().createSquareBlocs();