Java-2 – YO2015 – Hafta – 6

java2_yo2015_hafta6_resim1

 

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

public class ornek1 implements ActionListener {

	JList[] jls;
	DefaultListModel<String>[] dlm_str; // ad ve tip 
	DefaultListModel<Integer>[] dlm_int;// sayilar(gunduz, tutar,..)
	JTextField jtf_ad,jtf_gunduz,jtf_puant,jtf_gece;
	JComboBox<String> jcb;
	
	DefaultListModel<Integer> cid,mid; // cid:combodan secilen tip bilgisine ait id (idfiyat)
									   // mid: silmek icin musteri id (idmusteri)

	public ornek1() {
		
		dlm_str=new DefaultListModel[2];
		dlm_int=new DefaultListModel[5];
		jls = new JList[7];
		
		JFrame jfrm = new JFrame("Elekrik sayac tarifesi");
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new GridLayout(1,7));
		jfrm.setSize(750,250);
		
		JPanel jpn = new JPanel();
		JLabel jlab = new JLabel("Müşteri Adı");
		dlm_str[0]= new DefaultListModel<String>(); // musteri isimleri
		jls[0]= new JList<String>(dlm_str[0]);
		JScrollPane jsp = new JScrollPane(jls[0]);
		jsp.setPreferredSize(new Dimension(80,150));
		jtf_ad= new JTextField();
		jtf_ad.setPreferredSize(new Dimension(80,20));
		JButton jbtn = new JButton("Ekle");
		jbtn.addActionListener(this);
		jbtn.setActionCommand("ekle");
		jpn.add(jlab);
		jpn.add(jsp);
		jpn.add(jtf_ad);
		jpn.add(jbtn);
		jfrm.add(jpn);
		
		
		jpn = new JPanel();
		jlab = new JLabel("Tarife");
		dlm_str[1]= new DefaultListModel<String>();
		jls[1]= new JList<String>(dlm_str[1]);
		jsp = new JScrollPane(jls[1]);
		jsp.setPreferredSize(new Dimension(80,150));
		jcb= new JComboBox<String>();
		jcb.setPreferredSize(new Dimension(100,20));
		jbtn = new JButton("Sil");
		jbtn.addActionListener(this);
		jbtn.setActionCommand("sil");
		jpn.add(jlab);
		jpn.add(jsp);
		jpn.add(jcb);
		jpn.add(jbtn);
		jfrm.add(jpn);
				
		jpn = new JPanel();
		jlab = new JLabel("Gündüz");
		dlm_int[0]= new DefaultListModel<Integer>();
		jls[2]= new JList<Integer>(dlm_int[0]);
		jsp = new JScrollPane(jls[2]);
		jsp.setPreferredSize(new Dimension(80,150));
		jtf_gunduz= new JTextField();
		jtf_gunduz.setPreferredSize(new Dimension(80,20));
		jbtn = new JButton("Güncelle");
		jbtn.addActionListener(this);
		jbtn.setActionCommand("guncelle");
		jpn.add(jlab);
		jpn.add(jsp);
		jpn.add(jtf_gunduz);
		jpn.add(jbtn);
		jfrm.add(jpn);
		
		
		
		jpn = new JPanel();
		jlab = new JLabel("Puant");
		dlm_int[1]= new DefaultListModel<Integer>();
		jls[3]= new JList<Integer>(dlm_int[1]);
		jsp = new JScrollPane(jls[3]);
		jsp.setPreferredSize(new Dimension(80,150));
		jtf_puant= new JTextField();
		jtf_puant.setPreferredSize(new Dimension(80,20));
		jpn.add(jlab);
		jpn.add(jsp);
		jpn.add(jtf_puant);
		jfrm.add(jpn);
		
		
		jpn = new JPanel();
		jlab = new JLabel("Gece");
		dlm_int[2]= new DefaultListModel<Integer>();
		jls[4]= new JList<Integer>(dlm_int[2]);
		jsp = new JScrollPane(jls[4]);
		jsp.setPreferredSize(new Dimension(80,150));
		jtf_gece= new JTextField();
		jtf_gece.setPreferredSize(new Dimension(80,20));
		jpn.add(jlab);
		jpn.add(jsp);
		jpn.add(jtf_gece);
		jfrm.add(jpn);
		
		jpn = new JPanel();
		jlab = new JLabel("Tek Zamanlı");
		dlm_int[3]= new DefaultListModel<Integer>();
		jls[5]= new JList<Integer>(dlm_int[3]);
		jsp = new JScrollPane(jls[5]);
		jsp.setPreferredSize(new Dimension(80,150));
		jpn.add(jlab);
		jpn.add(jsp);
		jfrm.add(jpn);
		
		jpn = new JPanel();
		jlab = new JLabel("Üç Zamanlı");
		dlm_int[4]= new DefaultListModel<Integer>();
		jls[6]= new JList<Integer>(dlm_int[4]);
		jsp = new JScrollPane(jls[6]);
		jsp.setPreferredSize(new Dimension(80,150));
		jpn.add(jlab);
		jpn.add(jsp);
		jfrm.add(jpn);
		verigetir();
		jfrm.setVisible(true);
	}
	
	
	private void verigetir()
	{		
		Connection conn = baglan();
		
		try{
			
			//temizlik
			
			for(int i=0;i<dlm_str.length;i++)
				dlm_str[i].removeAllElements();
			
			for(int i=0;i<dlm_int.length;i++)
				dlm_int[i].removeAllElements();
			
			String sql_combo="select idfiyat,tip from fiyat;";
			
			Statement st = conn.createStatement();
			ResultSet rs =st.executeQuery(sql_combo);
			cid= new DefaultListModel<Integer>();
			jcb.removeAllItems(); // listeyi temizle
			jcb.addItem("Seçiniz");
			while(rs.next())
			{
				cid.addElement(rs.getInt("idfiyat")); // ekleme kisminda kullanilacak
				jcb.addItem(rs.getString("tip"));		
			}
			
			
			String sql_list="select idmusteri,adi,hgunduz,hpuant,hgece,tip," +
					"(hgunduz+hpuant+hgece)*tekzamanli/100 as tutar1," +
					"(hgunduz*gunduz+hpuant*puant+hgece*gece)/100 as tutar2" +
					" from musteri inner join fiyat on tipid=idfiyat;";
		
			mid= new DefaultListModel<Integer>(); // silmek icin kullanilacak
			
			rs =st.executeQuery(sql_list);
			while(rs.next())
			{
				
				int tutar1=rs.getInt("tutar1");
				int tutar2=rs.getInt("tutar2");
				
				mid.addElement(rs.getInt("idmusteri"));
				dlm_str[0].addElement(rs.getString("adi"));
				dlm_str[1].addElement(rs.getString("tip"));
				dlm_int[0].addElement(rs.getInt("hgunduz"));
				dlm_int[1].addElement(rs.getInt("hpuant"));
				dlm_int[2].addElement(rs.getInt("hgece"));
				dlm_int[3].addElement(tutar1);
				dlm_int[4].addElement(tutar2);
				
				
			}
		
		
		}catch(Exception ex)
		{

		}
		
		
		try{
			
			conn.close();
			
		}
		catch(Exception ex)
		{
			
		}
		
		
	}

	public Connection baglan()
	{
	Connection conn=null;
	try{
		Class.forName("org.sqlite.JDBC");
		conn=DriverManager.getConnection("jdbc:sqlite:ornek1.db"); 
	}
	catch(Exception ex)
	{
		JOptionPane.showMessageDialog(null,"Bağlantı Yapılamadı!","Hata",0);
	}
	return conn;	
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {

		Connection conn = baglan();
		
		if(ae.getActionCommand().equals("ekle"))
		{
			
			try{
				int gunduz =Integer.parseInt(jtf_gunduz.getText());
				int puant =Integer.parseInt(jtf_puant.getText());
				int gece =Integer.parseInt(jtf_gece.getText());
				
				if(jtf_ad.getText().length()>0 && jcb.getSelectedIndex()>0)
				{
					
					String sql= "insert into musteri(adi,tipid,hgunduz,hpuant,hgece)" +
							" values(?,?,?,?,?);";
					
					int tipid=cid.get(jcb.getSelectedIndex()-1);
										
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, jtf_ad.getText());
					ps.setInt(2, tipid);
					ps.setInt(3, gunduz);
					ps.setInt(4, puant);
					ps.setInt(5, gece);
					ps.executeUpdate();
					
					jtf_ad.setText("");
					jtf_gunduz.setText("");
					jtf_puant.setText("");
					jtf_gece.setText("");
				}
				
				
				
				
			}catch(Exception ex)
			{
				
				
			}
			
			
			
			
		}
	
		if(ae.getActionCommand().equals("sil"))
		{
		
			try{
				
				String sql ="delete from musteri where idmusteri=?";
				
				int secilen=jls[0].getSelectedIndex();
				
				if(secilen>-1)
				{
					int silinecek_id=mid.get(secilen); // silinecek musterinin id	
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setInt(1, silinecek_id);
					ps.executeUpdate();
				}
				
				
				
				
			}catch(Exception ex)
			{
				
				
			}
			
			
		}
	
		if(ae.getActionCommand().equals("guncelle"))
		{
			
			try
			{
				int gunduz =Integer.parseInt(jtf_gunduz.getText());
				int puant =Integer.parseInt(jtf_puant.getText());
				int gece =Integer.parseInt(jtf_gece.getText());
				
				int listeden_sec=jls[0].getSelectedIndex();
				
				if(listeden_sec>-1 && jtf_ad.getText().length()>0 && jcb.getSelectedIndex()>0)
				{
					 String sql ="update musteri set adi=?,tipid=?" +
						 		",hgunduz=?,hpuant=?,hgece=? where idmusteri=?;";	
							
					int tipid=cid.get(jcb.getSelectedIndex()-1);
					int musteri_id=mid.get(listeden_sec);
										
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, jtf_ad.getText());
					ps.setInt(2, tipid);
					ps.setInt(3, gunduz);
					ps.setInt(4, puant);
					ps.setInt(5, gece);
					ps.setInt(6, musteri_id);
					ps.executeUpdate();
					
					jtf_ad.setText("");
					jtf_gunduz.setText("");
					jtf_puant.setText("");
					jtf_gece.setText("");
				}

				
			}catch(Exception ex)
			{	
				
			}
			
		}
	
		verigetir();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ornek1();
	}

}

JAVA-2 YO2015 6. Hafta Uygulamaları

Share