Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-13

import java.awt.*;
import java.awt.event.*;
import java.util.*;

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

public class anaprogram extends JFrame implements ActionListener {

	JList<String>[] jl;
	JComboBox<String> jcb_isim,jcb_harf;
	JTextField jtf;
	Vector<Integer> vc_jcb_isimid, vc_jcb_harfid;
	Vector<String> vc_isimler = new Vector<String>();
	ikinciform frm = new ikinciform(this);
	
	int[] harfkarsilik={90,80,70,60,50,40,35,30};
	String[] harfler={"AA","BA","BB","CB","CC","DC","DD","FD"};
	Connection conn=null;
	
	public anaprogram()
	{
		this.setTitle("Sana Kaç Lazım?");
		this.setLayout(new FlowLayout());
		this.setSize(500,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		String[] labels={"İsim","Vize","Harf","Gereken Not"};
		
		
		JPanel jpn_main = new JPanel();
		jpn_main.setPreferredSize(new Dimension(490,290));
		jpn_main.setLayout(new GridLayout(1,labels.length));
		
		jl = new JList[labels.length];
		Dimension dmn = new Dimension(100,30);
		
		for(int i=0;i<labels.length;i++)
		{
			JPanel jpn = new JPanel();
			jpn.add(new JLabel(labels[i]));
			
			jl[i]= new JList<String>();
			JScrollPane jscp = new JScrollPane(jl[i]);
			jscp.setPreferredSize(new Dimension(100,190));
			jpn.add(jscp);
			
			if(i==0)
			{
				jcb_isim = new JComboBox<String>();
				jcb_isim.setPreferredSize(dmn);
				
				jpn.add(jcb_isim);
				
				JButton jbtn = new JButton("Ekle");
				jbtn.setPreferredSize(dmn);
				jbtn.setActionCommand("ekle");
				jbtn.addActionListener(this);
				jpn.add(jbtn);
			}
			
			if(i==1)
			{
				jtf = new JTextField();
				jtf.setPreferredSize(dmn);
				jpn.add(jtf);
				
				JButton jbtn = new JButton("Sil");
				jbtn.setPreferredSize(dmn);
				jbtn.setActionCommand("sil");
				jbtn.addActionListener(this);
				jpn.add(jbtn);
			}
			

			if(i==2)
			{
				jcb_harf = new JComboBox<String>(harfler);
				jcb_harf.setPreferredSize(dmn);
				jpn.add(jcb_harf);
			}
			
			if(i==3)
			{
				JPanel jpnx = new JPanel();
				jpnx.setPreferredSize(dmn);
				jpn.add(jpnx);
				
				JButton jbtn = new JButton("Liste");
				jbtn.setPreferredSize(dmn);
				jbtn.setActionCommand("liste");
				jbtn.addActionListener(this);
				jpn.add(jbtn);
			}
			
			
			jpn_main.add(jpn);
		}
		
		this.add(jpn_main);
		
		baglan();
		if(conn==null)
		{
			JOptionPane.showMessageDialog(this, "Veritabanı açılamadı!");
			System.exit(0);
		}
		
		isimcombodoldur();
		listeguncelle();
		
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {

		switch (arg0.getActionCommand()) {
		case "ekle":
			ekle();
			break;
		case "sil":
			sil();		
			break;
		case "liste":
			frm.goster();
			break;
		}
		
		listeguncelle();
		

	}
	
	private void baglan()
	{
		try{
			
			Class.forName("org.sqlite.JDBC");
			conn=DriverManager.getConnection("jdbc:sqlite:ornek13.db"); 
		}
		catch(Exception ex)
		{
			
		}
		
	}

	public void isimcombodoldur() {
		try{
			jcb_isim.removeAllItems();
			vc_jcb_isimid = new Vector<Integer>(); // yeni obje olusturduk
			vc_isimler.clear();
		
			
			String sql="select id,isim from isimler order by isim";
			PreparedStatement ps =conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				vc_jcb_isimid.add(rs.getInt("id")); // id listesi dolduruluyor  	
				jcb_isim.addItem(rs.getString("isim"));
				vc_isimler.add(rs.getString("isim"));
			}
			
			frm.jl.updateUI(); // ikinci formdaki jlist gorseli guncellensin
			
		}catch(Exception ex)
		{
			
		}
		
	}
	
	private void listeguncelle() {

		try{
			Vector<String>[] vc = new Vector[4];

			for(int i=0;i<vc.length;i++)
				vc[i] = new Vector<String>();
			
			vc_jcb_harfid = new Vector<Integer>();
			
			String sql="select h.id,i.isim,h.vize,h.harf from harfler h inner join isimler i on i.id=h.uid;";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			
			while(rs.next())
			{
				vc[0].add(rs.getString("isim"));
				vc[1].add(rs.getString("vize"));
				vc[2].add(rs.getString("harf"));
				vc[3].add(hesapla(rs.getInt("vize"),rs.getString("harf"))); // gereken deger
				vc_jcb_harfid.add(rs.getInt("id"));
			}
			
			for(int i=0;i<vc.length;i++)
				jl[i].setListData(vc[i]);
			
			
		}catch(Exception ex)
		{
			
		}
		
	}
	
	
	private String hesapla(int vize, String harf)
	{
		
		int indis = Arrays.binarySearch(harfler, harf);
		
		if(indis>-1)
		{	
			int ortalama=harfkarsilik[indis];
			
			int finalnot= (int)(((ortalama-vize*0.4)/0.6)+0.9);
			
			
			if(finalnot<101)
			{
				if(finalnot<30)
					finalnot=30;
				
				return finalnot+"";
			}
		}
		
		return "-";
	}

	private void sil() {
		
		try{
			int secilen = jl[0].getSelectedIndex();
			
			if(secilen>-1){	
				int id= vc_jcb_harfid.get(secilen);
				String sql="delete from harfler where id=?";
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setInt(1, id);
				ps.executeUpdate();	
			}
		}
		catch(Exception ex)
		{
			
		}
		
	}

	private void ekle() {

		try{
			
			int secilen = jcb_isim.getSelectedIndex();
			
			if(secilen>-1)
			{
				int uid = vc_jcb_isimid.get(secilen); // isimler listesinde yer alan id bilgisi gelecek  
				int vize=Integer.parseInt(jtf.getText());
				if(vize<101 && vize>-1)
				{
					String harf=jcb_harf.getSelectedItem().toString();
					String sql = "insert into harfler(uid,vize,harf) values(?,?,?);";
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setInt(1, uid);
					ps.setInt(2, vize);
					ps.setString(3, harf);
					ps.executeUpdate();
					
					jtf.setText("");
					jcb_isim.setSelectedIndex(0);
					jcb_harf.setSelectedIndex(0);
				}
			}
		}catch(Exception ex)
		{
			
			
		}
		
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new anaprogram();

	}

}
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.*;


public class ikinciform extends JFrame implements ActionListener {

	
	JList<String> jl;
	JTextField jtf;
	
