Database/Compte
This commit is contained in:
@@ -1,13 +1,109 @@
|
||||
package linea;
|
||||
|
||||
import java.util.List;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.UIManager;
|
||||
|
||||
public class LineaAppli {
|
||||
|
||||
private static String choisirPseudo(DatabaseConnection db, String message, int messageType) {
|
||||
List<String> pseudos = db.getPseudos();
|
||||
if (pseudos.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(null, "Aucun compte disponible.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return (String) JOptionPane.showInputDialog(
|
||||
null,
|
||||
message,
|
||||
"Comptes",
|
||||
messageType,
|
||||
null,
|
||||
pseudos.toArray(String[]::new),
|
||||
pseudos.get(0)
|
||||
);
|
||||
}
|
||||
|
||||
private static Integer menuComptes(DatabaseConnection db) {
|
||||
while (true) {
|
||||
Object[] options = {"Sélectionner", "Créer", "Supprimer", "Retour"};
|
||||
int choix = JOptionPane.showOptionDialog(
|
||||
null,
|
||||
"Gestion des comptes :",
|
||||
"Comptes",
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE,
|
||||
null,
|
||||
options,
|
||||
options[0]
|
||||
);
|
||||
|
||||
if (choix == JOptionPane.CLOSED_OPTION || choix == 3) return null;
|
||||
|
||||
switch (choix) {
|
||||
case 0 -> {
|
||||
String pseudo = choisirPseudo(db, "Sélectionnez un compte :", JOptionPane.QUESTION_MESSAGE);
|
||||
if (pseudo != null) return db.getIdParPseudo(pseudo);
|
||||
}
|
||||
case 1 -> {
|
||||
String pseudo = JOptionPane.showInputDialog(null, "Nouveau pseudo :");
|
||||
if (pseudo == null || pseudo.trim().isEmpty()) continue;
|
||||
pseudo = pseudo.trim();
|
||||
int id = db.getIdParPseudo(pseudo);
|
||||
return id > 0 ? id : db.creerCompte(pseudo);
|
||||
}
|
||||
case 2 -> {
|
||||
String pseudo = choisirPseudo(db, "Sélectionnez le compte à supprimer :", JOptionPane.WARNING_MESSAGE);
|
||||
if (pseudo == null) continue;
|
||||
int confirm = JOptionPane.showConfirmDialog(
|
||||
null,
|
||||
"Supprimer le compte \"" + pseudo + "\" et toute sa progression ?",
|
||||
"Confirmation",
|
||||
JOptionPane.YES_NO_OPTION
|
||||
);
|
||||
if (confirm == JOptionPane.YES_OPTION) {
|
||||
int id = db.getIdParPseudo(pseudo);
|
||||
if (id > 0) db.supprimerCompte(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Classe de base de l'application, rien à modifier ici
|
||||
//-------------------------------------------------------------------------
|
||||
public static void main(String[] arg) {
|
||||
UIManager.put("OptionPane.cancelButtonText", "Retour");
|
||||
|
||||
Jeu jeu = new Jeu();
|
||||
|
||||
jeu.demarrer();
|
||||
DatabaseConnection db = new DatabaseConnection();
|
||||
db.connect();
|
||||
db.createTables();
|
||||
|
||||
while (true) {
|
||||
Object[] options = {"Comptes", "Sans compte", "Quitter"};
|
||||
int choix = JOptionPane.showOptionDialog(null,
|
||||
"Choisissez une action :",
|
||||
"Menu", JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
|
||||
|
||||
if (choix == JOptionPane.CLOSED_OPTION || choix == 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (choix) {
|
||||
case 1 -> {
|
||||
new Jeu(db, -1).demarrer();
|
||||
return;
|
||||
}
|
||||
case 0 -> {
|
||||
Integer idCompte = menuComptes(db);
|
||||
if (idCompte != null) {
|
||||
new Jeu(db, idCompte).demarrer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user