Advertisement
Guest User

Untitled

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