	anaprogram anafrm;
	public ikinciform(anaprogram _anafrm)
	{
		anafrm=_anafrm;
		
		this.setTitle("İsim Listesi");
		this.setLayout(new FlowLayout());
		this.setSize(200,400);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		
		JPanel jpn_main= new JPanel();
		jpn_main.setPreferredSize(new Dimension(190,390));
		
		JLabel jlab = new JLabel("İsim Listesi");
		jpn_main.add(jlab);
		
		jl = new JList<String>(anafrm.vc_isimler);	
		JScrollPane jscp = new JScrollPane(jl);
		jscp.setPreferredSize(new Dimension(180,290));
		jpn_main.add(jscp);
		
		
		jtf = new JTextField();
		jtf.setPreferredSize(new Dimension(180,30));
		jpn_main.add(jtf);
		
		
		JButton jbtn1 = new JButton("Ekle");
		jbtn1.setPreferredSize(new Dimension(85,30));
		jbtn1.setActionCommand("ekle");
		jbtn1.addActionListener(this);
		
		JButton jbtn2 = new JButton("Çıkış");
		jbtn2.setPreferredSize(new Dimension(85,30));
		jbtn2.addActionListener(this);
		jpn_main.add(jbtn1);
		jpn_main.add(jbtn2);
		
		
		
		
		
		this.add(jpn_main);
		this.setVisible(false);
	}
	
	public void goster()
	{
		this.setVisible(true);
	}

	public void gizle()
	{
		this.setVisible(false);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getActionCommand().equals("ekle"))
		{
			ekle();
		}
		else
			gizle();

	}
	
	private void ekle()
	{
		
		try{
			
			String sql="insert into isimler(isim) values(?);";
			PreparedStatement ps = anafrm.conn.prepareStatement(sql);
			ps.setString(1, jtf.getText());
			ps.executeUpdate();
			jtf.setText("");
			
			anafrm.isimcombodoldur();
			
		}catch(Exception ex){
			
		}
		
		
	}

}

 

 

 

NTP2 (Bahar-2016) Hafta-13

Share