Ders programı uygulaması indir

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class Calistir extends JFrame implements ActionListener,ListSelectionListener,ItemListener {
	 DefaultListModel<String>[] dlm;
     JList<String>[] jlist;
     String[] lbl= {"Ders","Hoca","Gün","Saat","Teorik","Uygulama"};
     JComboBox<String> jcb;
     JComboBox<Integer> jcb_id;
     JComboBox<Integer> jcb_orgun;
	public Calistir()  {
		this.setTitle("Ders Programı");
		this.setSize(700,450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		String[] btn_txt={"Ders","Hoca","Eşleştirme"};
		JPanel jpn_main=new JPanel();
		jpn_main.setPreferredSize(new Dimension(700, 450));
		
		dlm=new DefaultListModel[lbl.length];
		jlist=new JList[dlm.length];
		
		
		for(int i=0;i<dlm.length;i++) {
			JPanel jpn=new JPanel();
			jpn.setPreferredSize(new Dimension(100, 400));
			
			JLabel jlb=new JLabel(lbl[i]);
			jlb.setPreferredSize(new Dimension(100,25));
			
			dlm[i]=new DefaultListModel<String>();
			jlist[i]=new JList<String>(dlm[i]);
			jlist[i].addListSelectionListener(this);
			JScrollPane jscp=new JScrollPane(jlist[i]);
		    jscp.setPreferredSize(new Dimension(100, 300));
		    
		    jpn.add(jlb);
		    jpn.add(jscp);
		  
		    jpn_main.add(jpn);
		    if(i!=0&&(i-1)<btn_txt.length){
		    	JButton btn=new JButton(btn_txt[i-1]);
		    	btn.setPreferredSize(new Dimension(100,25));
		    	btn.addActionListener(this);
		    	btn.setActionCommand(btn_txt[i-1]);	
		    	jpn.add(btn);
		    }
		    if(i==0) {
		       jcb=new JComboBox<String>();
		       jcb.setSelectedIndex(-1);
		       jcb.setPreferredSize(new Dimension(100, 25));
		       jcb.addItemListener(this);
		       jcb_id=new JComboBox<Integer>(); 
		       jcb_orgun=new JComboBox<Integer>();
		       jpn.add(jcb);   
		    }
		}
		combodoldur();
		if(jcb_id.getItemCount()==0)
		listdoldur(0,0);
		else
		listdoldur(jcb_id.getItemAt(0),jcb_orgun.getItemAt(0));
		this.add(jpn_main);
	   this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
	
		switch(arg0.getActionCommand()) {
		
		case "Ders":new Ders(this);break;
		case "Hoca":new Hoca(this);break;
		case "Eşleştirme":new Eslestirme(this);break;
		}
	}
 public static void main(String[] arg) {
	 
	 new Calistir();
 }
@Override
public void valueChanged(ListSelectionEvent arg0) {
	JList secilen=(JList)arg0.getSource();
	for(int i=0;i<jlist.length;i++)
		jlist[i].setSelectedIndex(secilen.getSelectedIndex());
}
Connection baglanti;
public void baglan() {
	try {
			baglanti=DriverManager.getConnection("jdbc:sqlite:Ders Programi.db");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	
}
public void combodoldur() {
	try {
		baglan();
		PreparedStatement sorgu=baglanti.prepareStatement("select distinct sinif,orgun from dersler");
		ResultSet veri=sorgu.executeQuery();
		jcb.removeAllItems();
	    jcb_id.removeAllItems();
	    jcb_orgun.removeAllItems();
		while(veri.next()) {
			int sinif=veri.getInt("sinif");
			int orgun=veri.getInt("orgun");
			jcb.addItem(sinif+".Sınıf"+"("+orgun+".Örgün"+")");
			jcb_id.addItem(sinif);
			jcb_orgun.addItem(orgun);
			
		}
	} catch (Exception e) {
		JOptionPane.showMessageDialog(this, e.getMessage()+"dsaasds");
	}
	
}
public void listdoldur(int sinif,int orgun) {
	try {
			baglan();
			 String sql="select d.teoriksaati as 'teorik', d.uygsaati as 'uyg', d.sinif as 'sinif',d.orgun as 'orgun',h.hocaismi as 'hoca',d.dersismi as 'ders',s.saad as 'saat',g.isim as 'gun' ,e.tip as 'tip' from eslestirme e inner join hoca h on h.id=e.hocaid inner join gun g on g.id=e.gunid inner join Saat s on s.id=e.saatid	inner join dersler d on d.id=e.dersid 	and e.dersid=d.id  where sinif=? and orgun=?" ;
			 		
			PreparedStatement sorgu=baglanti.prepareStatement(sql);
			sorgu.setInt(1,sinif);
			sorgu.setInt(2, orgun);
			ResultSet veri=sorgu.executeQuery();
		   for(int i=0;i<dlm.length;i++)
			   dlm[i].clear();
			while(veri.next()) {
				dlm[0].addElement(veri.getString("ders"));
				dlm[1].addElement(veri.getString("hoca"));
				dlm[2].addElement(veri.getString("gun"));
				dlm[3].addElement(veri.getString("saat"));
				dlm[4].addElement(veri.getString("teorik"));
				dlm[5].addElement(veri.getString("uyg"));
				
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
}
@Override
public void itemStateChanged(ItemEvent arg0) {
	
          if(arg0.getStateChange()==2) {
	int secilen=jcb.getSelectedIndex();	
	if(secilen>-1) {
	listdoldur(jcb_id.getItemAt(secilen),jcb_orgun.getItemAt(secilen));

}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class Ders extends JFrame implements ActionListener,ListSelectionListener,WindowListener{
      DefaultListModel<String>[] dlm;
      JList<String>[] jlist;
      String[] lbl= {"Ders Ad","Kodu","Teorik Saati","Uyg Saati","Sınıf","Örgün"};
	  JTextField[] jtext;
	  Calistir frm;
      public Ders(Calistir frm) {
    	 this.frm=frm;
    	 frm.setVisible(false);
		this.setTitle("Ders");
		this.setSize(700,450);
		this.addWindowListener(this);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main=new JPanel();
		jpn_main.setPreferredSize(new Dimension(700, 450));
	   
		dlm=new DefaultListModel[lbl.length];
		jlist=new JList[dlm.length];
		jtext=new JTextField[jlist.length];
		
		for(int i=0;i<dlm.length;i++) {
			JPanel jpn=new JPanel();
			jpn.setPreferredSize(new Dimension(100, 450));
			
			JLabel jlb=new JLabel(lbl[i]);
			jlb.setPreferredSize(new Dimension(100,25));
			
			dlm[i]=new DefaultListModel<String>();
			jlist[i]=new JList<String>(dlm[i]);
			jlist[i].addListSelectionListener(this);
			JScrollPane jscp=new JScrollPane(jlist[i]);
		    jscp.setPreferredSize(new Dimension(100, 300));
		    
		    jtext[i]=new JTextField();
		    jtext[i].setPreferredSize(new Dimension(100, 25));
		    
		    jpn.add(jlb);
		    jpn.add(jscp);
		    jpn.add(jtext[i]);
		    if(i==0) {
		    JButton btn=new JButton("Ekle");
		    btn.setPreferredSize(new Dimension(100, 25));
		    btn.addActionListener(this);
		    jpn.add(btn);	
		    }
		    jpn_main.add(jpn);
		}
		listdoldur();
		this.add(jpn_main);
		this.setVisible(true);
	}
   public boolean kontrol() {
	   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;
   }
   Connection baglanti;
   public void baglan() {
   	try {
			baglanti=DriverManager.getConnection("jdbc:sqlite:Ders Programi.db");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
   	
   }
   public void listdoldur() {
   	try {
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("select * from dersler");
			ResultSet veri=sorgu.executeQuery();
		   for(int i=0;i<dlm.length;i++)
			   dlm[i].clear();
			while(veri.next()) {
				dlm[0].addElement(veri.getString("dersismi"));
				dlm[1].addElement(veri.getString("kodu"));
				dlm[2].addElement(""+veri.getInt("teoriksaati"));
				dlm[3].addElement(""+veri.getInt("uygsaati"));
				dlm[4].addElement(""+veri.getInt("sinif"));
				dlm[5].addElement(""+veri.getInt("orgun"));
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
   }
   public boolean kontrol_ders() {
	   
	   for(int i=0;i<dlm[0].getSize();i++) {
		   if(dlm[0].getElementAt(i).equals(jtext[0].getText())&&dlm[4].getElementAt(i).equals(jtext[4].getText())&&dlm[5].getElementAt(i).equals(jtext[5].getText()))
	           return false;
	   }
   return true;}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(kontrol()&&kontrol_ders()) {
		   
			try {
				 int teoriks=Integer.parseInt(jtext[2].getText());
				 int uygs=Integer.parseInt(jtext[3].getText());
				 int sinif=Integer.parseInt(jtext[4].getText());
				 int orgun=Integer.parseInt(jtext[5].getText());
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("insert into dersler(dersismi,kodu,teoriksaati,uygsaati,sinif,orgun) values(?,?,?,?,?,?)");
		    sorgu.setString(1, jtext[0].getText());
		    sorgu.setString(2, jtext[1].getText());
		    sorgu.setInt(3, teoriks);
		    sorgu.setInt(4, uygs);
		    sorgu.setInt(5, sinif);
		    sorgu.setInt(6, orgun);
		    sorgu.executeUpdate();
		    for(int i=0;i<jtext.length;i++)
		    	jtext[i].setText("");
		    listdoldur();
		    frm.combodoldur();
			} catch (Exception e2) {
			JOptionPane.showMessageDialog(this, e2.getMessage());
		}
		}
	}
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		JList secilen=(JList)arg0.getSource();
		for(int i=0;i<jlist.length;i++)
			jlist[i].setSelectedIndex(secilen.getSelectedIndex());
		
	}
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosing(WindowEvent arg0) {
		frm.setVisible(true);
		
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class Eslestirme extends JFrame implements ActionListener,ListSelectionListener,WindowListener {
	 DefaultListModel<String>[] dlm;
	 DefaultListModel<Integer>[] dlm_id;
	 DefaultListModel<Integer> dlm_teouyg;
	 DefaultListModel<Integer> dlm_sinifid;
     JList<String>[] jlist;
     String[] lbl= {"Ders","Hoca","Saat","Gün","Tip"};
     JComboBox<String>[] jcb;
     JComboBox<Integer>[] jcb_id;
     Calistir frm;
	public Eslestirme(Calistir frm) {
		 this.frm=frm;
    	 frm.setVisible(false);
		this.setTitle("Eslestirme");
		this.setSize(600,450);
		this.addWindowListener(this);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main=new JPanel();
		jpn_main.setPreferredSize(new Dimension(600, 450));
	   
		dlm_teouyg=new DefaultListModel<Integer>();
		dlm_sinifid=new DefaultListModel<Integer>();
		dlm=new DefaultListModel[lbl.length];
		dlm_id=new DefaultListModel[lbl.length];
		jlist=new JList[dlm.length];
		jcb=new JComboBox[jlist.length];
		jcb_id=new JComboBox[jlist.length];
		
		for(int i=0;i<dlm.length;i++) {
			JPanel jpn=new JPanel();
			jpn.setPreferredSize(new Dimension(100, 450));
			
			JLabel jlb=new JLabel(lbl[i]);
			jlb.setPreferredSize(new Dimension(100,25));
			
			dlm[i]=new DefaultListModel<String>();
			dlm_id[i]=new DefaultListModel<Integer>();
			jlist[i]=new JList<String>(dlm[i]);
			jlist[i].addListSelectionListener(this);
			JScrollPane jscp=new JScrollPane(jlist[i]);
		    jscp.setPreferredSize(new Dimension(100, 300));
		    
		    jcb[i]=new JComboBox<String>();
		    jcb[i].setPreferredSize(new Dimension(100, 25));
		    jcb_id[i]=new JComboBox<Integer>();
		    
		    jpn.add(jlb);
		    jpn.add(jscp);
		    jpn.add(jcb[i]); 
		    if(i==0) {
		    JButton btn=new JButton("Ekle");
		    btn.setPreferredSize(new Dimension(100, 25));
		    btn.addActionListener(this);
		    jpn.add(btn);	
		    }
		    jpn_main.add(jpn);
		}
		combodoldur();
		listdoldur();
		this.add(jpn_main);
	   this.setVisible(true);
	}
	Connection baglanti;
    public void baglan() {
    	try {
			baglanti=DriverManager.getConnection("jdbc:sqlite:Ders Programi.db");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
    	
    }
    public void combodoldur() {
    	try {
    		PreparedStatement sorgu=null;
    		ResultSet veri=null;
			baglan();
			sorgu=baglanti.prepareStatement("select id,dersismi,sinif,orgun,teoriksaati,uygsaati from dersler");
		    veri=sorgu.executeQuery();
			jcb[0].removeAllItems();
			jcb_id[0].removeAllItems();
			dlm_teouyg.clear();
			dlm_sinifid.clear();
			while(veri.next()) {
				int sinif_id=veri.getInt("sinif");
				 jcb[0].addItem(veri.getString("dersismi")+"("+sinif_id+"."+veri.getInt("orgun")+"."+"Ögün"+")");
				 jcb_id[0].addItem(veri.getInt("id"));
				 dlm_teouyg.addElement(veri.getInt("teoriksaati")+veri.getInt("uygsaati"));
				 dlm_sinifid.addElement(sinif_id);
			}
			sorgu=baglanti.prepareStatement("select id,hocaismi from hoca");
		    veri=sorgu.executeQuery();
			jcb[1].removeAllItems();
			jcb_id[1].removeAllItems();
			while(veri.next()) {
				 jcb[1].addItem(veri.getString("hocaismi"));
				 jcb_id[1].addItem(veri.getInt("id"));
			}
			sorgu=baglanti.prepareStatement("select id,saad from Saat");
		    veri=sorgu.executeQuery();
			jcb[2].removeAllItems();
			jcb_id[2].removeAllItems();
			while(veri.next()) {
				 jcb[2].addItem(veri.getString("saad"));
				 jcb_id[2].addItem(veri.getInt("id"));	
			}
			sorgu=baglanti.prepareStatement("select id,isim from gun");
		    veri=sorgu.executeQuery();
			jcb[3].removeAllItems();
			jcb_id[3].removeAllItems();
			while(veri.next()) {
				 jcb[3].addItem(veri.getString("isim"));
				 jcb_id[3].addItem(veri.getInt("id"));
			}
			jcb[4].addItem("Teorik");
			jcb[4].addItem("Uygulama");
			jcb_id[4].addItem(1);
			jcb_id[4].addItem(2);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
    }
    public void listdoldur() {
       	try {
    			baglan();
    			PreparedStatement sorgu=baglanti.prepareStatement("select d.sinif as 'sinif',d.orgun as 'orgun',h.id as 'h',s.id as 's',g.id as 'g',d.dersismi as 'ders',h.hocaismi as 'hoca',g.isim as 'gun',s.saad as 'saat' ,e.tip as 'tip' from eslestirme e inner join dersler d on d.id=e.dersid inner join hoca h on h.id=e.hocaid inner join gun g on g.id=e.gunid inner join Saat s on s.id=e.saatid");
    				
    			ResultSet veri=sorgu.executeQuery();
    		   for(int i=0;i<dlm.length;i++)
    			   dlm[i].clear();
    			while(veri.next()) {
    				int sinif_id=veri.getInt("sinif");
    				dlm[0].addElement(veri.getString("ders")+"("+veri.getInt("sinif")+"."+veri.getInt("orgun")+"."+"Ögün"+")");
    				dlm[1].addElement(veri.getString("hoca"));
    				dlm[2].addElement(veri.getString("saat"));
    				dlm[3].addElement(veri.getString("gun"));
    				String tip="Teorik";
    				if(veri.getInt("tip")==2)
    					tip="Uygulama";
    			    dlm[4].addElement(tip);
    			
    			    dlm_id[0].addElement(sinif_id);
    				dlm_id[1].addElement(veri.getInt("h"));
    				dlm_id[2].addElement(veri.getInt("s"));
    				dlm_id[3].addElement(veri.getInt("g"));
    				
    			}
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(this, e.getMessage());
    		}
       }
    public boolean teouyg_kontrol(String ders,int teouyg_saat) {
    	int sayac=0;
    	for(int i=0;i<dlm[0].getSize();i++) {
    		
    		if(dlm[0].getElementAt(i).equals(ders))
    			sayac++;
    	}
    		if(sayac==teouyg_saat)//teouyg_saat adlı dlm de teo+uyg toplamı var
    			return false;
    return true;}
    public boolean kontrol(int sinif_id,int h_id,int s_id,int g_id) {
    	
    	for(int i=0;i<dlm_id[0].size();i++) {
    		
    		//Hoca aynı saatte ve aynı gunde derse giremez
    		if(h_id==dlm_id[1].getElementAt(i)&&s_id==dlm_id[2].getElementAt(i)&&g_id==dlm_id[3].getElementAt(i))
				return false;
    		//Aynı saatte aynı sınıfa baska bir ders olamaz
			if(dlm_id[0].getElementAt(i)==sinif_id&&s_id==dlm_id[2].getElementAt(i)&&g_id==dlm_id[3].getElementAt(i))
				return false;
    			
    	}
   return true;
    }
	@Override
	public void actionPerformed(ActionEvent e) {
		int secilen_d=jcb[0].getSelectedIndex();
		int secilen_h=jcb[1].getSelectedIndex();
		int secilen_s=jcb[2].getSelectedIndex();
		int secilen_g=jcb[3].getSelectedIndex();
		if(secilen_d>-1&&secilen_h>-1&&secilen_s>-1&&secilen_g>-1) {
		 int d_id=jcb_id[0].getItemAt(secilen_d);
		 int h_id=jcb_id[1].getItemAt(secilen_h);
		 int s_id=jcb_id[2].getItemAt(secilen_s);
		 int g_id=jcb_id[3].getItemAt(secilen_g);
		 int sinif_id=dlm_sinifid.getElementAt(secilen_d);
		 if(kontrol(sinif_id,h_id,s_id,g_id)&&teouyg_kontrol(jcb[0].getItemAt(secilen_d), dlm_teouyg.getElementAt(secilen_d))) {
			 int tip=1;
				if(jcb[4].getItemAt(jcb[4].getSelectedIndex())=="Uygulama")
					tip=2;
			try {
				PreparedStatement sorgu=baglanti.prepareStatement("insert into eslestirme(dersid,hocaid,gunid,saatid,tip) values(?,?,?,?,?)");
			     sorgu.setInt(1, d_id);
			     sorgu.setInt(2, h_id);
			     sorgu.setInt(3, g_id);
			     sorgu.setInt(4, s_id);
			     sorgu.setInt(5, tip);
			     sorgu.executeUpdate();
			     listdoldur();
			     frm.combodoldur();
			     frm.listdoldur(frm.jcb_id.getItemAt(0),frm.jcb_orgun.getItemAt(0));
			} catch (Exception e2) {
				JOptionPane.showMessageDialog(this, e2.getMessage());
		      } 	 
		   }
		 else
			 JOptionPane.showMessageDialog(this, "Aynı gün ve aynı saatte ");
		}
	}
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		JList secilen=(JList)arg0.getSource();
		for(int i=0;i<jlist.length;i++)
			jlist[i].setSelectedIndex(secilen.getSelectedIndex());
		
	}
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosing(WindowEvent arg0) {
		frm.setVisible(true);
		
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Hoca extends JFrame implements ActionListener,WindowListener{
      DefaultListModel<String> dlm;
      JList<String> jlist;
      JTextField jtext;
      Calistir frm;
	public Hoca(Calistir frm) {
		 this.frm=frm;
    	 frm.setVisible(false);
		this.setTitle("Hoca");
		this.setSize(150,500);
		this.addWindowListener(this);
		this.setLayout(new FlowLayout());
		
		JPanel jpn=new JPanel();
		jpn.setPreferredSize(new Dimension(150, 500));
        
		dlm=new DefaultListModel<String>();
		jlist=new JList<String>(dlm);
	
		JScrollPane jscp=new JScrollPane(jlist);
		jscp.setPreferredSize(new Dimension(100, 300));
		 
		jtext=new JTextField();
		jtext.setPreferredSize(new Dimension(100, 25));
		
		JButton btn=new JButton("Ekle");
		btn.setPreferredSize(new Dimension(100, 25));
		btn.addActionListener(this);
		JLabel jlb=new JLabel("Hocalar");
		jlb.setPreferredSize(new Dimension(100,25));
	   
		jpn.add(jlb);
		jpn.add(jscp);
		jpn.add(jtext);
		jpn.add(btn);
		listdoldur();
		this.add(jpn);
	    this.setVisible(true);
	}
	Connection baglanti;
    public void baglan() {
    	try {
			baglanti=DriverManager.getConnection("jdbc:sqlite:Ders Programi.db");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
    	
    }
    public void listdoldur() {
    	try {
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("select hocaismi from hoca");
			ResultSet veri=sorgu.executeQuery();
			dlm.clear();
			while(veri.next()) {
				dlm.addElement(veri.getString("hocaismi"));
				
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
    }
	@Override
	public void actionPerformed(ActionEvent arg0) {
	  
		   if(jtext.getText().length()>0) {
    try {
		PreparedStatement sorgu=baglanti.prepareStatement("insert into hoca(hocaismi) values(?)");
		sorgu.setString(1, jtext.getText());
		sorgu.executeUpdate();
		jtext.setText("");
		listdoldur();
	} catch (Exception e) {
		JOptionPane.showMessageDialog(this, e.getMessage());
	}
	}
	}
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowClosing(WindowEvent arg0) {
			frm.setVisible(true);
		
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

Su İhtiyac Hesaplama

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Calistir extends JFrame implements ActionListener, ListSelectionListener {
      String[] jlbl= {"Ad","SoyAd","Kilo","Günlük Su İhtiyacı"};
	  DefaultListModel<String>[] dlm;
	  DefaultListModel<Integer> dlm_id;
	  JList<String>[] jlist;
 	  JTextField[] jtext;
 	  JTextField[] jtext_gun;
 	  JPanel main_jpn;
	  JButton jbtn;
	  String[] btn_text= {"Ekle","Sil","Güncelle"};
	  Connection baglanti=null;
	  JFrame gun_frm;
	  public Calistir() {
		this.setTitle("Su İhtiyacı Hesaplama");
		this.setSize(450, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		dlm=new DefaultListModel[jlbl.length];
		jlist=new JList[dlm.length];
		dlm_id=new DefaultListModel<Integer>();
		jtext=new JTextField[dlm.length-1];
		main_jpn=new JPanel();
		main_jpn.setPreferredSize(new Dimension(450,450));
		for(int i=0;i<dlm.length;i++)
		{
			JPanel jpn=new JPanel();
			jpn.setPreferredSize(new Dimension(100,450));
			jpn.setLayout(new FlowLayout());
		    JLabel jlab=new JLabel(jlbl[i]);
		    jlab.setPreferredSize(new Dimension(100,20));
			dlm[i]=new DefaultListModel<String>();
		    jlist[i]=new JList<String>(dlm[i]);
		    jlist[i].addListSelectionListener(this);
           JScrollPane jscp=new JScrollPane(jlist[i]);
           jscp.setPreferredSize(new Dimension(100,300));
           jpn.add(jlab);
           jpn.add(jscp);
            if(i<jtext.length)
            {
            	jtext[i]=new JTextField();
            	jtext[i].setPreferredSize(new Dimension(100,25));
            	jbtn=new JButton(btn_text[i]);
            	jbtn.setPreferredSize(new Dimension(100,25));
            	jbtn.addActionListener(this);
            	jbtn.setActionCommand(btn_text[i]);
            	jpn.add(jtext[i]);
            	jpn.add(jbtn);
            }
           main_jpn.add(jpn);
		}
		verigetir();
		this.add(main_jpn);
		this.setVisible(true);
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {//Herhangi jlistler tıklanırsa tıklansın bu fonksiyon çalısır
		  JList jsecilen=(JList)arg0.getSource();
		  for(int i=0;i<jlist.length;i++)
			  jlist[i].setSelectedIndex(jsecilen.getSelectedIndex());
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
        int secilen=jlist[0].getSelectedIndex();
		switch(arg0.getActionCommand())
		{
		case "Ekle":Ekle();break; 
		case "Sil":Sil();break; 
		case "Güncelle":Guncelle();break; 
		case "guncelle":guncelle(secilen);break;
		case "cıkıs":cıkıs();break;
		}
	}
	public double sayicevir(String s)
	{   
		double sayi=-1;
		try {
			sayi=Double.parseDouble(s);
			return sayi;
		}catch(Exception ex)
		{
			return sayi;
		}	
	}
	public boolean dogrula(JTextField[] txt)
	{
		int sayac=0;
		for(int i=0;i<txt.length;i++)
			if(txt[i].getText().length()>0)
				  sayac++;
		  if(sayac==txt.length)
			  return true;
	         
	return false;
		  }
public void Ekle() {
	double kilo=sayicevir(jtext[2].getText());
	if(dogrula(jtext)&&kilo!=-1) {
		 double ihtiyac=0.032*kilo;
		    int ihtiyac1=(int)(ihtiyac*100);
		    ihtiyac=ihtiyac1/100.0;
		try {
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("insert into Sahıslar(S_Ad,S_SoyAd,S_Kilo,S_Suihtiyac) values(?,?,?,?)");
			sorgu.setString(1, jtext[0].getText());
			sorgu.setString(2, jtext[1].getText());
			sorgu.setDouble(3, kilo);
			sorgu.setDouble(4, ihtiyac);
			sorgu.executeUpdate();
			baglanti.close();
			for(int i=0;i<jtext.length;i++)
				    jtext[i].setText("");
			verigetir();
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.getMessage());
		}
	}
}
public void Sil() {
 int secilen=jlist[0].getSelectedIndex();
 if(secilen>-1)
 {
	 try {
		 baglan();
		 PreparedStatement sorgu=baglanti.prepareStatement("delete from Sahıslar where S_No=?");
		 sorgu.setInt(1, dlm_id.getElementAt(secilen)); 
		 sorgu.executeUpdate();
		 baglanti.close();
		 verigetir();
	 }
	 catch(Exception ex)
	 {
		 JOptionPane.showMessageDialog(this, ex.getMessage());
	 }
 }
	
}	
public void Guncelle() {
 int secilen=jlist[0].getSelectedIndex();
 
	if(secilen>-1)
	{   
		this.setVisible(false);
		gun_frm(dlm,secilen);	
	}
}
public void guncelle(int secilen)
{   double kilo=sayicevir(jtext_gun[2].getText());
	if(dogrula(jtext_gun)&&kilo!=-1&&secilen>-1)
	{
		try {
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("update Sahıslar set S_Ad=?,S_SoyAd=?,S_Kilo=?,S_Suihtiyac=? where S_No=?");
			sorgu.setString(1, jtext_gun[0].getText());
			sorgu.setString(2, jtext_gun[1].getText());
			sorgu.setDouble(3, kilo);
			double ihtiyac=0.032*kilo;
			int ihtiyac1=(int)(ihtiyac*100);//Virgulden sonra iki basamak alamak için
            sorgu.setDouble(4,ihtiyac1/100.0);
            sorgu.setInt(5, dlm_id.getElementAt(secilen));
            sorgu.executeUpdate();
            baglanti.close();
            verigetir();//buraya kadar hersey yapılırsa jlistleri dolduruyorum
            this.setVisible(true);//Ana formun visible 'nı true(görünür) yapıyorum
            gun_frm.dispose();//ve gun_frm formun yok ediyorum
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.getMessage());
			
		}
		
	}
}
public void cıkıs() {
	this.setVisible(true);
	gun_frm.dispose();
}
public void baglan() {
	
	try {
       Class.forName("org.sqlite.JDBC");
	 baglanti=DriverManager.getConnection("jdbc:sqlite:Suihtiyac.db");	
   }
	catch(Exception ex){
	
		JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
public void verigetir()
{
   try {
	   baglan();
	   Statement sorgu=baglanti.createStatement();
	   ResultSet veri=sorgu.executeQuery("select S_No,S_Ad,S_SoyAd,S_Kilo,S_Suihtiyac from Sahıslar");
	   for(int i=0;i<dlm.length;i++)
		   dlm[i].clear();
	   dlm_id.clear();
	   while(veri.next())
	   {
		   dlm_id.addElement(veri.getInt("S_No"));
		   dlm[0].addElement(veri.getString("S_Ad"));
		   dlm[1].addElement(veri.getString("S_SoyAd"));
		   dlm[2].addElement(veri.getDouble("S_Kilo")+"");
		   dlm[3].addElement(veri.getDouble("S_Suihtiyac")+" Lt"); 
	   }
	   veri.close();
	   baglanti.close();
	   
   }catch(Exception ex)	{
	   
	   JOptionPane.showMessageDialog(this, ex.getMessage());
   }

}
public void gun_frm(DefaultListModel<String>[] dlm,int secilen)
{
    gun_frm=new JFrame("Güncelle");
	gun_frm.setSize(250,200);
	gun_frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//X simgesine basıldığında hiç bir sey yapama
	gun_frm.setLayout(new FlowLayout());
    
	JPanel ana_jpn=new JPanel();
	JPanel jpn_lbl=new JPanel();
	JPanel jpn_jtext=new JPanel();
	ana_jpn.setPreferredSize(new Dimension(250,200));
	jpn_lbl.setPreferredSize(new Dimension(40,130));
	jpn_jtext.setPreferredSize(new Dimension(100,130));
	
	jtext_gun=new JTextField[jtext.length];
	for(int i=0;i<jtext_gun.length;i++)
	{
		jtext_gun[i]=new JTextField();
		jtext_gun[i].setPreferredSize(new Dimension(100,20));
	    JLabel jlb_gun= new JLabel(jlbl[i]+":");
	    jlb_gun.setPreferredSize(new Dimension(40,20));
	    jlb_gun.setHorizontalAlignment(JLabel.RIGHT);
	    jpn_lbl.add(jlb_gun);
	    jpn_jtext.add(jtext_gun[i]);
	    jtext_gun[i].setText(dlm[i].getElementAt(secilen));
	}
	jbtn=new JButton("Güncelle");
	jbtn.setPreferredSize(new Dimension(100,20));
	jbtn.addActionListener(this);
	jbtn.setActionCommand("guncelle");
	jpn_jtext.add(jbtn);
	jbtn=new JButton("Çık");
	jbtn.setPreferredSize(new Dimension(100,20));
	jbtn.addActionListener(this);
	jbtn.setActionCommand("cıkıs");
	jpn_jtext.add(jbtn);
	ana_jpn.add(jpn_lbl);
	ana_jpn.add(jpn_jtext);
	gun_frm.add(ana_jpn);
	gun_frm.setVisible(true);	
}
public static void main(String[] args) {
		new Calistir();
	}
}

 

Java KaloriHesaplahepsi indir

create database Kaloriler
create table Kategoriler(
KNO int primary key identity(10,10),
KAD nvarchar(50) not null
)
create table Besinler(
BNO int primary key identity(1,1),
BAD nvarchar(50) not null,
BM?KTAR float not null,
BKALOR? float not null,
KNO int not null
foreign key(KNO) references Kategoriler(KNO)
)
import java.io.*;
import java.sql.*;

public class Calistir {

	public Connection baglan()
	{   
		Connection baglanti=null;
		try
		{
			 baglanti=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Kaloriler;integratedSecurity=true");
		}
		catch(SQLException ex)
		{
			System.out.println(ex.getMessage()+"hata");
		}
	return baglanti;}
	public int kategoriekle(String ad,String birim)
	{  int no=0;
		Connection baglanti=null;
		try
		{
			 baglanti=baglan();
			 PreparedStatement sorgu=baglanti.prepareStatement("insert into Kategoriler(KAD,KBİRİM) values(?,?)");
			 sorgu.setString(1, ad);
			 sorgu.setString(2, birim);
			 sorgu.executeUpdate();
		    baglanti.close(); 
		}
		catch(SQLException ex)
		{
			System.out.println(ex.getMessage());	
		}
	
	try {
			baglanti=baglan();
			PreparedStatement sorgu2=baglanti.prepareStatement("select KNO from Kategoriler where KAD=?");
			   sorgu2.setString(1,ad);
			   ResultSet veri=sorgu2.executeQuery();
			   veri.next();
			   no=veri.getInt("KNO");
			   veri.close();
			   baglanti.close();
		}
		catch(SQLException ex1)
		{
		System.out.println(ex1.getMessage());	
		}
	return no;}
	public void bekle(String ad,double miktar,double kalori,int kno)
	{
		Connection baglanti=null;
		try
		{
			 baglanti=baglan();
			 PreparedStatement sorgu=baglanti.prepareStatement("insert into Besinler(BAD,BMİKTAR,BKALORİ,KNO) values(?,?,?,?)");
			 sorgu.setString(1, ad);
			 sorgu.setDouble(2, miktar);
			 sorgu.setDouble(3,kalori);
			 sorgu.setInt(4, kno);
			 sorgu.executeUpdate();
		   baglanti.close();
		}
		catch(SQLException ex)
		{
			
		}	
	}
public static void main(String[] arg) throws FileNotFoundException 
{ 
String[] kategoriler= {"Sebzeler","Şarküteri","Et Ürünleri","Şekerli Gıdalar","Yağlar","Kuru Gıdalar","Unlu Mamuller","Meyveler","Süt Ürünleri","İçecekler","Çorbalar","Tatlılar"}; 
String yol="C:\\Users\\Ali\\Desktop\\Kalori.txt";
Calistir db=new Calistir();
try 
{
    File dosya=new File(yol);
	BufferedReader bfr=new BufferedReader(new FileReader(dosya));
int no=0;
	while(bfr.ready())
{  
    boolean durum=false;
	String[] veri=bfr.readLine().split(":");
    if(veri.length==2)
    {
    	String ad=veri[0].trim();
    	String miktar=veri[1].trim();
    	for(int i=0;i<kategoriler.length;i++)
    	{
    		if(ad.equals(kategoriler[i]))
    		{
    			durum=true;
    			break;
    		}			
    	}
    	if(durum)
            no=db.kategoriekle(ad,"100gr");
    	else 
    		 db.bekle(ad,100.0,Double.parseDouble(miktar), no);
    }
}
}
catch(IOException ex)
{
  	System.out.println(ex.getMessage());
}
}
}
package VeriTabani;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import com.microsoft.sqlserver.jdbc.*;

public class DB {

private Connection baglan() 
{
  Connection baglanti=null;
  try
  {   
	  baglanti=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Kaloriler;integratedSecurity=true");
  }
	catch(SQLException ex)
  {
		JOptionPane.showMessageDialog(null, "HATA="+ex.getMessage());
  }
return baglanti;}

public void kategorigetir(JComboBox<String> jckategori,JComboBox<Integer> jcnumara)
{
   jckategori.removeAllItems();	
   jcnumara.removeAll();
   Connection baglanti=null;
   try
   {
 	  baglanti=baglan();
 	  Statement sorgu=baglanti.createStatement();
 	  ResultSet veri=sorgu.executeQuery("select KAD,KNO from Kategoriler");
      while(veri.next())
    	  {
    	       jckategori.addItem(veri.getString("KAD"));
    	       jcnumara.addItem(veri.getInt("KNO"));
    	  }
      jcnumara.setSelectedIndex(0);
      veri.close();
      baglanti.close();
   }
 	catch(SQLException ex)
   {   
 		JOptionPane.showMessageDialog(null, "HATA="+ex.getMessage());
   }

}
public void besinekle(String besinad,double besinmiktar,double kalori,int kno)
{
	   Connection baglanti=null;
	   try
	   {
	 	  baglanti=baglan();
	 	  PreparedStatement sorgu=baglanti.prepareStatement("insert into Besinler(BAD,BMİKTAR,BKALORİ,KNO) values(?,?,?,?)");
	 	  sorgu.setString(1, besinad);
          sorgu.setDouble(2, besinmiktar);
          sorgu.setDouble(3, kalori);
          sorgu.setInt(4, kno);  
          sorgu.executeUpdate();
          baglanti.close();
	   }
	 	catch(SQLException ex)
	   {   
	 		JOptionPane.showMessageDialog(null, "HATA="+ex.getMessage());
	   }
}
public void kategoriekle(String kad)
{
	 Connection baglanti=null;
	   try
	   {
	 	  baglanti=baglan();
	 	  PreparedStatement sorgu=baglanti.prepareStatement("insert into Kategoriler(KAD) values(?)");
	 	  sorgu.setString(1, kad);
          sorgu.executeUpdate();
          baglanti.close();
	   }
	 	catch(SQLException ex)
	   {   
	 		JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());
	   }
}
public void kategorisil(int KNO)
{
	 Connection baglanti=null;
	   try
	   {
	 	  baglanti=baglan();
	 	  PreparedStatement sorgu=baglanti.prepareStatement("delete from Besinler where KNO=? delete from Kategoriler where KNO=?");
	 	  sorgu.setInt(1,KNO);
	 	  sorgu.setInt(2, KNO);
        sorgu.executeUpdate();
        baglanti.close();
	   }
	 	catch(SQLException ex)
	   {   
	 		JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());
	   }
}
public void kategoriguncelle(String ad,int kno)
{
  Connection baglanti=null;
  try
  {
	  baglanti=baglan();
	  PreparedStatement sorgu=baglanti.prepareStatement("update Kategoriler set KAD=? where KNO=?");
	  sorgu.setString(1, ad);
	  sorgu.setInt(2, kno);
	  sorgu.executeUpdate();
	  baglanti.close();
	  
  }catch(SQLException ex)
  {
	  JOptionPane.showMessageDialog(null, ex.getMessage());
  }
}
public void listdoldur(DefaultListModel[] dlm,int kno,DefaultComboBoxModel[] dcm)
{
	 for(int i=0;i<dlm.length;i++)
	 { 
		 dlm[i].clear();
	     dcm[i].removeAllElements();
	 }
	 Connection baglanti=null;
	   try
	   {
	 	  baglanti=baglan();
	 	  PreparedStatement sorgu=baglanti.prepareStatement("select * from Besinler where KNO=?");
	 	  sorgu.setInt(1,kno);
         ResultSet veri=sorgu.executeQuery();
         while(veri.next())
         {
        	   dlm[0].addElement(veri.getString("BAD"));
        	   dlm[1].addElement(veri.getDouble("BMİKTAR"));
        	   dlm[2].addElement(veri.getDouble("BKALORİ"));
        	   dcm[0].addElement(veri.getString("BAD"));
        	   dcm[1].addElement(veri.getDouble("BMİKTAR"));
        	   dcm[2].addElement(veri.getDouble("BKALORİ"));   	  
         }
         veri.close();
         baglanti.close();
	   }
	 	catch(SQLException ex)
	   {   
	 		JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());
	   }
}
public void besinsil(String ad,int kno)
{
    Connection baglanti=null;
    try
    {
    	baglanti=baglan();
     	PreparedStatement sorgu=baglanti.prepareStatement("delete from Besinler where BAD=? and KNO=?");
        sorgu.setString(1, ad);
     	sorgu.setInt(2, kno);
     	sorgu.executeUpdate();
     	baglanti.close();
    }
    catch(SQLException ex)
    {
    	JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());
    }
}
public void besinguncelle(String ad,double miktar,double kalori,int kno,int bno)
{   
	Connection baglanti=null;
      try {
    	  baglanti=baglan();
          PreparedStatement sorgu=baglanti.prepareStatement("update Besinler set BAD=?,BMİKTAR=?,BKALORİ=? where KNO=? and BNO=?"); 
           sorgu.setString(1, ad);
           sorgu.setDouble(2, miktar);
           sorgu.setDouble(3, kalori);
           sorgu.setInt(4, kno);
           sorgu.setInt(5, bno);
          sorgu.executeUpdate();
      }  
      catch(SQLException ex)
      {
    	  JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());		  
      }
}
public int BNOBUL(String ad,int kno)
{   int bno=0;
   Connection baglanti=null;
    try {
    	baglanti=baglan();
    	PreparedStatement sorgu=baglanti.prepareStatement("select BNO from Besinler where BAD=? and KNO=?");
    	sorgu.setString(1, ad);
    	sorgu.setInt(2, kno);
        ResultSet veri=sorgu.executeQuery();
        veri.next();
        bno=veri.getInt("BNO");
        veri.close();
        baglanti.close();
    }catch(SQLException ex)
    {
    	JOptionPane.showMessageDialog(null,"HATA="+ex.getMessage());
    }
return bno;}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import VeriTabani.*;

class Kalori  implements ActionListener,ListSelectionListener,ItemListener{
	    DB dbislem;
	    JPanel anajpn,jpn[]=new JPanel[3];
	    JScrollPane[] jsc=new JScrollPane[3]; 
	    JList[] jlist=new JList[3];
	    DefaultListModel[] dlm=new DefaultListModel[3];
	    JLabel[] jlb=new JLabel[3];
	    String[] baslik= {"Besin","Miktar","Kalori"};
	    JTextField jtpor,jtext[]=new JTextField[3];
	    JButton[] jbtn=new JButton[4];
	    String[] btitle= {"Ekle","Sil","Düzenle","Cıkış"};
	    JComboBox<String> jcsecim; 
	    JComboBox<Integer> jcno; 
	    JFrame frm,frmgun;
	    JButton[] btnkategori; 
    public Kalori(JFrame frm1)
	{   dbislem=new DB();//Veri Tabanı işlemleri icin nesne tanımlanıyor
    	frm=frm1; 
		frm=new JFrame("Kalori Hesaplama");
		frm.setSize(500,500);
		frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frm.setLayout(new FlowLayout());
		
		//Anapanel
		anajpn=new JPanel();
		anajpn.setPreferredSize(new Dimension(450,450));
		anajpn.setLayout(new FlowLayout());
		
		//
		    JPanel jpn_secim=new JPanel();
		    jpn_secim.setPreferredSize(new Dimension(100,450));
		    jcsecim=new JComboBox<String>();
		    jcsecim.setPreferredSize(new Dimension(100,20));
		    JLabel jlkategori=new JLabel("Kategoriler");
		    jlkategori.setPreferredSize(new Dimension(100,25));
		    jpn_secim.add(jlkategori);
		    jpn_secim.add(jcsecim);
		    btnkategori=new JButton[3];
		    for(int i=0;i<btnkategori.length;i++)
		    {
		    	btnkategori[i]=new JButton(btitle[i]);
		    	btnkategori[i].setPreferredSize(new Dimension(100,20));
		    	btnkategori[i].addActionListener(this);
		    	btnkategori[i].setActionCommand("btn_"+btitle[i]);
		    	jpn_secim.add(btnkategori[i]);
		    }
		    jcno=new JComboBox<Integer>();
		    jcno.setPreferredSize(new Dimension(100,22));
			dbislem.kategorigetir(jcsecim,jcno);
			jcno.setSelectedIndex(0);
			jcsecim.addItemListener(this);
		anajpn.add(jpn_secim);
		for(int i=0;i<jlist.length;i++)
		{
			jpn[i]=new JPanel();
			jpn[i].setPreferredSize(new Dimension(100,450));
			jpn[i].setLayout(new FlowLayout());
	
			jlb[i]=new JLabel(baslik[i]);
			jlb[i].setPreferredSize(new Dimension(100,22));
		     
			jtext[i]=new JTextField();
			jtext[i].setPreferredSize(new Dimension(100,22));	
		    
			dlm[i]=new DefaultListModel();
		    jlist[i]=new JList(dlm[i]);
		    jlist[i].addListSelectionListener(this);
		    jsc[i]=new JScrollPane(jlist[i]);
		    jsc[i].setPreferredSize(new Dimension(100,290));
	        
		    jbtn[i]=new JButton(btitle[i]);	    
		    jbtn[i].setPreferredSize(new Dimension(100,22));
		    jbtn[i].addActionListener(this);
		    jbtn[i].setActionCommand(btitle[i]);
		   
		    jpn[i].add(jlb[i]);
		    jpn[i].add(jsc[i]);
		    jpn[i].add(jtext[i]);
		    jpn[i].add(jbtn[i]);
		    anajpn.add(jpn[i]);
		}
		for(int i=0;i<Calistir.jcombo[0].getItemCount();i++)
		{
		    dlm[0].addElement(Calistir.dcm[0].getElementAt(i));
			double miktar=sayicevir(Calistir.dcm[1].getElementAt(i).toString());
			double kalori=sayicevir(Calistir.dcm[2].getElementAt(i).toString());
		}
		
		dbislem.listdoldur(dlm,jcno.getItemAt(jcno.getSelectedIndex()),Calistir.dcm);
		jbtn[3]=new JButton(btitle[3]);
		jbtn[3].setPreferredSize(new Dimension(100,22));
		jbtn[3].addActionListener(this);
		jbtn[3].setActionCommand(btitle[3]);
		jpn[0].add(jbtn[3]);
        frm.add(anajpn); 
		frm.setVisible(true);
	}
    JPanel gun[],anagun;
    JTextField[] tgun;
    JLabel[] jlgun;
    String[] jlisim= {"Besin Ad:","Miktarı:","Kalorisi:"};
    JButton btngun;
    public void guncelle()
    {  
    	frmgun=new JFrame("Düzenle");
    	frmgun.setSize(300, 250);
    	frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frm.setLayout(new FlowLayout());
    	anagun=new JPanel();
        anagun.setPreferredSize(new Dimension(225,150));
    	gun=new JPanel[2];
    	tgun=new JTextField[3];
    	jlgun=new JLabel[3];
    	btngun=new JButton("Güncelle");
    	btngun.setPreferredSize(new Dimension(100,20));
    	btngun.addActionListener(this);
    	btngun.setActionCommand("Duzenle");
    	for(int i=0;i<gun.length;i++)
    	{
     	   gun[i]=new JPanel();
     	   gun[i].setPreferredSize(new Dimension(100,100));	
    	}
       for(int i=0;i<tgun.length;i++)
       {
    	   tgun[i]=new JTextField();
    	   tgun[i].setPreferredSize(new Dimension(100,20));
    	   jlgun[i]=new JLabel(jlisim[i]);
    	   jlgun[i].setPreferredSize(new Dimension(100,20));
    	   jlgun[i].setHorizontalAlignment(JLabel.RIGHT);
           gun[0].add(jlgun[i]);
           gun[1].add(tgun[i]);
       }
           gun[1].add(btngun);
          anagun.add(gun[0]);
          anagun.add(gun[1]);
    frmgun.add(anagun);
    frmgun.setVisible(true);
    } 
	public static boolean dogrula(JTextField[] jtext)
	{
		int boyut=jtext.length;
	    int sayac=0;
	    for(int i=0;i<boyut;i++)
	        if(jtext[i].getText().length()>0)
	                sayac++;
	      if(sayac==boyut)
	    	   return true;
	return false;} 
   public static double sayicevir(String s)
   {
	   double sayi=-1;
	   try {
		   sayi=Double.parseDouble(s);
	      return sayi;
	   }
	   catch(Exception ex)
	   {
		   return sayi;
	   }
   }
   int BNO;
	@Override
	public void actionPerformed(ActionEvent arg0) {
		int secilen=jlist[0].getSelectedIndex();
		int secilen2=jcsecim.getSelectedIndex();
		double miktar=sayicevir(jtext[1].getText());
		double kalori=sayicevir(jtext[2].getText());
		if(arg0.getActionCommand().equals("Ekle")&&dogrula(jtext)&&miktar>0&&kalori>0&&secilen2>-1)
		{     dbislem.besinekle(jtext[0].getText(),miktar, kalori,jcno.getItemAt(secilen2));
			  dlm[0].addElement(jtext[0].getText());
			  dlm[1].addElement(miktar);
			  dlm[2].addElement(kalori);
			  Calistir.dcm[0].addElement(jtext[0].getText());   
			  Calistir.dcm[1].addElement(miktar);   
			  Calistir.dcm[2].addElement(kalori);   
			  for(int i=0;i<dlm.length;i++)
			    jtext[i].setText("");     
		}
		else if(arg0.getActionCommand().equals("Sil")&&secilen>-1&&secilen2>-1)
		{
			dbislem.besinsil(dlm[0].getElementAt(secilen).toString(),jcno.getItemAt(secilen2));
			for(int i=0;i<dlm.length;i++)
				{
				  dlm[i].removeElementAt(secilen);
		           Calistir.dcm[i].removeElementAt(secilen);
				}
		} 
		else if(arg0.getActionCommand().equals("Düzenle")&&secilen>-1)
		{   guncelle();
			for(int i=0;i<tgun.length;i++)
			{
				tgun[i].setText((dlm[i].getElementAt(secilen).toString()));
			}
			BNO=dbislem.BNOBUL(tgun[0].getText(), jcno.getItemAt(secilen2));
		}
		else if(arg0.getActionCommand().equals("Duzenle")&&dogrula(tgun))
		{
			int kno= jcno.getItemAt(secilen2);
	dbislem.besinguncelle(tgun[0].getText(), Double.parseDouble(tgun[1].getText()), Double.parseDouble(tgun[2].getText()),kno,BNO);
	for(int i=0;i<dlm.length;i++)
			    dlm[i].remove(secilen);
               double kamiktar=sayicevir(tgun[1].getText());
               double bkalori=sayicevir(tgun[2].getText());
        	   dlm[0].insertElementAt(tgun[0].getText(), secilen);
        	   dlm[1].insertElementAt(kamiktar, secilen);
        	   dlm[2].insertElementAt(bkalori, secilen);	
		     frmgun.dispose();
		}
		else if(arg0.getActionCommand().equals("Cıkış"))
		{
			frm.dispose();
			Calistir.frm1.setVisible(true);
		}
		else if(arg0.getActionCommand().equals("btn_Ekle"))
		{
			  String kategoriad=JOptionPane.showInputDialog(frm, "Yeni Kategori Adını Giriniz!!","",JOptionPane.YES_OPTION);	
			  if(kategoriad.length()>0)
					 {
						dbislem.kategoriekle(kategoriad);
						jcsecim.addItem(kategoriad);
					 }
		}
		else if(arg0.getActionCommand().equals("btn_Sil")&&secilen2>-1)
		{
	      int durum=JOptionPane.showConfirmDialog(frm, "Bu kategori silerseniz Bu kategoriye ait Besinleride silersiniz!!");
		    if(durum==0)
	       {
		    	dbislem.kategorisil(jcno.getItemAt(secilen2));
		    	dbislem.kategorigetir(jcsecim,jcno);
	       }
	       }
		else if(arg0.getActionCommand().equals("btn_Düzenle")&&secilen2>-1)
		{
			String kategoriad=JOptionPane.showInputDialog(frm, "Kategorinin İsmini Düzenleyin","",JOptionPane.YES_OPTION);	
			  if(kategoriad.length()>0)
					 {  int kno=jcno.getItemAt(secilen2);
						dbislem.kategoriguncelle(kategoriad, kno);					
						dbislem.kategorigetir(jcsecim,jcno);
					 }
		}
		else
			JOptionPane.showMessageDialog(frm, "Alanlar Boş gecilemez");
	}
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
	     JList jlb=(JList)arg0.getSource();
	 int  secilen=jlb.getSelectedIndex();
		   for(int i=0;i<jlist.length;i++)
		       jlist[i].setSelectedIndex(secilen);
	}
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		JComboBox<Integer> jcombo=(JComboBox<Integer>)arg0.getSource();
		if(arg0.getStateChange()==ItemEvent.SELECTED)
     	   	dbislem.listdoldur(dlm,jcno.getItemAt(jcombo.getSelectedIndex()),Calistir.dcm);
		   }           
}
/////////////////////////////
public class Calistir implements ActionListener,ListSelectionListener {
    JPanel anajpn,jpn[]=new JPanel[4];
    JScrollPane[] jsc=new JScrollPane[3]; 
    JList[] jlist=new JList[3];
    DefaultListModel[] dlm=new DefaultListModel[3];
    JLabel[] jlb=new JLabel[3];
    String[] baslik= {"Kilo:","Boy:","Yaş:"};
    JLabel[] jlad=new JLabel[3];
    String[] jisim= {"Besinler","Porsiyon","Kalorisi"};
    JTextField jtpor,jtext[]=new JTextField[3];
    JButton[] jbtn=new JButton[3];
    String[] btitle= {"Ekle","Sil","Kalori"};
   static JComboBox[] jcombo=new JComboBox[3];
   static DefaultComboBoxModel[] dcm=new DefaultComboBoxModel[3];
	JLabel[] jlb1=new JLabel[2];
	String[] baslik1= {"Günlük kalori:","Toplam Kalori:"};
	JRadioButton[] jrd=new JRadioButton[2];
	String[] cins= {"Erkek","Kız"};
	ButtonGroup grup=new ButtonGroup();
	static JFrame frm1;
	static JFrame frm2;
    public Calistir()
	{
		frm1=new JFrame("Kalor Hesaplama");
		frm1.setSize(500,500);
		frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm1.setLayout(new FlowLayout());
		
		//Anapanel
		anajpn=new JPanel();
		anajpn.setPreferredSize(new Dimension(400,500));
		anajpn.setLayout(new FlowLayout());
		
		//
		for(int i=0;i<jlist.length;i++)
		{
			jpn[i]=new JPanel();
			jpn[i].setPreferredSize(new Dimension(100,500));
			jpn[i].setLayout(new FlowLayout());
			jlb[i]=new JLabel(baslik[i]);
			jlb[i].setPreferredSize(new Dimension(25,25));
			jlad[i]=new JLabel(jisim[i]);
			jlad[i].setPreferredSize(new Dimension(100,18));
			jtext[i]=new JTextField();
			jtext[i].setPreferredSize(new Dimension(65,25));	
			jtext[i].setText("10");
			dlm[i]=new DefaultListModel();
		    jlist[i]=new JList(dlm[i]);
		    jlist[i].addListSelectionListener(this);
		    jsc[i]=new JScrollPane(jlist[i]);
		    jsc[i].setPreferredSize(new Dimension(100,290));
		    jpn[i].add(jlb[i]);
		    jpn[i].add(jtext[i]);
		    jpn[i].add(jlad[i]);
		    jpn[i].add(jsc[i]);
		    anajpn.add(jpn[i]);
		    dcm[i]=new DefaultComboBoxModel();
		}
		jcombo[0]=new JComboBox(dcm[0]);
		jcombo[0].setPreferredSize(new Dimension(100,25));
		jtpor=new JTextField();
		jtpor.setPreferredSize(new Dimension(100,25));
		jpn[0].add(jcombo[0]);
		jpn[1].add(jtpor);
		for(int i=0;i<jlb1.length;i++)
		{    
			jlb1[i]=new JLabel(baslik1[i]);
			jlb1[i].setPreferredSize(new Dimension(90,10));
			jpn[2].add(jlb1[i]);
	  		jcombo[i+1]=new JComboBox(dcm[i+1]);
		}
		for(int i=0;i<jbtn.length;i++)
		{
			jbtn[i]=new JButton(btitle[i]);	    
		    jbtn[i].setPreferredSize(new Dimension(100,25));
		    jbtn[i].addActionListener(this);
		    jbtn[i].setActionCommand(btitle[i]);
		    jpn[i].add(jbtn[i]);
		    anajpn.add(jpn[i]);
		}
		
		jpn[3]=new JPanel();
		jpn[3].setPreferredSize(new Dimension(70,407));
		jrd[0]=new JRadioButton(cins[0],true);
		jrd[0].setPreferredSize(new Dimension(70,25));
		jrd[1]=new JRadioButton(cins[1],false);
		jrd[1].setPreferredSize(new Dimension(70,25));
		grup.add(jrd[0]);
		grup.add(jrd[1]);
		jpn[3].add(jrd[0]);
		jpn[3].add(jrd[1]);
		anajpn.add(jpn[3]);
		frm1.add(anajpn);
		frm1.setVisible(true);
	}
    @Override
    public void actionPerformed(ActionEvent arg)
    {
    	 int secilen1=jcombo[0].getSelectedIndex();
    	 double por=Kalori.sayicevir(jtpor.getText());
    	 int secilen2=jlist[0].getSelectedIndex();
    	if(arg.getActionCommand().equals("Ekle")&&secilen1>-1&&por>0)
    	{
    		dlm[0].addElement(dcm[0].getElementAt(secilen1));
    		dlm[1].addElement(virgul(por));
    		double kalori=(por*(double)dcm[2].getElementAt(secilen1))/(double)dcm[1].getElementAt(secilen1);
    	    dlm[2].addElement(virgul(kalori));
    	    gunkal();
    	    topkal();
    	    jtpor.setText("");
    	}
    	else if(arg.getActionCommand().equals("Sil")&&secilen2>-1)
    	{
    		for(int i=0;i<dlm.length;i++)
    			dlm[i].removeElementAt(secilen2);
    		  topkal();
    	}
    	else if(arg.getActionCommand().equals("Kalori"))
    		{   frm1.setVisible(false);
    		   new Kalori(frm2);  	
    		}
    	else
    		JOptionPane.showMessageDialog(frm1,"Besin secilmek zorunda ve miktar boş gecilemez");
    }
    private void topkal()
    {
    	double toplam=0;
    	for(int i=0;i<dlm[2].getSize();i++)
    		toplam+=(double)dlm[2].getElementAt(i);
    	jlb1[1].setText("Top Kal:"+virgul(toplam));
    }
    public static double virgul(double sayi)
    {
    	double sonuc=sayi*100;
    	int sonuc1=(int)sonuc;		
    	sonuc=sonuc1/100.0;
    return sonuc;}
    
    public void gunkal()
    {  double[] erkek= {66,13.7,5,6.8};
       double[] kadin= {655,9.6,1.8,4.7};
       double sonuc=0.0;
      double[] oranlar=kadin;
      double[] degerler=new double[4];
      degerler[0]=1;
      for(int i=0;i<jtext.length;i++)
    	  degerler[i+1]=Kalori.sayicevir(jtext[i].getText());
      
      if(jrd[0].isSelected())//jrd radiobuuton secilimi değilmi
    	  oranlar=erkek;
      if(Kalori.dogrula(jtext))
        sonuc=(oranlar[0]*degerler[0]+oranlar[1]*degerler[1]+oranlar[2]*degerler[2])-(oranlar[3]*degerler[3]);
    jlb1[0].setText("Gün Kal:"+sonuc); 
    }
	public static void main(String[] arg)
	{
    new Calistir();
	}
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
	     JList jlb=(JList)arg0.getSource();
	     int secilen=jlb.getSelectedIndex();  
	   if(secilen>-1)
	   {
		   for(int i=0;i<jlist.length;i++)
		       jlist[i].setSelectedIndex(secilen);
	   }
	}  
}