Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Vector;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- public class Buy extends JFrame implements ActionListener{
- JPanel panelHeader, panelContent, panelFooter;
- Vector tableHeader;
- Vector<Vector> tableContent;
- DefaultTableModel dtm;
- JTable tableClothes;
- JLabel lblTitle, lblQuantity;
- JSpinner quantity;
- JButton buyClothes;
- Connect con;
- void Buy() {
- panelHeader = new JPanel();
- panelContent = new JPanel();
- panelContent.setLayout(new BorderLayout());
- panelFooter = new JPanel();
- panelFooter.setLayout(new GridLayout(2,1));
- //header
- lblTitle = new JLabel("Buy Clothes");
- panelHeader.add(lblTitle);
- //content
- tableContent = new Vector(); //2 Dimensi
- tableHeader = new Vector(); // 1 Dimensi
- tableHeader.add("ID");
- tableHeader.add("Type");
- tableHeader.add("Name");
- tableHeader.add("Brand");
- tableHeader.add("Stock");
- tableHeader.add("Price");
- tableClothes = new JTable() {
- public boolean isCellEditable(int row, int column) {
- return false;
- };
- };
- fillTable();
- tableClothes.getTableHeader().setReorderingAllowed(false);
- JScrollPane jsp = new JScrollPane(tableClothes);
- panelContent.add(jsp);
- //footer
- lblQuantity = new JLabel("Quantity");
- SpinnerModel value = new SpinnerNumberModel(0,0,10,1);
- quantity = new JSpinner(value);
- buyClothes = new JButton("Buy Clothes");
- JPanel panelQuantity = new JPanel();
- panelQuantity.add(lblQuantity);
- panelQuantity.add(quantity);
- panelFooter.add(panelQuantity);
- panelFooter.add(buyClothes);
- add(panelHeader, BorderLayout.NORTH);
- add(panelContent, BorderLayout.CENTER);
- add(panelFooter, BorderLayout.SOUTH);
- setSize(800,400);
- setTitle("Buy Clothes");
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- setLocationRelativeTo(null);
- buyClothes.addActionListener(this);
- }
- public Buy() {
- con = new Connect();
- Buy();
- }
- void fillTable(){
- dtm = new DefaultTableModel(tableHeader, 0);
- //udah bikin header tapi ga ada ada
- //ResultSet //table dari DB beda JTable
- String query = "SELECT clothesId, clothingTypeName, clothesName, brand, stock, price "
- + "FROM clothes c, clothingtype ct "
- + "WHERE c.typeId = ct.typeId ";
- //* -> keypad
- ResultSet rs = con.executeQuery(query);
- try {
- while(rs.next()){ //rs.next() -> return true kalau dia masih ada data setelah
- //ambil data dari kolom 1 s/d 4
- String clothesId = rs.getObject(1)+"";
- String clothingTypeName = rs.getObject(2)+"";
- String clothesName = rs.getObject(3)+"";
- String brand = rs.getObject(4)+"";
- String stock = rs.getObject(5)+"";
- String price = rs.getObject(6)+"";
- Vector row = new Vector<>();
- row.add(clothesId);
- row.add(clothingTypeName);
- row.add(clothesName);
- row.add(brand);
- row.add(stock);
- row.add(price);
- dtm.addRow(row);
- }
- tableClothes.setModel(dtm);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public boolean notZero (int qtsold) {
- qtsold = (Integer)quantity.getValue();
- if(qtsold < 1 )
- return false;
- else
- return true;
- }
- public boolean cekStock (int qtsold) {
- qtsold = (Integer)quantity.getValue();
- int rowId = tableClothes.getSelectedRow();
- int stock = Integer.parseInt(tableClothes.getValueAt(rowId, 4).toString());
- if(qtsold > stock)
- return false;
- else
- return true;
- }
- public boolean cekPencet(JTable table) {
- if(table.getSelectionModel().isSelectionEmpty())
- return false;
- else
- return true;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if(e.getSource() == buyClothes) {
- int qtsold = (Integer)quantity.getValue();
- int rowId = tableClothes.getSelectedRow();
- if(!cekPencet (tableClothes))
- JOptionPane.showMessageDialog(this,"Select a data!");
- if(!notZero(qtsold))
- JOptionPane.showMessageDialog(this, "Zero quantity!");
- try {
- if(!cekStock(qtsold))
- JOptionPane.showMessageDialog(this, "Over dong");
- }catch(Exception e2){}
- try {
- if(cekPencet(tableClothes) && notZero(qtsold) && cekStock(qtsold) ){
- rowId = tableClothes.getSelectedRow(); // row yang di select sama JTable
- String clothesId = tableClothes.getValueAt(rowId, 0) + "";
- qtsold = (Integer)quantity.getValue();
- try {
- con.updateStock(clothesId, qtsold);
- int userId = Integer.parseInt(Login.currentId);
- int purchaseRow = tableClothes.getSelectedRow();
- int _clothesId = Integer.parseInt( (String) tableClothes.getValueAt(purchaseRow, 0));
- int qty = (Integer)quantity.getValue();
- con.insertHeaderTransaction(userId);
- con.insertDetailTransaction(_clothesId, qty);
- JOptionPane.showMessageDialog(this,"Succesfully purchased clothes!");
- fillTable();
- }catch(Exception e1) {
- e1.printStackTrace();
- JOptionPane.showMessageDialog(this, e1);
- }
- }
- }catch(Exception e3) {
- e3.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement