//Application Forms

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;

import com.fujinet.services.SanPhamService;
import com.fujinet.services.ThuChiService;

public class ThuChiFrm extends JFrame {

    private JLabel lbMaSanPham;
    private JTextField txtMaSanPham;
    private JLabel lbTenSanPham;
    private JLabel lbNgayThuChi;
    private JTextField txtNgayThuChi;
    private JButton btnTimKiem;
    private BorderLayout borderLayout;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panelA;
    protected SanPhamService sanPhamService;
    protected ThuChiService thuchiService;
    protected JTable table;
    protected JScrollPane scrollPane;
    private JPanel panelB;

    public ThuChiFrm() {
        borderLayout = new BorderLayout();
        panelA = new JPanel(new GridLayout(2, 1));
        panel1 = new JPanel(new GridLayout(1, 6));
        panel2 = new JPanel();
        scrollPane =new JScrollPane();
        panelB = new JPanel(new BorderLayout());

        lbMaSanPham = new JLabel("Ma San Pham:");
        lbTenSanPham = new JLabel();
        txtMaSanPham = new JTextField();
        txtMaSanPham.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent arg0) {}

            public void keyPressed(KeyEvent arg0) {}

            public void keyReleased(KeyEvent arg0) {
                sanPhamService = new SanPhamService();
                String ma_san_pham = txtMaSanPham.getText().trim();
                String ten_san_pham = sanPhamService.layTenSanPham(ma_san_pham);
                lbTenSanPham.setOpaque(true);
                lbTenSanPham.setBackground(Color.CYAN);
                if(ten_san_pham!=null)
                    lbTenSanPham.setText(ten_san_pham);
                else
                    lbTenSanPham.setText("Khong ton tai MaSP nay!");

            }
        });

        lbNgayThuChi = new JLabel("Ngay Thu Chi:");
        txtNgayThuChi = new JTextField();
        btnTimKiem = new JButton("Tim kiem");
        btnTimKiem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String maSp = txtMaSanPham.getText().trim();
                String ngayThuChi_chuoi = txtNgayThuChi.getText().trim();
                if(!maSp.equals("")&&!ngayThuChi_chuoi.equals("")){
                    //chuyen doi chuoi ngay sang Date
                    Date ngay_thu_chi = doi_chuoi_sang_ngay(ngayThuChi_chuoi,"dd/MM/yyyy");
                    if(ngay_thu_chi!=null){
                        thuchiService = new ThuChiService();
                        //Khai bao ten cot bang hien thi
                        Vector<String> columnNames = new Vector<String>();
                        columnNames.addElement("STT");
                        columnNames.addElement("Ma San Pham");
                        columnNames.addElement("Loai");
                        columnNames.addElement("Tien chi");
                        columnNames.addElement("Tien thu");
                        columnNames.addElement("Thanh tien");
                        Vector<Vector> data = thuchiService.DanhSachThuChi(maSp, ngay_thu_chi);
                    if(data==null){
                        remove(panelB);
                        remove(scrollPane);
                        repaint();

                        JLabel lbMessage = new JLabel("Khong ton tai du lieu nay");
                        panelB = new JPanel();
                        panelB.add(lbMessage);
                        add(panelB, borderLayout.SOUTH);    

                    }else{
                        table = new JTable(data,columnNames);
                        for(int i=0;i<columnNames.size();i++){
                            table.getColumnModel().getColumn(i).setCellRenderer(new TableCellRenderer() {

                                public Component getTableCellRendererComponent(JTable table, Object value,
                                        boolean isSelected, boolean hasFocus, int row, int column) {
                                    JLabel lb = new JLabel();
                                    lb.setText(value.toString());
                                    lb.setOpaque(true);
                                    if(row%2==0){
                                        lb.setBackground(Color.GRAY);
                                    }else{
                                        lb.setBackground(getBackground());
                                    }
                                    float thanh_tien= Float.parseFloat(table.getModel().getValueAt(row, 5).toString());
                                    if((thanh_tien<0)&&(column==5)){
                                        thanh_tien *= (-1);
                                        lb.setText(String.valueOf(thanh_tien));
                                        lb.setForeground(Color.RED);
                                    }else{
                                        lb.setForeground(getForeground());
                                    }
                                    return lb;
                                }
                            });
                        }

                        remove(panelB);
                        remove(scrollPane);
                        repaint();

                        scrollPane = new JScrollPane(table);
                        add(scrollPane,borderLayout.SOUTH);


                        }
                    setVisible(true);
                    }else{
                        JOptionPane.showConfirmDialog(null,"dd/MM/yyyy","Loi",JOptionPane.OK_CANCEL_OPTION);
                    }
                }else{
                    JOptionPane.showConfirmDialog(null,"CanNhap gia tri vao","Loi",JOptionPane.OK_CANCEL_OPTION);
                }
            }
        });
        panel1.add(lbMaSanPham);
        panel1.add(txtMaSanPham);
        panel1.add(lbTenSanPham);
        panel1.add(lbNgayThuChi);
        panel1.add(txtNgayThuChi);
        panel1.add(btnTimKiem);
        panelA.add(panel1);
        panelA.add(panel2);

        add(panelA, borderLayout.NORTH);

        setTitle("Danh Sach Thu Chi Form");
        Rectangle rec = new Rectangle();
        rec.x = 100;
        rec.y = 100;
        rec.width = 600;
        rec.height = 600;
        setBounds(rec);
        setVisible(true);
    }

    protected Date doi_chuoi_sang_ngay(String ngayThuChi_chuoi,
            String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date ngay;
        try {
            ngay = sdf.parse(ngayThuChi_chuoi);
        } catch (ParseException e) {
            return null;
        }
        if (ngay != null) {
            // doi ngay sang chuoi
            String ngay_chuoi = sdf.format(ngay);
            if (ngay_chuoi.equals(ngayThuChi_chuoi))
                return ngay;
        }
        return null;
    }
}
//ThuChiService
import java.util.Date;
import java.util.Vector;

