Java İdeal Kilo ve Vücut Kitle

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Calistir extends JFrame implements ActionListener,ListSelectionListener{
    DefaultListModel<String>[] dlm;
    DefaultListModel<Double>[] degerler;
    DefaultListModel<Integer> id;
    DefaultListModel<String> durumlar;
    JRadioButton[] rbtn;
    JList<String>[] jlist;
    JTextField[] jtext;
    DefaultTableModel dtm; 
    JTable jtbl;
    String[] btn_isim= {"Ekle","Sil","Güncelle"};
    String[] jlb_text= {"İsim","Soy İsim","Boy","Kilo"};
    String[] column= {"İsim","Soy İsim","Vücut Endeksi","Durum","İdeal Kilo"};
    String[] cinsiyet= {"Erkek","Kadın"};
    ButtonGroup bg=new ButtonGroup();
   Connection baglanti;
    public Calistir() {
		this.setTitle("Vücut kitle Endeksi İdeal Kilo");
		this.setSize(1200, 650);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        
        JPanel main_jpn=new JPanel();
        main_jpn.setPreferredSize(new Dimension(1200,650));
        main_jpn.setLayout(new FlowLayout());
        
        degerler=new DefaultListModel[2];
        id=new DefaultListModel<Integer>();
        durumlar=new DefaultListModel<String>();
        dlm=new DefaultListModel[jlb_text.length];
        jlist=new JList[dlm.length];
        jtext=new JTextField[dlm.length];
        JPanel[] jpn= {new JPanel(),new JPanel()};
        jpn[0].setPreferredSize(new Dimension(110*dlm.length+75,650));
        jpn[1].setPreferredSize(new Dimension(1100-(125*dlm.length),650));
       
 
        main_jpn.add(jpn[0]);
        main_jpn.add(jpn[1]);
       
        dtm=new DefaultTableModel();
        jtbl=new JTable(dtm);
        
        rbtn=new JRadioButton[2];
        dtm.setColumnIdentifiers(column);
        JScrollPane tscp=new JScrollPane(jtbl);
        tscp.setPreferredSize(new Dimension(1100-(125*dlm.length),450));
        jpn[1].add(tscp);
        Dimension boyut=new Dimension(100, 25);
        for(int i=0;i<dlm.length;i++) {
        	JPanel jpn_list=new JPanel();
        	jpn_list.setPreferredSize(new Dimension(100, 650));
     
        	JLabel jlb=new JLabel(jlb_text[i]);
        	jlb.setPreferredSize(boyut);
        	dlm[i]=new DefaultListModel<String>();
            jlist[i]=new JList<String>(dlm[i]);
            jlist[i].addListSelectionListener(this);
        	JScrollPane scp=new JScrollPane(jlist[i]);
        	scp.setPreferredSize(new Dimension(100,350));
        	jpn_list.add(jlb);
        	jpn_list.add(scp);
        	jtext[i]=new JTextField();
        	jtext[i].setPreferredSize(boyut);
        	jpn_list.add(jtext[i]);
        	if(i<btn_isim.length) {
                 JButton btn=new JButton(btn_isim[i]);
                 btn.setPreferredSize(new Dimension(100,33));
                 btn.addActionListener(this);
                 btn.setActionCommand(btn_isim[i]);
               jpn_list.add(btn);        
        	}
        	if(i==dlm.length-1) {
        		for(int j=0;j<rbtn.length;j++) {
        			rbtn[j]=new JRadioButton(cinsiyet[j]);
        			rbtn[j].setPreferredSize(new Dimension(75, 15));
        			rbtn[0].setSelected(true);
        		    bg.add(rbtn[j]);
        		    jpn_list.add(rbtn[j]);
        	        degerler[j]=new DefaultListModel<Double>();

        		}
        	}
          jpn[0].add(jpn_list);
        }
        degerdoldur();
        listdoldur();
        this.add(main_jpn);
        this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		switch(arg0.getActionCommand()) {
		case "Ekle":ekle();break;
		case "Sil":sil();break;
		case "Güncelle":guncelle();break;
		}
	}
	public boolean dogrula()
	{
		int sayac=0;
		for(int i=0;i<jtext.length;i++)
 			    if(jtext[i].getText().length()>0)
                         sayac++;		
	 if(sayac==jtext.length)
		 return true;
	 else
		 return false;
				 
	}
public void ekle() {
  try {
	  double boy=Double.parseDouble(jtext[2].getText());
	  double kilo=Double.parseDouble(jtext[3].getText());
	 if(dogrula()) {
	  baglan();
	  String cins=rbtn[1].getText();
	  if(rbtn[0].isSelected())
		  cins=rbtn[0].getText();
	PreparedStatement sorgu=baglanti.prepareStatement("insert into Kisiler(Ad,SoyAd,Boy,Kilo,Cinsiyet) values(?,?,?,?,?)");
	 sorgu.setString(1, jtext[0].getText());
	 sorgu.setString(2, jtext[1].getText());
	 sorgu.setDouble(3, boy);
	 sorgu.setDouble(4, kilo);
	 sorgu.setString(5,cins);
	 sorgu.executeUpdate();
	 baglanti.close();
	 listdoldur();
	 for(int i=0;i<jtext.length;i++)
		 jtext[i].setText("");
	 }
	 rbtn[0].setSelected(true);
  } catch (Exception e) {
	  JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());	
	}
  }	

