diff --git a/ProjetJava/src/main/java/models/Calcul.java b/ProjetJava/src/main/java/models/Calcul.java new file mode 100644 index 0000000000000000000000000000000000000000..4f206bcb76d0585a742c4941e9308237eaf1d06a --- /dev/null +++ b/ProjetJava/src/main/java/models/Calcul.java @@ -0,0 +1,115 @@ +package models; + +import java.util.concurrent.ThreadLocalRandom; + +/** + * + * @author Adrien + */ +public class Calcul { + + private int level; + private int operand1; + private int operand2; + private int operator; + private int result; + + public Calcul(int level) { + if (level == 1) { + this.level = level; + generateOperator(level); + + // Donne une valeur entre 1 et 9 + operand1 = ThreadLocalRandom.current().nextInt(1, 10); + operand2 = ThreadLocalRandom.current().nextInt(1, 10); + + // On échange les valeur si nécessaire pour éviter les résultats négatifs + if (operand1 < operand2) { + int tempNb = operand1; + operand1 = operand2; + operand2 = tempNb; + } + } else { // Niveau 2 + this.level = level; + generateOperator(level); + + // Ici on gère les multiplications + if (operator == 2) { + // Donne une valeur entre 1 et 9 + operand1 = ThreadLocalRandom.current().nextInt(1, 10); + operand2 = ThreadLocalRandom.current().nextInt(1, 10); + } else { + // Sinon pour les additions et soustractions + // Donne une valeur entre 1 et 999 + operand1 = ThreadLocalRandom.current().nextInt(1, 1000); + operand2 = ThreadLocalRandom.current().nextInt(1, 1000); + } + } + } + + // Génére 0 , 1, ou 2 pour - , + ou * + public final void generateOperator(int level) { + if (level == 1) { + operator = ThreadLocalRandom.current().nextInt(0, 2); + } else { + operator = ThreadLocalRandom.current().nextInt(0, 3); + } + } + + // Similaire au getter, mais retourne une chaine pour l'affichage + public final String showOperator() { + String stringOperator = ""; + + switch (operator) { + case 0: + stringOperator = "+"; + break; + case 1: + stringOperator = "-"; + break; + case 2: + stringOperator = "x"; + break; + } + return stringOperator; + } + + public int getLevel() { + return level; + } + + public int getOperand1() { + return operand1; + } + + public int getOperand2() { + return operand2; + } + + public int getOperator() { + return operator; + } + + public int getResult() { + return result; + } + + public void setResult(int result) { + this.result = result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Calcul{"); + sb.append("level=").append(level); + sb.append(", operand1=").append(operand1); + sb.append(", operand2=").append(operand2); + sb.append(", operator=").append(operator); + sb.append(", result=").append(result); + sb.append('}'); + return sb.toString(); + } + + +} diff --git a/ProjetJava/src/main/java/services/CalculService.java b/ProjetJava/src/main/java/services/CalculService.java new file mode 100644 index 0000000000000000000000000000000000000000..c0f891fe3d99776b8ed2ebd6d9e3eed1569544d6 --- /dev/null +++ b/ProjetJava/src/main/java/services/CalculService.java @@ -0,0 +1,50 @@ +package services; + +import models.Calcul; + +/** + * + * @author Adrien + */ +public class CalculService { + + public void addition(Calcul newCalcul) { + int result = newCalcul.getOperand1() + newCalcul.getOperand2(); + newCalcul.setResult(result); + } + + public void subtraction(Calcul newCalcul) { + int result = newCalcul.getOperand1() - newCalcul.getOperand2(); + newCalcul.setResult(result); + } + + public void multiplication(Calcul newCalcul) { + int result = newCalcul.getOperand1() * newCalcul.getOperand2(); + newCalcul.setResult(result); + } + + public Calcul generateCalcul(int level) { + Calcul newCalcul = new Calcul(level); + + if (level == 1) { + if (newCalcul.getOperator() == 0) { + addition(newCalcul); + } else { + subtraction(newCalcul); + } + } else { + switch (newCalcul.getOperator()) { + case 0: + addition(newCalcul); + break; + case 1: + subtraction(newCalcul); + break; + case 2: + multiplication(newCalcul); + break; + } + } + return newCalcul; + } +} diff --git a/ProjetJava/src/main/java/views/CalculPanel.java b/ProjetJava/src/main/java/views/CalculPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..0c3c02e493e60d0daa2724ee4d861bfa0a5895ea --- /dev/null +++ b/ProjetJava/src/main/java/views/CalculPanel.java @@ -0,0 +1,161 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package views; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import models.Calcul; +import services.CalculService; + +/** + * + * @author stag + */ +public class CalculPanel extends JPanel { + + // Initialiser le calcul + private CalculService cs = new CalculService(); + private Calcul calcul; + + Color lightGreen = new Color(180, 230, 180); + Color lightRed = new Color(245, 190, 190); + + public CalculPanel() { + this.calcul = cs.generateCalcul(1); + // Layout principal + setLayout(new BorderLayout()); + + // Une grille de 2 lignes et 1 colonne + JPanel container = new JPanel(new GridLayout(2, 1, 0, 30)); + + // Affichage de la question + JPanel panelCalcul = new JPanel(new FlowLayout(FlowLayout.CENTER)); + JLabel labelCalcul = new JLabel(); + showCalcul(calcul, labelCalcul); + + labelCalcul.setFont(new Font("Arial", Font.BOLD, 36)); + panelCalcul.add(labelCalcul); + + // Affichage du label et champ de saisie + JPanel panelResponse = new JPanel(new FlowLayout(FlowLayout.CENTER)); + JLabel labelReponse = new JLabel("Votre réponse : "); + labelReponse.setFont(new Font("Arial", Font.PLAIN, 16)); + JTextField textResponse = new JTextField(20); + textResponse.setFont(new Font("Arial", Font.PLAIN, 16)); + + panelResponse.add(labelReponse); + panelResponse.add(textResponse); + + // Texte de vérif + JLabel labelVerif = new JLabel(""); + panelResponse.add(labelVerif); + + // Ajout des deux lignes dans la grille + container.add(panelCalcul); + container.add(panelResponse); + + // Mettre le container dans un container centré + JPanel containerCenter = new JPanel(new GridBagLayout()); + containerCenter.add(container); + + // Bouton (Partie SUD) + JPanel panelSud = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 20)); + JButton btnCheck = new JButton("Vérifier"); + + // Vérifie la valeur entrée au click du bouton + btnCheck.addActionListener(e -> checkAnswer(textResponse, labelVerif)); + + //Vérifie la valeur après la touche entrée + textResponse.addActionListener(e -> checkAnswer(textResponse, labelVerif)); + + // Couleur + btnCheck.setBackground(new Color(76, 128, 45)); + // Couleur texte + btnCheck.setForeground(Color.WHITE); + // Taille (horizontal, vertical) + btnCheck.setPreferredSize(new Dimension(200, 30)); + + JButton btnSolution = new JButton("Solution"); + btnSolution.setPreferredSize(new Dimension(200, 30)); + + // Insère le résultat en cliquant sur le bouton solution + btnSolution.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + clearResponse(textResponse, labelVerif); + textResponse.setText(String.valueOf(calcul.getResult())); + } + }); + + JButton btnNewCalcul = new JButton("Nouveau calcul"); + btnNewCalcul.setPreferredSize(new Dimension(200, 30)); + + btnNewCalcul.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + calcul = cs.generateCalcul(1); + showCalcul(calcul, labelCalcul); + + // Remet les réponses à 0 + clearResponse(textResponse, labelVerif); + } + }); + + panelSud.add(btnCheck); + panelSud.add(btnSolution); + panelSud.add(btnNewCalcul); + + // Assemblage + add(containerCenter, BorderLayout.CENTER); + add(panelSud, BorderLayout.SOUTH); + } + + private void checkAnswer(JTextField t, JLabel l) { + try { + int answer = Integer.parseInt(t.getText()); + + if (answer == calcul.getResult()) { + l.setText("Vrai !"); + t.setBackground(lightGreen); + t.setBorder(BorderFactory.createLineBorder(Color.green)); + } else { + l.setText("Faux !"); + t.setBackground(lightRed); + t.setBorder(BorderFactory.createLineBorder(Color.RED)); + } + + } catch (NumberFormatException ex) { + l.setText("Veuillez entrer un nombre !"); + t.setBackground(lightRed); + t.setBorder(BorderFactory.createLineBorder(Color.RED)); + } + } + + public final void showCalcul(Calcul c, JLabel l) { + l.setText(c.getOperand1() + " " + + c.showOperator() + " " + + c.getOperand2() + " = "); + } + + public final void clearResponse(JTextField t, JLabel l) { + // Remet les réponses à 0 + t.setText(""); + l.setText(""); + t.setBackground(Color.WHITE); + t.setBorder(null); + } +} diff --git a/ProjetJava/src/main/java/views/PrimaryWindow.java b/ProjetJava/src/main/java/views/PrimaryWindow.java index 07b0ce438cc023f1f087c3dc28017eb42676f85a..be0efa616c0bad83786ec179ccc169fca1171eec 100644 --- a/ProjetJava/src/main/java/views/PrimaryWindow.java +++ b/ProjetJava/src/main/java/views/PrimaryWindow.java @@ -62,14 +62,14 @@ public class PrimaryWindow extends JFrame { MagicSlate ardoise = new MagicSlate(); // Calcul - //JPanel CalculPanel p = new CalculPanel(); + CalculPanel p = new CalculPanel(); // Question QuestionPanel panelQuestion = new QuestionPanel(); // Ajout des onglets visibles tabbedPane.addTab("Dessin", ardoise); - //tabbedPane.addTab("Calcul", panelCalcul); + tabbedPane.addTab("Calcul", p); tabbedPane.addTab("Question", panelQuestion); /*