/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package diet;

/**
 *
 * @author therbron
 */
public class PoidsIdeal extends javax.swing.JFrame {

    private String nom;
    private String prenom;
    private double imc;
    private double poids;
    private double taille;
    private int age;
    final private double imcs[] = {16.5, 18.5, 25, 30, 35, 40};
    final private String corpulences[] = {"Famine", "Maigreur", "Normale", "Surpoids", "Obésité modérée", "Obésité sévère", "Obésité morbide"};
    final private int NBIMC = 6;
    final private int NBCORPULENCE = 7;
    
    /**
     * Creates new form PoidsIdeal
     */
    public PoidsIdeal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupSexe = new javax.swing.ButtonGroup();
        jPanelInformations = new javax.swing.JPanel();
        jLabelAge = new javax.swing.JLabel();
        jLabelNom = new javax.swing.JLabel();
        jLabelPoids = new javax.swing.JLabel();
        jLabelPrenom = new javax.swing.JLabel();
        jLabelSexe = new javax.swing.JLabel();
        jLabelTaille = new javax.swing.JLabel();
        jTextFieldNom = new javax.swing.JTextField();
        jTextFieldPrenom = new javax.swing.JTextField();
        jSpinnerAge = new javax.swing.JSpinner();
        jSpinnerPoids = new javax.swing.JSpinner();
        jSpinnerTaille = new javax.swing.JSpinner();
        jPanelSexe = new javax.swing.JPanel();
        jRadioFemme = new javax.swing.JRadioButton();
        jRadioHomme = new javax.swing.JRadioButton();
        jButtonSuite = new javax.swing.JButton();
        jPanelPoidsIdeal = new javax.swing.JPanel();
        jButtonDevine = new javax.swing.JButton();
        jButtonLorentz = new javax.swing.JButton();
        jButtonLorentzAge = new javax.swing.JButton();
        jTextAreaAfficheur = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(530, 620));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabelAge.setText("Age");

        jLabelNom.setText("Nom");

        jLabelPoids.setText("Poids");

        jLabelPrenom.setText("Prénom");

        jLabelSexe.setText("Sexe");

        jLabelTaille.setText("Taille");

        jSpinnerAge.setModel(new javax.swing.SpinnerNumberModel(1, 1, 200, 1));

        jSpinnerPoids.setModel(new javax.swing.SpinnerNumberModel(15.0d, 15.0d, 300.0d, 0.10000000149011612d));

        jSpinnerTaille.setModel(new javax.swing.SpinnerNumberModel(0.5d, 0.5d, 3.0d, 0.009999999776482582d));

        buttonGroupSexe.add(jRadioFemme);
        jRadioFemme.setText("Femme");

        buttonGroupSexe.add(jRadioHomme);
        jRadioHomme.setText("Homme");

        javax.swing.GroupLayout jPanelSexeLayout = new javax.swing.GroupLayout(jPanelSexe);
        jPanelSexe.setLayout(jPanelSexeLayout);
        jPanelSexeLayout.setHorizontalGroup(
            jPanelSexeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSexeLayout.createSequentialGroup()
                .addComponent(jRadioFemme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioHomme)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanelSexeLayout.setVerticalGroup(
            jPanelSexeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSexeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioFemme)
                .addComponent(jRadioHomme))
        );

        javax.swing.GroupLayout jPanelInformationsLayout = new javax.swing.GroupLayout(jPanelInformations);
        jPanelInformations.setLayout(jPanelInformationsLayout);
        jPanelInformationsLayout.setHorizontalGroup(
            jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInformationsLayout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelTaille)
                    .addComponent(jLabelPrenom)
                    .addComponent(jLabelNom)
                    .addComponent(jLabelAge)
                    .addComponent(jLabelPoids)
                    .addComponent(jLabelSexe))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSpinnerPoids, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                    .addComponent(jTextFieldPrenom, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                    .addComponent(jPanelSexe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSpinnerAge)
                    .addComponent(jSpinnerTaille)))
        );
        jPanelInformationsLayout.setVerticalGroup(
            jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInformationsLayout.createSequentialGroup()
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelInformationsLayout.createSequentialGroup()
                        .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelNom)
                            .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelPrenom)
                            .addComponent(jTextFieldPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanelSexe, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelSexe))
                .addGap(14, 14, 14)
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinnerAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelAge))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinnerPoids, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPoids))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelInformationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinnerTaille, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelTaille))
                .addGap(0, 24, Short.MAX_VALUE))
        );

        getContentPane().add(jPanelInformations);
        jPanelInformations.setBounds(40, 30, 280, 210);

        jButtonSuite.setText("Suite");
        jButtonSuite.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSuiteMouseClicked(evt);
            }
        });
        getContentPane().add(jButtonSuite);
        jButtonSuite.setBounds(110, 240, 94, 25);

        jButtonDevine.setText("Poids idéal Devine");
        jButtonDevine.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDevineMouseClicked(evt);
            }
        });

        jButtonLorentz.setText("Poids idéal Lorentz");
        jButtonLorentz.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonLorentzMouseClicked(evt);
            }
        });

        jButtonLorentzAge.setText("Poids idéal lorentz / age");
        jButtonLorentzAge.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonLorentzAgeMouseClicked(evt);
            }
        });

        jTextAreaAfficheur.setColumns(20);
        jTextAreaAfficheur.setRows(5);

        javax.swing.GroupLayout jPanelPoidsIdealLayout = new javax.swing.GroupLayout(jPanelPoidsIdeal);
        jPanelPoidsIdeal.setLayout(jPanelPoidsIdealLayout);
        jPanelPoidsIdealLayout.setHorizontalGroup(
            jPanelPoidsIdealLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPoidsIdealLayout.createSequentialGroup()
                .addGroup(jPanelPoidsIdealLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPoidsIdealLayout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(jPanelPoidsIdealLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonLorentzAge)
                            .addComponent(jButtonLorentz)
                            .addComponent(jButtonDevine)))
                    .addGroup(jPanelPoidsIdealLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextAreaAfficheur, javax.swing.GroupLayout.PREFERRED_SIZE, 503, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanelPoidsIdealLayout.setVerticalGroup(
            jPanelPoidsIdealLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPoidsIdealLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jButtonDevine)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLorentz)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLorentzAge)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextAreaAfficheur, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanelPoidsIdeal);
        jPanelPoidsIdeal.setBounds(0, 290, 530, 330);
        jPanelPoidsIdeal.setVisible(false);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSuiteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSuiteMouseClicked

        nom = jTextFieldNom.getText();
        prenom = jTextFieldPrenom.getText();
        age = (int)jSpinnerAge.getValue();
        poids = (double)jSpinnerPoids.getValue();
        taille = (double)jSpinnerTaille.getValue();
        imc = poids/ (taille * taille);
        if(jPanelPoidsIdeal.isVisible() != true)
        {
            jPanelPoidsIdeal.setVisible(true);
            jPanelInformations.setVisible(false);
        }
        else
        {
            jPanelPoidsIdeal.setVisible(false);
            jPanelInformations.setVisible(true);
        }
        jTextAreaAfficheur.setText("Bonjour " + prenom + " " + nom + "\n" + "Votre indice de masse corporel est de : " + imc);
        
        // recherche de la corpulence de l'individu en fonction de son imc
        int indiceCorpulence = 0;
        for (int i = 0; i < NBIMC - 1; i++) {
           if (imc > imcs[i] && imc <= imcs[i + 1]) {
              indiceCorpulence = i + 1;
           }
        }

        // cas extreme
        if (imc < 16.5) {
           indiceCorpulence = 0;
        }
        
        if (imc > 40) {
            indiceCorpulence = NBCORPULENCE - 1;
        }
        
        jTextAreaAfficheur.append("\nVotre corpulence est qualifiée de " + corpulences[indiceCorpulence]);
    }//GEN-LAST:event_jButtonSuiteMouseClicked

    private void jButtonDevineMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDevineMouseClicked
        
        double poidsIdeal = 0;
        if (jRadioFemme.isSelected()) {
           poidsIdeal = 45.5 + 2.3 * (taille / 0.0254 - 60.0);
        }
        if (jRadioHomme.isSelected()) {
           poidsIdeal = 50.0 + 2.3 * (taille / 0.0254 - 60.0);
        }
        if (poidsIdeal > 0) {
           jTextAreaAfficheur.append("\nVotre poids ideal avec la formule de devine : " + ((Double) poidsIdeal).toString() + " kg\n");
           double ecart = poids - poidsIdeal;
           if (ecart >= 0) {
              jTextAreaAfficheur.append("\nVous devez perdre " + ((Double) ecart).toString() + " kg\n");
           } 
           else {
              jTextAreaAfficheur.append("\nVous devez prendre " + ((Double) (-ecart)).toString() + " kg\n");
           }
        }
        
    }//GEN-LAST:event_jButtonDevineMouseClicked

    private void jButtonLorentzMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonLorentzMouseClicked
        
        double poidsIdeal = 0;
        if (jRadioFemme.isSelected()) {
            poidsIdeal = taille * 100.0 - 100.0 - ((taille * 100.0 - 150.0) / 2.5);
        }
        if (jRadioHomme.isSelected()) {
           poidsIdeal = taille * 100.0 - 100.0 - ((taille * 100.0 - 150.0) / 4);
        }
        if (poidsIdeal > 0) {
           jTextAreaAfficheur.append("\nVotre poids ideal avec la formule de lorrentz : " + ((Double) poidsIdeal).toString() + " kg\n");
           double ecart = poids - poidsIdeal;
           if (ecart >= 0) {
              jTextAreaAfficheur.append("\nVous devez perdre " + ((Double) ecart).toString() + " kg\n");
           } 
           else {
              jTextAreaAfficheur.append("\nVous devez prendre " + ((Double) (-ecart)).toString() + " kg\n");
           }
        }
    }//GEN-LAST:event_jButtonLorentzMouseClicked

    private void jButtonLorentzAgeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonLorentzAgeMouseClicked
        
        double poidsIdeal = 0;
        poidsIdeal = 50 + ((taille * 100 - 150) / 4) + ((age - 20) / 4);
        if (poidsIdeal > 0) {
           jTextAreaAfficheur.append("\nVotre poids ideal avec la formule de lorrentz/age : " + ((Double) poidsIdeal).toString() + " kg\n");
           double ecart = poids - poidsIdeal;
           if (ecart >= 0) {
              jTextAreaAfficheur.append("\nVous devez perdre " + ((Double) ecart).toString() + " kg\n");
           } 
           else {
              jTextAreaAfficheur.append("\nVous devez prendre " + ((Double) (-ecart)).toString() + " kg\n");
           }
        }
        
    }//GEN-LAST:event_jButtonLorentzAgeMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PoidsIdeal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PoidsIdeal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PoidsIdeal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PoidsIdeal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PoidsIdeal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupSexe;
    private javax.swing.JButton jButtonDevine;
    private javax.swing.JButton jButtonLorentz;
    private javax.swing.JButton jButtonLorentzAge;
    private javax.swing.JButton jButtonSuite;
    private javax.swing.JLabel jLabelAge;
    private javax.swing.JLabel jLabelNom;
    private javax.swing.JLabel jLabelPoids;
    private javax.swing.JLabel jLabelPrenom;
    private javax.swing.JLabel jLabelSexe;
    private javax.swing.JLabel jLabelTaille;
    private javax.swing.JPanel jPanelInformations;
    private javax.swing.JPanel jPanelPoidsIdeal;
    private javax.swing.JPanel jPanelSexe;
    private javax.swing.JRadioButton jRadioFemme;
    private javax.swing.JRadioButton jRadioHomme;
    private javax.swing.JSpinner jSpinnerAge;
    private javax.swing.JSpinner jSpinnerPoids;
    private javax.swing.JSpinner jSpinnerTaille;
    private javax.swing.JTextArea jTextAreaAfficheur;
    private javax.swing.JTextField jTextFieldNom;
    private javax.swing.JTextField jTextFieldPrenom;
    // End of variables declaration//GEN-END:variables
}