public void sil() {
	int secilen=jlist[0].getSelectedIndex();
	try {
		if(secilen>-1)
		{   baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("delete from Kisiler where ID=?");
			sorgu.setInt(1, id.getElementAt(secilen));
			sorgu.executeUpdate();
			baglanti.close();
			listdoldur();
		}
	} catch (Exception e) {
		 JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());
		 }
	
}
public void guncelle() {
	int secilen=jlist[0].getSelectedIndex();
	try {
		double boy=Double.parseDouble(jtext[2].getText());
		double kilo=Double.parseDouble(jtext[3].getText());
		if(secilen>-1&&dogrula()) {
			 String cins=rbtn[0].getText();
			  if(rbtn[1].isSelected())
				  cins=rbtn[1].getText();
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("update Kisiler set Ad=?,SoyAd=?,Boy=?,Kilo=?,Cinsiyet=? where ID=?");
			sorgu.setString(1, jtext[0].getText());
			sorgu.setString(2, jtext[1].getText());
			sorgu.setDouble(3, boy);
			sorgu.setDouble(4, kilo);
			sorgu.setString(5, cins);
			sorgu.setInt(6, id.getElementAt(secilen));
			sorgu.executeUpdate();
		   baglanti.close();
		   for(int i=0;i<jtext.length;i++)
		       jtext[i].setText("");
		  listdoldur();
		  rbtn[0].setSelected(true);
		}
		
	} catch (Exception e) {
		 JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());	
	}
}
	@Override
	public void valueChanged(ListSelectionEvent e) {
		JList secilen=(JList)e.getSource();
		for(int i=0;i<jlist.length;i++)
			jlist[i].setSelectedIndex(secilen.getSelectedIndex());
	}
public static void main(String[] arg) {
	new Calistir();
}
public void degerdoldur() {
	try {
		 
	     baglan();
		PreparedStatement sorgu=baglanti.prepareStatement("select Baslangic,Bitis,Durum from Degerler");
	    ResultSet veri=sorgu.executeQuery();
	    while(veri.next()) {
	    	degerler[0].addElement(veri.getDouble("Baslangic"));
	    	degerler[1].addElement(veri.getDouble("Bitis"));
	    	durumlar.addElement(veri.getString("Durum"));
	    }
	    	veri.close();
	    	baglanti.close();
	} catch (Exception e) {
		 JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());	
		 }
}
public String durumbul(double endeks)
{   String durum="Aşırı Şişman";
	for(int i=0;i<degerler[0].size();i++)
	{
		 double bas=degerler[0].getElementAt(i),bitis=degerler[1].getElementAt(i);
		if(endeks>=bas&&endeks<=bitis)
		{
			 durum=durumlar.getElementAt(i);
			 break;
		}
	}

return durum;
}
public double virgul2(double sayi) {
	int s=(int)(sayi*100);
	sayi=s/100.0;
	return sayi;
}
public double idealkilo(double boy,String cins)
{
   double[] dgr= {45.5,2.3,60};
   double erkek=50;
   double sonuc=0;	
    if(cins.equals("Erkek"))
            dgr[0]=erkek;
    sonuc=dgr[0]+(dgr[1]*(boy/0.0254-dgr[2]));
return virgul2(sonuc);
}
public void baglan() {
	try {
		baglanti=DriverManager.getConnection("jdbc:sqlite:Vucut Endeksi.db");
	} catch (Exception e) {
	  JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());
	}
}
public void listdoldur() {

	try {
		baglan();
		  dtm.getDataVector().removeAllElements();//Table tamamen temizlemek için
		  dtm.fireTableDataChanged();
	   PreparedStatement sorgu=baglanti.prepareStatement("select ID,Ad,SoyAd,Boy,Kilo,Cinsiyet from Kisiler");
	   ResultSet veri=sorgu.executeQuery();
	   id.clear();
	   for(int i=0;i<dlm.length;i++)
		   dlm[i].clear();
	while(veri.next()) {
	   id.addElement(veri.getInt("ID"));
	   String ad=veri.getString("Ad");
	   String adsoy=veri.getString("SoyAd");
	    double boy=veri.getDouble("Boy");
	    double kilo=veri.getDouble("Kilo");
	    dlm[0].addElement(ad);
	    dlm[1].addElement(adsoy);
	    dlm[2].addElement(""+boy);
	    dlm[3].addElement(""+kilo);
	    double endeks=virgul2(kilo/((boy)*boy));
	    double idkilo=idealkilo(boy,veri.getString("Cinsiyet"));
	    String drm=durumbul(endeks);
	    String[] rows= {ad,adsoy,""+endeks,drm,""+idkilo};
	    dtm.addRow(rows);
	}
	}
	catch(Exception e)
	{
		 JOptionPane.showMessageDialog(this, "Hata="+e.getMessage());
	}
}
}

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir