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.util.*;
- 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 static void main(String[] args) {
- new Buy();
- }
- public boolean notZero(int qtsold) {
- qtsold = (Integer)quantity.getValue();
- if(qtsold < 1)
- return false;
- else
- return true;
- }
- public boolean cekStock(int qtsold, int stock) {
- qtsold = (Integer)quantity.getValue();
- stock = Integer.parseInt((String) tableClothes.getValueAt(0, 4));
- 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 stock = Integer.parseInt((String) tableClothes.getValueAt(0, 4));
- if(!notZero(qtsold))
- JOptionPane.showMessageDialog(this, "Quantity must be 1 one or more!");
- if(!cekPencet(tableClothes)) {
- JOptionPane.showMessageDialog(this,"Select a data first!");
- }
- if(!cekStock(qtsold, stock)) {
- JOptionPane.showMessageDialog(this,"Quantity is more than stock!");
- }
- try {
- if(cekPencet(tableClothes) && notZero(qtsold) && cekStock(qtsold, stock)){
- int rowId = tableClothes.getSelectedRow();
- String clothesId = tableClothes.getValueAt(rowId, 0) + "";
- String qtsold1 = quantity.getValue().toString();
- try {
- con.updateStock(clothesId, qtsold1);
- JOptionPane.showMessageDialog(this,"Succesfully purchased clothes");
- fillTable();
- }catch(Exception e1) {
- e1.printStackTrace();
- JOptionPane.showMessageDialog(this, e1);
- }
- }
- }catch(Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement