import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.Vector;
public class fatura extends JFrame implements ActionListener,
ListSelectionListener {
JList<String>[] jl;
Vector<String>[] vc_jl; // liste icin
JComboBox<String> jcb;
Vector<String>[] vc_cb; //combo icin
JTextField[] jtf;
Connection conn=null;
public fatura()
{
this.setTitle("Fatura Hesaplama");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,250);
this.setLayout(new FlowLayout());
JPanel jpn_main = new JPanel();
jpn_main.setPreferredSize(new Dimension(490,240));
jpn_main.setLayout(new GridLayout(1,5));
String[] labels={"Tarife","MB","DK","SMS","Tutar"};
String[] buttons={"Ekle","Sil"};
jl= new JList[labels.length];
vc_jl = new Vector[labels.length+1]; // fatura id icin +1
vc_cb= new Vector[2]; // 0-->tarife adi, 1-->tarife id
jtf = new JTextField[3];
vc_cb[1]= new Vector<String>(); // tarife id
vc_jl[labels.length]= new Vector<String>(); // fatura id
for(int i=0;i<labels.length;i++)
{
JPanel jpn = new JPanel();
jpn_main.add(jpn);
JLabel jlab = new JLabel(labels[i]);
jpn.add(jlab);
vc_jl[i] = new Vector<String>();
jl[i]= new JList<String>(vc_jl[i]);
jl[i].addListSelectionListener(this);
JScrollPane jscp = new JScrollPane(jl[i]);
jscp.setPreferredSize(new Dimension(90,140));
jpn.add(jscp);
// combobox
if(i==0)
{
vc_cb[i] = new Vector<String>();
jcb = new JComboBox<String>(vc_cb[i]);
jcb.setPreferredSize(new Dimension(90,30));
jpn.add(jcb);
}
// textfield
if(i>0 && i<4)
{
jtf[i-1] = new JTextField();
jtf[i-1].setPreferredSize(new Dimension(90,30));
jpn.add(jtf[i-1]);
}
//button
if(i<2)
{
JButton jbtn = new JButton(buttons[i]);
jbtn.setPreferredSize(new Dimension(90,30));
jbtn.addActionListener(this);
if(i==0)
jbtn.setActionCommand("ekle");
jpn.add(jbtn);
}
}
baglan();
if(conn==null)
{
JOptionPane.showMessageDialog(this, "Veritabanı Bağlantı Hatası!");
System.exit(0);
}
cb_doldur();
jl_doldur();
this.add(jpn_main);
this.setVisible(true);
}
private void baglan()
{
try{
Class.forName("org.sqlite.JDBC");
conn=DriverManager.getConnection("jdbc:sqlite:hafta11ders1.db");
}
catch(Exception ex)
{
}
}
private void ekle()
{
try{
int secilen = jcb.getSelectedIndex();
if(secilen>-1)
{
for(int i=0;i<jtf.length;i++)
Integer.parseInt(jtf[i].getText());
String tarifeid = vc_cb[1].get(secilen);
String sql = "insert into fatura(tarifeid,kul_mb,kul_dk,kul_sms) values(?,?,?,?);";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, tarifeid);
ps.setString(2, jtf[0].getText());
ps.setString(3, jtf[1].getText());
ps.setString(4, jtf[2].getText());
ps.executeUpdate();
for(int i=0;i<jtf.length;i++)
jtf[i].setText("");
jcb.setSelectedIndex(0);
}
}catch(Exception ex)
{
}
}
private void sil()
{
try{
int secilen = jl[0].getSelectedIndex();
if(secilen>-1)
{
String faturaid = vc_jl[vc_jl.length-1].get(secilen); // fatura id bilgisi icin son liste
String sql="delete from fatura where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, faturaid);
ps.executeUpdate();
}
}
catch(Exception ex)
{
}
}
private void cb_doldur()
{
for(int i =0;i<vc_cb.length;i++)
vc_cb[i].clear();
try{
String sql="select adi,id from tarife order by adi;";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
vc_cb[0].add(rs.getString("adi"));
vc_cb[1].add(rs.getString("id"));
/*for(int i =0;i<vc_cb.length;i++)
vc_cb[i].add(rs.getString(i));*/
}
if(vc_cb[0].size()>0)
jcb.setSelectedIndex(0);
}
catch(Exception ex)
{
}
}
private void jl_doldur()
{
for(int i =0;i<vc_jl.length;i++){
//vc_jl[i].clear();
vc_jl[i].clear();
}
try{
String sql="select t.adi,f.kul_mb,f.kul_dk,f.kul_sms, (t.sabitucret + f.kul_mb*t.asim_mb + f.kul_dk*t.asim_dk + f.kul_sms*t.asim_sms )*1.18 as tutar, f.id from fatura f inner join tarife t on t.id=f.tarifeid;";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
for(int i =0;i<vc_jl.length;i++)
vc_jl[i].add(rs.getString(i+1));
}
}
catch(Exception ex)
{
}
/*for(int i=0;i<jl.length;i++)
jl[i].setListData(vc_jl[i]);*/
for(int i=0;i<jl.length;i++)
jl[i].updateUI(); // liste gorsellerini guncelle
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("ekle"))
ekle();
else
sil();
jl_doldur();
}
/**
* @param args
*/
public static void main(String[] args) {
new fatura();
}
}
NTP2 (Bahar-2016) Hafta-11