import com.fujinet.dao.ThuChiDao;

public class ThuChiService {

    ThuChiDao thuChiDao;
    public ThuChiService() {
        thuChiDao = new ThuChiDao();
    }

    public Vector<Vector> DanhSachThuChi(String maSanPham, Date ngayThuChi){
        return thuChiDao.layDanhSachThuChiDaXuLy(maSanPham, ngayThuChi);
    }

}


//ThuChiDAO
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import com.fujinet.connection.OracleConnection;

public class ThuChiDao {
    private OracleConnection oracleConnect;
    private Connection connect;

    public ThuChiDao() {
        oracleConnect = new OracleConnection();
        connect = oracleConnect.openConnect();
    }

    private Vector<Vector> layDanhSachThuChiBanDau(String maSanPham, Date ngayThuChi ){
        Vector<Vector> danhsach = new Vector<Vector>();
        String sql = "Select * From TB_THUCHI Where MASP =? And NGAYTHUCHI =?";
        try {
            PreparedStatement pstm = this.connect.prepareStatement(sql);
            pstm.setString(1, maSanPham);
            pstm.setDate(2, new java.sql.Date(ngayThuChi.getTime()));
            ResultSet rs = pstm.executeQuery();
            ResultSetMetaData metadata = rs.getMetaData();
            int soCot = metadata.getColumnCount();
            while(rs.next()){
                Vector<String> rowsValue = new Vector<String>(soCot);
                for(int i=1;i<=soCot; i++){
                    rowsValue.addElement(rs.getString(i));
                }
                danhsach.addElement(rowsValue);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        oracleConnect.closeConnect();
        return danhsach;
    }

    public Vector<Vector> layDanhSachThuChiDaXuLy(String maSanPham, Date ngayThuChi){
        Vector<Vector> danhSachThuChiBanDau = layDanhSachThuChiBanDau(maSanPham, ngayThuChi);
        if(danhSachThuChiBanDau.size()>0){
            Vector<Vector> dsLucSau =new Vector<Vector>();
            //so hang = so luong hang co trong CSDL
            float thanh_tien = 0.0f;
            int soHang = danhSachThuChiBanDau.size();
            //so cot moi = so cot Bang CSDL + 1 cot moi
            int soCot = danhSachThuChiBanDau.elementAt(0).size()+1;
            int stt = 0;
            for(int i = 0; i<soHang;i++){
                stt++;
                Vector<String> gia_tri_hang_luc_dau = danhSachThuChiBanDau.elementAt(i);
                Vector<String> gia_tri_hang_luc_sau = new Vector<String>(soCot);
                for(int j=0;j<soCot; j++){
                    if(j==0){
                        //Xu ly cot STT
                        String so_thu_tu = xuLySTT(stt);
                        gia_tri_hang_luc_sau.addElement(so_thu_tu);
                    }else if(j==1){
                        //Xu ly cot MaSP
                        gia_tri_hang_luc_sau.addElement(gia_tri_hang_luc_dau.elementAt(2));
                    }else if(j==2){
                        //Xu ly cot Loai
                        String ma_loai = gia_tri_hang_luc_dau.elementAt(1);
                        String ten_loai = xuLyTenLoai(ma_loai);
                        gia_tri_hang_luc_sau.addElement(ten_loai);
                    }else if(j==3){
                        //Xu ly cot Tien chi ra
                        String ma_loai = gia_tri_hang_luc_dau.elementAt(1);
                        //Neu ma_loai la "M" la tien chi ra

                        if(ma_loai.equals("M")){
                            float so_tien_chi = Float.parseFloat(gia_tri_hang_luc_dau.elementAt(3));
                            thanh_tien -= so_tien_chi;
                            gia_tri_hang_luc_sau.addElement(String.valueOf(so_tien_chi));
                        }
                        //nguoc lai ma_loai la "B" la tien thu vao nen gia tri la ""
                        else
                            gia_tri_hang_luc_sau.addElement("");
                    }else if(j==4){
                        //Xu ly cot Tien thu vao
                        String ma_loai = gia_tri_hang_luc_dau.elementAt(1);
                        //Neu ma_loai la 'B' la tien chi ra

                        if(ma_loai.equals("B")){
                            float so_tien_thu = Float.parseFloat(gia_tri_hang_luc_dau.elementAt(3));
                            thanh_tien += so_tien_thu;
                            gia_tri_hang_luc_sau.addElement(String.valueOf(so_tien_thu));
                        }
                        //nguoc lai ma_loai la 'M' la tien thu vao nen gia tri la ""
                        else
                            gia_tri_hang_luc_sau.addElement("");
                    }else if(j==5){
                        //Xu ly cot Thanh Tien
                        gia_tri_hang_luc_sau.addElement(String.valueOf(thanh_tien));
                    }
                }
                dsLucSau.addElement(gia_tri_hang_luc_sau);
            }
            return dsLucSau;
        }else{
            return null;
        }
    }

    private String xuLyTenLoai(String ma_loai) {
        if(ma_loai.equals("M"))
            return "Mua";
        else
            return "Ban";
    }

    private String xuLySTT(int stt) {
        // TODO Auto-generated method stub
        String stt_chuoi = String.valueOf(stt);
        int do_dai_stt = stt_chuoi.length();
        switch(do_dai_stt){
            case 1:
                stt_chuoi = "00000"+stt_chuoi;
                break;
            case 2:
                stt_chuoi = "0000"+stt_chuoi;
                break;
            case 3:
                stt_chuoi = "000"+stt_chuoi;
                break;
            case 4:
                stt_chuoi = "00"+stt_chuoi;
                break;
            case 5:
                stt_chuoi = "0"+stt_chuoi;
                break;
            default:
                stt_chuoi = stt_chuoi;
                break;
        }
        return stt_chuoi;
    }
}
//SanPhamDao
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.fujinet.connection.OracleConnection;

public class SanPhamDao {


    private OracleConnection oracleConnect;
    private Connection connect;

    public SanPhamDao() {
        oracleConnect = new OracleConnection();
        connect = oracleConnect.openConnect();
    }

    public String layTenSanPham(String maSanPham){
        String tenSanPham = null;
        String sql = "Select TenSP From TB_SANPHAM Where MASP =?";
        try {
            PreparedStatement pstm = this.connect.prepareStatement(sql);
            pstm.setString(1, maSanPham);
            ResultSet rs = pstm.executeQuery();
            if(rs.next()){
                tenSanPham = rs.getString("TenSP");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            oracleConnect.closeConnect();
            return tenSanPham;
        }

    }
}

//OracleConnnection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleConnection {
    private Connection conn;

    public Connection openConnect() {

        String driver = "oracle.jdbc.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, "QLThuChi", "thuchi");

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public void closeConnect() {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

How can we help you? :) Do you have a question or did you mean to add this to our code snippet library?

This question has already been answered. Start a new discussion instead.