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

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

public class ornek1 extends JFrame implements ActionListener,
		ListSelectionListener {

	JList<String>[] jl;
	JTextField jtf;
	JComboBox<String> jcb;
	Vector<String> vc_jl_id,vc_jcb_id;
	JRadioButton jrb1,jrb2;
	Connection conn=null;
	
	public ornek1()
	{
		this.setTitle("Stok Programı");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,380);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main = new JPanel();
		jpn_main.setPreferredSize(new Dimension(390,370));
		jpn_main.setLayout(new GridLayout(1,4));
		
		String[] labels={"Adı","Miktar","İşlem","Tutar"};
		jl= new JList[labels.length];
		
		Dimension dmn = new Dimension(90,30);
		
		for(int i=0;i<labels.length;i++)
		{
			JPanel jpn = new JPanel();
			
			JLabel jlab= new JLabel(labels[i]);
			jpn.add(jlab);
			
			jl[i]= new JList();
			JScrollPane jscp = new JScrollPane(jl[i]);
			jscp.setPreferredSize(new Dimension(90,265));
			jpn.add(jscp);
			
			switch (i) {
			case 0:
				jcb = new JComboBox<String>();
				jcb.setPreferredSize(dmn);
				JButton jbtn1 = new JButton("Ekle");
				jbtn1.setPreferredSize(dmn);
				jbtn1.addActionListener(this);
				jbtn1.setActionCommand("ekle");
				
				jpn.add(jcb);
				jpn.add(jbtn1);
				
				break;

			case 1:
				
				jtf = new JTextField();
				jtf.setPreferredSize(dmn);
				JButton jbtn2 = new JButton("Sil");
				jbtn2.setPreferredSize(dmn);
				jbtn2.addActionListener(this);
				
				jpn.add(jtf);
				jpn.add(jbtn2);
				
				
				
				break;
			case 2:
				ButtonGroup bg = new ButtonGroup();
				jrb1 = new JRadioButton("Alış");
				jrb1.setPreferredSize(dmn);
				jrb2 = new JRadioButton("Satış");
				jrb2.setPreferredSize(dmn);
				bg.add(jrb1);
				bg.add(jrb2);
				jrb1.setSelected(true);
				jpn.add(jrb1);
				jpn.add(jrb2);
				break;
			}
			
			
			jpn_main.add(jpn);
		}
		this.add(jpn_main);
		
		baglan();
		if(conn==null)
		{
			System.exit(0);
		}
		
		cb_doldur();
		jl_doldur();
		this.setVisible(true);
	}
	
	
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub

	}
	
	

	@Override
	public void actionPerformed(ActionEvent arg0) {

		if(arg0.getActionCommand().equals("ekle"))
			ekle();
		else
			sil();

		jl_doldur();

	}

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

		try{
			vc_jl_id= new Vector<String>();
			Vector<String>[] vc = new Vector[jl.length]; // liste icerik
			
			for(int i=0;i<vc.length;i++)
				vc[i]= new Vector<String>();
			
			String sql="select u.adi, (u.alisfiyat*d.miktar) as tutar1, (u.satisfiyat*d.miktar) as tutar2, d.miktar, d.islem, d.id from urunler u inner join depo d on d.uid=u.id;";
			
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			
			while(rs.next())
			{
				vc[0].add(rs.getString("adi"));
				vc[1].add(rs.getString("miktar"));
				if(rs.getInt("islem")==1){
					vc[2].add("Alış");
					vc[3].add(rs.getString("tutar1"));
				}
				else
				{
					vc[2].add("Satış");
					vc[3].add(rs.getString("tutar2"));
				}
				
				vc_jl_id.add(rs.getString("id"));
			}
			
			// vektorlerin listelere aktarilmasi
			for(int i=0;i<vc.length;i++)
				jl[i].setListData(vc[i]);
			
		}
		catch(Exception ex)
		{
			
		}
		
	}
	
	private void cb_doldur() {
	
		try{
			jcb.removeAllItems();
			vc_jcb_id = new Vector<String>();
			
			String sql="select id,adi from urunler order by adi;";
			
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			
			while(rs.next())
			{
				vc_jcb_id.add(rs.getString("id"));
				jcb.addItem(rs.getString("adi"));
			}
			
			if(jcb.getItemCount()>0)
				jcb.setSelectedIndex(0);
			
		}catch(Exception ex)
		{}
		
		
	}
	
	private void sil() {

		try{
			
			int secilen = jl[0].getSelectedIndex();
			
			if(secilen>-1)
			{
				String uid = vc_jl_id.get(secilen);
				String sql="delete from depo where id=?";		
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, uid);
				ps.executeUpdate();
		
			}
		}
		catch(Exception ex)
		{
			
			
		}
		
	}


	private void ekle() {

		try{
			
			int miktar = Integer.parseInt(jtf.getText());
			int secilen = jcb.getSelectedIndex();
		
			if(secilen>-1)
			{
				String id = vc_jcb_id.get(secilen);
				
				int islem=1;
				
				if(jrb2.isSelected())
				{
					islem=2;
					
					int stok = stokdurum(id);
					if(stok<miktar)
						return;
					
				}
		
			
				String sql="insert into depo(uid,miktar,islem) values(?,?,?);";
			
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1,id);
				ps.setInt(2, miktar);
				ps.setInt(3, islem);
				ps.executeUpdate();
				
				
				jrb1.setSelected(true);
				jcb.setSelectedIndex(0);
				jtf.setText("");
				
			}
			
			
		}catch(Exception ex)
		{
			
			
		}
		
		
	}

	private int stokdurum(String uid)
	{
		int miktar=0;
		try{
			
			String sql="select sum(miktar) as toplam, islem from depo where uid=? group by islem;";
			
			int alis=0;
			int satis=0;
			
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, uid);
			
			ResultSet rs = ps.executeQuery();
			
			while(rs.next())
			{
				if(rs.getInt("islem")==1)
					alis=rs.getInt("toplam");
				else
					satis=rs.getInt("toplam");
			}
			
			miktar=alis-satis;
			
		}catch(Exception ex)
		{
			
		}
		
		return miktar;
	}

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

	}

}

 

 

NTP2 (Bahar-2016) Hafta-12

Share