Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Vector;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.table.DefaultTableModel;
  12.  
  13. public class Buy extends JFrame implements ActionListener{
  14. JPanel panelHeader, panelContent, panelFooter;
  15. Vector tableHeader;
  16. Vector<Vector> tableContent;
  17. DefaultTableModel dtm;
  18. JTable tableClothes;
  19. JLabel lblTitle, lblQuantity;
  20. JSpinner quantity;
  21. JButton buyClothes;
  22. Connect con;
  23.  
  24. void Buy() {
  25.  
  26. panelHeader = new JPanel();
  27. panelContent = new JPanel();
  28. panelContent.setLayout(new BorderLayout());
  29. panelFooter = new JPanel();
  30. panelFooter.setLayout(new GridLayout(2,1));
  31.  
  32. //header
  33. lblTitle = new JLabel("Buy Clothes");
  34. panelHeader.add(lblTitle);
  35.  
  36. //content
  37. tableContent = new Vector(); //2 Dimensi
  38. tableHeader = new Vector(); // 1 Dimensi
  39. tableHeader.add("ID");
  40. tableHeader.add("Type");
  41. tableHeader.add("Name");
  42. tableHeader.add("Brand");
  43. tableHeader.add("Stock");
  44. tableHeader.add("Price");
  45.  
  46. tableClothes = new JTable() {
  47. public boolean isCellEditable(int row, int column) {
  48. return false;
  49. };
  50. };
  51.  
  52. fillTable();
  53.  
  54. tableClothes.getTableHeader().setReorderingAllowed(false);
  55. JScrollPane jsp = new JScrollPane(tableClothes);
  56. panelContent.add(jsp);
  57.  
  58. //footer
  59.  
  60. lblQuantity = new JLabel("Quantity");
  61. SpinnerModel value = new SpinnerNumberModel(0,0,10,1);
  62. quantity = new JSpinner(value);
  63.  
  64. buyClothes = new JButton("Buy Clothes");
  65.  
  66. JPanel panelQuantity = new JPanel();
  67. panelQuantity.add(lblQuantity);
  68. panelQuantity.add(quantity);
  69.  
  70. panelFooter.add(panelQuantity);
  71. panelFooter.add(buyClothes);
  72.  
  73. add(panelHeader, BorderLayout.NORTH);
  74. add(panelContent, BorderLayout.CENTER);
  75. add(panelFooter, BorderLayout.SOUTH);
  76. setSize(800,400);
  77. setTitle("Buy Clothes");
  78. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  79. setVisible(true);
  80. setLocationRelativeTo(null);
  81.  
  82. buyClothes.addActionListener(this);
  83. }
  84.  
  85. public Buy() {
  86. con = new Connect();
  87. Buy();
  88.  
  89. }
  90.  
  91. void fillTable(){
  92. dtm = new DefaultTableModel(tableHeader, 0);
  93. //udah bikin header tapi ga ada ada
  94. //ResultSet //table dari DB beda JTable
  95.  
  96. String query = "SELECT clothesId, clothingTypeName, clothesName, brand, stock, price "
  97. + "FROM clothes c, clothingtype ct "
  98. + "WHERE c.typeId = ct.typeId ";
  99. //* -> keypad
  100.  
  101. ResultSet rs = con.executeQuery(query);
  102.  
  103. try {
  104. while(rs.next()){ //rs.next() -> return true kalau dia masih ada data setelah
  105. //ambil data dari kolom 1 s/d 4
  106. String clothesId = rs.getObject(1)+"";
  107. String clothingTypeName = rs.getObject(2)+"";
  108. String clothesName = rs.getObject(3)+"";
  109. String brand = rs.getObject(4)+"";
  110. String stock = rs.getObject(5)+"";
  111. String price = rs.getObject(6)+"";
  112.  
  113. Vector row = new Vector<>();
  114. row.add(clothesId);
  115. row.add(clothingTypeName);
  116. row.add(clothesName);
  117. row.add(brand);
  118. row.add(stock);
  119. row.add(price);
  120.  
  121. dtm.addRow(row);
  122. }
  123. tableClothes.setModel(dtm);
  124. } catch (SQLException e) {
  125. // TODO Auto-generated catch block
  126. e.printStackTrace();
  127. }
  128. }
  129.  
  130. public boolean notZero (int qtsold) {
  131. qtsold = (Integer)quantity.getValue();
  132. if(qtsold < 1 )
  133. return false;
  134. else
  135. return true;
  136.  
  137. }
  138.  
  139.  
  140. public boolean cekStock (int qtsold) {
  141. qtsold = (Integer)quantity.getValue();
  142. int rowId = tableClothes.getSelectedRow();
  143. int stock = Integer.parseInt(tableClothes.getValueAt(rowId, 4).toString());
  144. if(qtsold > stock)
  145. return false;
  146. else
  147. return true;
  148. }
  149.  
  150.  
  151. public boolean cekPencet(JTable table) {
  152. if(table.getSelectionModel().isSelectionEmpty())
  153. return false;
  154. else
  155. return true;
  156. }
  157.  
  158. @Override
  159. public void actionPerformed(ActionEvent e) {
  160. // TODO Auto-generated method stub
  161. if(e.getSource() == buyClothes) {
  162. int qtsold = (Integer)quantity.getValue();
  163. int rowId = tableClothes.getSelectedRow();
  164.  
  165. if(!cekPencet (tableClothes))
  166. JOptionPane.showMessageDialog(this,"Select a data!");
  167.  
  168. if(!notZero(qtsold))
  169. JOptionPane.showMessageDialog(this, "Zero quantity!");
  170.  
  171. try {
  172. if(!cekStock(qtsold))
  173. JOptionPane.showMessageDialog(this, "Over dong");
  174. }catch(Exception e2){}
  175.  
  176. try {
  177. if(cekPencet(tableClothes) && notZero(qtsold) && cekStock(qtsold) ){
  178. rowId = tableClothes.getSelectedRow(); // row yang di select sama JTable
  179. String clothesId = tableClothes.getValueAt(rowId, 0) + "";
  180. qtsold = (Integer)quantity.getValue();
  181. try {
  182. con.updateStock(clothesId, qtsold);
  183. int userId = Integer.parseInt(Login.currentId);
  184.  
  185. int purchaseRow = tableClothes.getSelectedRow();
  186. int _clothesId = Integer.parseInt( (String) tableClothes.getValueAt(purchaseRow, 0));
  187. int qty = (Integer)quantity.getValue();
  188.  
  189. con.insertHeaderTransaction(userId);
  190. con.insertDetailTransaction(_clothesId, qty);
  191. JOptionPane.showMessageDialog(this,"Succesfully purchased clothes!");
  192. fillTable();
  193. }catch(Exception e1) {
  194. e1.printStackTrace();
  195. JOptionPane.showMessageDialog(this, e1);
  196. }
  197. }
  198. }catch(Exception e3) {
  199. e3.printStackTrace();
  200. }
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement