83
BACHELOR of INFORMATION TECHNOLOGY CS240KA, UiTM SHAH ALAM MINI PROJECT REPORT Semester: MARCH 2016 – JULY 2016 Course name: JAVA PROGRAMMING (CSC 444) Title: Stock Application Lecturer’s name: DR. ROSE HAFSAH ABD RAUF Students’ name Student’s ID Group Khamit Mateyev Kuanysh Konysbek Maksat Makhali 2015573383 2015513715 2015770435 CS240KB CS240KB CS240KB

read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

BACHELOR of INFORMATION TECHNOLOGY

CS240KA, UiTM SHAH ALAM

MINI PROJECT REPORT

Semester: MARCH 2016 – JULY 2016Course name: JAVA PROGRAMMING (CSC 444)Title: Stock ApplicationLecturer’s name: DR. ROSE HAFSAH ABD RAUF

Students’ name Student’s ID Group

Khamit MateyevKuanysh KonysbekMaksat Makhali

201557338320155137152015770435

CS240KBCS240KBCS240KB

Page 2: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

1. Description of the system

The System represents application for a stock. Application has UI for work with database.

There are two entities in database:

1. Categories entity (category_id INT PRIMARY KEY, name VARCHAR(100) UNIQUE NOT NULL );This entity is for information about type of a product.

2. Products entity (product_id INT PRIMARY KEY, name VARCHAR(100), description VARCHAR(100), price DOUBLE,quantity INT NOT NULL,barcode INT UNIQUE NOT NULL,category_id FK REFERENCES categories(category_id));This entity is for information about a products.

Using this application user can:

1. Add product/category into database.2. Edit product in database.3. Remove product from database.4. Search product in database.

Page 3: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

2. Algorithm of the system

DBConnection.java:

Page 4: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Entity.java:

Category.java:

Page 5: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Product.java:

Page 6: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 7: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

CategoryService.java:

Page 8: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

ProductService.java:

Page 9: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 10: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 11: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 12: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 13: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 14: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

MainWindow.java:

Page 15: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 16: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 17: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 18: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 19: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 20: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 21: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 22: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 23: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 24: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 25: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 26: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 27: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 28: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 29: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 30: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 31: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 32: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 33: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”
Page 34: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3. Source code listening

3.1 DBConnection.java

1. package malaysiajavaproj.model.JDBCPoint;2. import java.sql.ResultSet;3. import java.sql.Connection;4. import java.sql.DriverManager;5. import java.sql.SQLException;6. import java.sql.Statement;7.8. /**9. * Class that allows to connect with database10. * @author Admin11. */12. public class DBConnection {13. private final String userName = "postgres";14. private final String password = "12345678";15. private Connection conn = null;16. private Statement stmt = null; 17. public DBConnection() throws SQLException{18. conn =

DriverManager.getConnection("jdbc:postgresql://localhost:5432/stock", userName, password);

19. if(conn == null){20. throw new SQLException("Could not set connection with

database");21. }22. stmt = conn.createStatement();23. }24. /**25. * Method for queries returning table26. * @param query27. * @return ResultSet with table28. * @throws SQLException 29. */30. public ResultSet query(String query) throws SQLException{31. ResultSet result;32. result = stmt.executeQuery(query);33. System.out.println(query);34. return result;35. }36. /**37. * Method for queries with no table return38. * @param query39. * @return true if query executed successfully40. * @throws SQLException 41. */42. public boolean executeQuery(String query) throws SQLException{43. System.out.println(query);44. return !stmt.execute(query);45. }46. /**47. * Closes connection with database48. * @throws SQLException 49. */50. public void closeConnection() throws SQLException{51. if(!conn.isClosed() || conn != null)52. conn.close();53. }54.

Page 35: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

55. @Override56. protected void finalize() throws Throwable {57. super.finalize(); //To change body of generated methods,

choose Tools | Templates.58. closeConnection();59. }60. 61. }

Page 36: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.2 Entity.java

1. package malaysiajavaproj.model.javaclasses;

2. /** Class representing entity of database @author Admin

3. */4. public class Entity {5. protected int id;6. protected String name;7. /**

Default constructor. Sets id to -18. */9. public Entity(){10. id = -1;11. }12. /**13. *

@param id @param name

14. */15. public Entity(int id, String name){16. this.id = id;17. this.name = name;18. }19. /**

@return the id20. */21. public int getId() {22. return id;23. }

24. /** @param id the id to set

25. */26. public void setId(int id) {27. this.id = id;28. }

29. /** @return the name

30. */31. public String getName() {32. return name;33. }

34. /** @param name the name to set

35. */36. public void setName(String name) {37. this.name = name;38. }

39. }

Page 37: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.3 Product.java

1. package malaysiajavaproj.model.javaclasses;2. /**

Entity 'Products' @author Admin

3. */4. public class Product extends Entity{ 5. protected String description;6. protected double price;7. protected int quantity;8. protected int barcode;9. protected Category category;10. /**

@param id is a ID of the product @param name is a name of the product @param description is a description of the product @param price is a price of the product @param quantity is a quantity of the product @param barcode is a barcode of the product @param category is a category of the product

11. */12. public Product(int id, String name, String description, double

price, int quantity, int barcode, Category category){13. super(id, name);14. this.description = description;15. this.price = price;16. this.quantity = quantity;17. this.barcode = barcode;18. this.category = category;19. }20. /**

Sets id to -121. */22. public Product(){23. super();24. }

25. /** @return the description

26. */27. public String getDescription() {28. return description;29. }

30. /** @param description the description to set

31. */32. public void setDescription(String description) {33. this.description = description;34. }

35. /** @return the price

36. */37. public double getPrice() {38. return price;39. }

40. /** @param price the price to set

41. */42. public void setPrice(double price) {

Page 38: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

43. this.price = price;44. }

45. /** @return the quantity

46. */47. public int getQuantity() {48. return quantity;49. }

50. /** @param quantity the quantity to set

51. */52. public void setQuantity(int quantity) {53. this.quantity = quantity;54. }

55. /** @return the barcode

56. */57. public int getBarcode() {58. return barcode;59. }

60. /** @param barcode the barcode to set

61. */62. public void setBarcode(int barcode) {63. this.barcode = barcode;64. }

65. /** @return the category

66. */67. public Category getCategory() {68. return category;69. }

70. /** @param category the category to set

71. */72. public void setCategory(Category category) {73. this.category = category;74. }

75. public boolean equals(Product p1){76. return (this.id == p1.id || this.barcode == p1.barcode);77. }78. }

Page 39: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.4 Category.java

1. package malaysiajavaproj.model.javaclasses;

2. /** Entity 'Categories' @author Admin

3. */4. public class Category extends Entity{5. /**

@Entity 'Categories' @param id is ID of category @param name is Name of category

6. */7. public Category(int id, String name){8. super(id, name);9. }10. /**

If IDs or Names are equal, then Categories are equal @param c1 @return

11. */12. public boolean equals(Category c1){13. return (this.id == c1.id || this.name.equals(c1.name));14. }15. }

Page 40: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.5 ProductService.java

1. package malaysiajavaproj.model.javaclasses;

2. import java.sql.ResultSet;3. import java.sql.SQLException;4. import java.util.ArrayList;5. import java.util.List;6. import malaysiajavaproj.model.JDBCPoint.DBConnection;

7. /** Class representing business model for Products entity @author Admin

8. */9. public class ProductService {10. private DBConnection dbc = null;11. /**12. *

@throws Exception 13. */14. public ProductService() throws SQLException{15. dbc = new DBConnection();16. }17. /**

Method for getting all products from database @return List with all products @throws SQLException

18. */19. public List getAllProducts() throws SQLException{20. final String query = "SELECT * FROM products ORDER BY name ASC";21. CategoryService categoryService = new CategoryService();22. Category category;23. Product product;24. ResultSet rs = dbc.query(query);25. List<Product> result = new ArrayList<>(rs.getFetchSize());26. while(rs.next()){27. product = new Product();28. product.setId(rs.getInt("product_id"));29. product.setName(rs.getString("name"));30. product.setDescription(rs.getString("description"));31. product.setPrice(rs.getDouble("price"));32. product.setQuantity(rs.getInt("quantity"));33. product.setBarcode(rs.getInt("barcode"));34. category = categoryService.findById(rs.getInt("category_id"));35. product.setCategory(category);36. result.add(product);37. }38. return result;39. }

40. /** Method for finding product by ID in database @param id @return Product with given ID @throws SQLException

41. */42. public List findById(int id) throws SQLException{43. final String query = "SELECT * FROM products WHERE CAST(product_id

AS TEXT) like '" + id + "%'"44. + " ORDER BY name ASC";45. CategoryService categoryService = new CategoryService();46. Category category;

Page 41: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

47. Product product;48. ResultSet rs = dbc.query(query);49. List<Product> result = new ArrayList<>(rs.getFetchSize());50. while(rs.next()){51. product = new Product();52. product.setId(rs.getInt("product_id"));53. product.setName(rs.getString("name"));54. product.setDescription(rs.getString("description"));55. product.setPrice(rs.getDouble("price"));56. product.setQuantity(rs.getInt("quantity"));57. product.setBarcode(rs.getInt("barcode"));58. category = categoryService.findById(rs.getInt("category_id"));59. product.setCategory(category);60. result.add(product);61. }62. return result;63. }64. /**

Method for finding product by name in database @param name @return Product with given name @throws SQLException

65. */66. public List findByName(String name) throws SQLException{67. final String query = "SELECT * FROM products WHERE name like '" +

name + "%'" +68. " ORDER BY name ASC";69. CategoryService categoryService = new CategoryService();70. Category category;71. Product product;72. ResultSet rs = dbc.query(query);73. List<Product> result = new ArrayList<>(rs.getFetchSize());74. while(rs.next()){75. product = new Product();76. product.setId(rs.getInt("product_id"));77. product.setName(rs.getString("name"));78. product.setDescription(rs.getString("description"));79. product.setPrice(rs.getDouble("price"));80. product.setQuantity(rs.getInt("quantity"));81. product.setBarcode(rs.getInt("barcode"));82. category = categoryService.findById(rs.getInt("category_id"));83. product.setCategory(category);84. result.add(product);85. }86. return result;87. }88. /**

Method for finding products by category @param category @return List of products @throws SQLException

89. */90. public List getAllProductsByCategory(Category category) throws

SQLException{91. final String query = "SELECT * FROM products WHERE category_id = "

+ category.getId()92. + " ORDER BY name ASC";93. CategoryService categoryService = new CategoryService();94. Category category1;95. Product product;96. ResultSet rs = dbc.query(query);97. List<Product> result = new ArrayList<>(rs.getFetchSize());98. while(rs.next()){99. product = new Product();

Page 42: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

100. product.setId(rs.getInt("product_id"));101. product.setName(rs.getString("name"));102. product.setDescription(rs.getString("description"));103. product.setPrice(rs.getDouble("price"));104. product.setQuantity(rs.getInt("quantity"));105. product.setBarcode(rs.getInt("barcode"));106. category1 = categoryService.findById(rs.getInt("category_id"));107. product.setCategory(category1);108. result.add(product);109. }110. return result;111. }112. /**

Method for adding product into database @param product @throws SQLException

113. */114. public boolean addProduct(Product product) throws SQLException{115. final String query = "INSERT INTO products(name, description,

price, quantity, barcode, category_id) "116. + "values('" + product.getName() + "', '" +

product.getDescription() + "', "117. + product.getPrice() + ", " + product.getQuantity() + "," +

product.getBarcode() + ", " 118. + product.getCategory().getId() + ");";119. return dbc.executeQuery(query);120. }121. /**

Method for removing product from database @param product @throws SQLException

122. */123. public boolean removeProduct(Product product) throws SQLException

{124. final String query = "DELETE FROM products WHERE product_id = " +

product.getId();125. return dbc.executeQuery(query);126. }127. /**

Method for updating product @param product @return @throws SQLException

128. */129. public boolean updateProduct(Product product) throws SQLException{130. final String query = "UPDATE products SET name = '" +

product.getName() + "',"131. + " description = '" + product.getDescription() + "',"132. + "price = " + product.getPrice() + ", "133. + "quantity = " + product.getQuantity() + ", "134. + "barcode = " + product.getBarcode() + ", "135. + "category_id = " + product.getCategory().getId()136. + " WHERE product_id = " + product.getId();137. return dbc.executeQuery(query);138. }139. }

Page 43: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.6 CategoryService.java

140. package malaysiajavaproj.model.javaclasses;

141. import java.sql.ResultSet;142. import java.sql.SQLException;143. import java.util.ArrayList;144. import java.util.List;145. import malaysiajavaproj.model.JDBCPoint.DBConnection;

146. /** Class representing business model for categories @author Admin

147. */148. public class CategoryService {149. private DBConnection dbc;150. /**151. *

@throws SQLException 152. */153. public CategoryService() throws SQLException{154. dbc = new DBConnection();155. }156. /**

Method for getting all categories from database @return List with all categories @throws SQLException

157. */158. public List getAllCategories() throws SQLException{159. final String query = "SELECT * FROM categories ORDER BY name ASC";160. ResultSet rs = dbc.query(query);161. List<Category> result = new ArrayList<>(rs.getFetchSize());162. Category category;163. while(rs.next()){164. category = new Category(rs.getInt("category_id"),

rs.getString("name"));165. result.add(category);166. }167. return result;168. }169. /**

Method for finding category by ID @param id @return Category with given ID @throws SQLException

170. */171. public Category findById(int id) throws SQLException{172. final String query = "SELECT * FROM categories WHERE category_id =

" + id;173. ResultSet rs = dbc.query(query);174. rs.next();175. Category result = new Category(rs.getInt("category_id"),

rs.getString("name"));176. return result;177. }178. /**

Method for finding category by name @param name @return Category with given name @throws SQLException

179. */180. public Category findByName(String name) throws SQLException{

Page 44: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

181. final String query = "SELECT * FROM categories WHERE name = '" + name + "'";

182. ResultSet rs = dbc.query(query);183. rs.next();184. Category result = new Category(rs.getInt("category_id"),

rs.getString("name"));185. return result;186. }187. /**

Method for adding category into database @param category @throws SQLException

188. */189. public boolean addCategory(Category category) throws SQLException{190. final String query = "INSERT INTO categories(name) VALUES('" +

category.getName() + "');";191. return dbc.executeQuery(query);192. }193. }

Page 45: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.7 MainWindow.java

1. package malaysiajavaproj.view;2. import java.io.File;3. import java.io.FileNotFoundException;4. import java.io.PrintStream;5. import java.sql.SQLException;6. import java.util.ArrayList;7. import java.util.Date;8. import java.util.Iterator;9. import java.util.List;10. import javax.swing.DefaultCellEditor;11. import javax.swing.ImageIcon;12. import javax.swing.JOptionPane;13. import javax.swing.table.TableCellEditor;14. import malaysiajavaproj.model.javaclasses.*;15. /**

Main class with GUI and main method @author Admin

16. */17. public class MainWindow extends javax.swing.JFrame {

18. /** Creates new form MainWindow

19. */

20. private ArrayList<Product> products = null;21. private ArrayList<Category> categories = null;22. private CategoryService cs = null;23. private ProductService ps = null;24. private boolean editing = false;25. public MainWindow() throws SQLException, FileNotFoundException{26. cs = new CategoryService();27. ps = new ProductService();28. //loadCategoriesIntoComboBox();29. initComponents();30. ImageIcon img = new ImageIcon("C:\\Users\\Admin\\Documents\\

NetBeansProjects\\MalaysiaJavaProj\\sico.png");31. jFrame1.setIconImage(img.getImage());32. this.setIconImage(img.getImage());33. loadData();34. System.setErr(new PrintStream(new File("log.log")));35. mainWindow = this;36. }37. /**

This method cleans add form38. */39. private void cleanAddProductForm(){40. jTextField2.setText(null);41. jTextField3.setText(null);42. jTextField4.setText(null);43. jTextField5.setText(null);44. jTextField6.setText(null);45. }46. /**

This method cleans add category form47. */48. private void cleanAddCategoryForm(){49. jTextArea1.setText(null);50. }51. /**

This method loads list of categories into ComboBox

Page 46: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

52. */53. private void loadCategoriesIntoComboBox(){54. jComboBox1.removeAllItems();55. for(Iterator it = categories.iterator(); it.hasNext(); ){56. jComboBox1.addItem(((Category )it.next()).getName());57. }58. }59. /**

This method loads/refreshed data from database @throws SQLException

60. */61. private void loadData() throws SQLException{62. loadCategoriesFromDB();63. loadProductsFromDB();64. loadProductsIntoList(products);65. clearTable();66. loadCategoriesIntoComboBox();67. }68. /**

This methods loads products from database into ArrayList 'products'

@throws SQLException 69. */70. private void loadProductsFromDB() throws SQLException{71. products = (ArrayList<Product>) ps.getAllProducts();72. }73. /**

This method loads categories from database into ArrayList 'categories'

@throws SQLException 74. */75. private void loadCategoriesFromDB() throws SQLException{76. categories = (ArrayList<Category>) cs.getAllCategories();77. }78. /**

This method loads a given product into table @param p

79. */80. private void loadProductIntoTable(Product p){81. jTable1.getModel().setValueAt(p.getId(), 0, 1);82. jTable1.getModel().setValueAt(p.getName(), 1, 1);83. jTable1.getModel().setValueAt(p.getBarcode(), 2, 1);84. jTable1.getModel().setValueAt(p.getPrice(), 3, 1);85. jTable1.getModel().setValueAt(p.getQuantity(), 4, 1);86. jTable1.getModel().setValueAt(p.getCategory().getName(), 5, 1);87. jTextField1.setText(p.getDescription());88. }89. /**

This method loads products from ArrayList 'products' into List Component

90. */91. private void loadProductsIntoList(List products){92. try{93. String s[] = new String[products.size()];94. int i = 0;95. for(Iterator it = products.iterator(); it.hasNext(); i++){96. s[i] = ((Product)it.next()).getName();97. }98. jList1.setModel(99. new javax.swing.AbstractListModel() {100. String[] strings = s;

101. public int getSize() { return strings.length; }102. public Object getElementAt(int i) { return strings[i]; }

Page 47: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

103. }104. );105. } catch(Exception e){106. showMessage("An error occured. " + e.getMessage());107. e.printStackTrace();108. }109. }110. /**

This method clears the table111. */112. private void clearTable(){113. jTable1.getModel().setValueAt(null, 0, 1);114. jTable1.getModel().setValueAt(null, 1, 1);115. jTable1.getModel().setValueAt(null, 2, 1);116. jTable1.getModel().setValueAt(null, 3, 1);117. jTable1.getModel().setValueAt(null, 4, 1);118. jTable1.getModel().setValueAt(null, 5, 1);119. jTextField1.setText(null);120. }121. /**

This method is called from within the constructor to initialize the form.

WARNING: Do NOT modify this code. The content of this method is always

regenerated by the Form Editor.122. */123. @SuppressWarnings("unchecked")124. // <editor-fold defaultstate="collapsed" desc="Generated

Code">//GEN-BEGIN:initComponents125. private void initComponents() {

126. jFrame1 = new javax.swing.JFrame();127. jTabbedPane2 = new javax.swing.JTabbedPane();128. jPanel1 = new javax.swing.JPanel();129. jTextField2 = new javax.swing.JTextField();130. jTextField3 = new javax.swing.JTextField();131. jTextField4 = new javax.swing.JTextField();132. jTextField5 = new javax.swing.JTextField();133. jComboBox1 = new javax.swing.JComboBox();134. jScrollPane4 = new javax.swing.JScrollPane();135. jTextArea1 = new javax.swing.JTextArea();136. jButton6 = new javax.swing.JButton();137. jPanel2 = new javax.swing.JPanel();138. jTextField6 = new javax.swing.JTextField();139. jButton7 = new javax.swing.JButton();140. jScrollPane1 = new javax.swing.JScrollPane();141. jList1 = new javax.swing.JList();142. jScrollPane2 = new javax.swing.JScrollPane();143. jTable1 = new javax.swing.JTable(){144. public TableCellEditor getCellEditor(int row, int column){145. DefaultCellEditor dce1 = new DefaultCellEditor(jComboBox1);146. int modelColumn = convertColumnIndexToModel( column );147. if(modelColumn == 1 && row == 0)148. return null;149. if (modelColumn == 1 && row == 5)150. return dce1;151. return super.getCellEditor(row, column);152. }

153. };154. jTextField1 = new javax.swing.JTextField();155. jScrollPane3 = new javax.swing.JScrollPane();156. jTextPane1 = new javax.swing.JTextPane();157. jButton1 = new javax.swing.JButton();

Page 48: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

158. jButton2 = new javax.swing.JButton();159. jButton3 = new javax.swing.JButton();160. jButton4 = new javax.swing.JButton();161. jButton5 = new javax.swing.JButton();162. jTextField7 = new javax.swing.JTextField();163. jMenuBar1 = new javax.swing.JMenuBar();164. jMenu1 = new javax.swing.JMenu();

165. jFrame1.setTitle("Add");166. jFrame1.setLocation(new java.awt.Point(0, 0));167. jFrame1.setLocationByPlatform(true);168. jFrame1.setResizable(false);169. jFrame1.setSize(new java.awt.Dimension(280, 410));

170. jTextField2.setText("Enter name");171. jTextField2.setToolTipText("Enter name");172. jTextField2.addMouseListener(new java.awt.event.MouseAdapter() {173. public void mouseClicked(java.awt.event.MouseEvent evt) {174. jTextField2MouseClicked(evt);175. }176. });

177. jTextField3.setText("Enter barcode");178. jTextField3.setToolTipText("Enter barcode");179. jTextField3.addMouseListener(new java.awt.event.MouseAdapter() {180. public void mouseClicked(java.awt.event.MouseEvent evt) {181. jTextField3MouseClicked(evt);182. }183. });

184. jTextField4.setText("Enter price");185. jTextField4.setToolTipText("Enter price");186. jTextField4.addMouseListener(new java.awt.event.MouseAdapter() {187. public void mouseClicked(java.awt.event.MouseEvent evt) {188. jTextField4MouseClicked(evt);189. }190. });

191. jTextField5.setText("Enter quantity");192. jTextField5.setToolTipText("Enter quantity");193. jTextField5.addMouseListener(new java.awt.event.MouseAdapter() {194. public void mouseClicked(java.awt.event.MouseEvent evt) {195. jTextField5MouseClicked(evt);196. }197. });

198. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

199. jComboBox1.setToolTipText("Select category");

200. jScrollPane4.setToolTipText("Enter description");

201. jTextArea1.setColumns(20);202. jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 11)); //

NOI18N203. jTextArea1.setRows(5);204. jTextArea1.setText("Enter description of product here");205. jTextArea1.setToolTipText("Enter description");206. jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {207. public void mouseClicked(java.awt.event.MouseEvent evt) {208. jTextArea1MouseClicked(evt);209. }210. });211. jScrollPane4.setViewportView(jTextArea1);

Page 49: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

212. jButton6.setText("Add product");213. jButton6.setSelected(true);214. jButton6.addActionListener(new java.awt.event.ActionListener() {215. public void actionPerformed(java.awt.event.ActionEvent evt) {216. jButton6ActionPerformed(evt);217. }218. });

219. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

220. jPanel1.setLayout(jPanel1Layout);221. jPanel1Layout.setHorizontalGroup(222. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)223. .addGroup(jPanel1Layout.createSequentialGroup()224. .addContainerGap()225. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayou

t.Alignment.LEADING)226. .addGroup(jPanel1Layout.createSequentialGroup()227. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayou

t.Alignment.LEADING, false)228. .addComponent(jTextField2)229. .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE,

110, Short.MAX_VALUE))230. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE

D, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)231. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayou

t.Alignment.LEADING, false)232. .addComponent(jTextField4)233. .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE,

122, Short.MAX_VALUE)))234. .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)235. .addComponent(jScrollPane4))236. .addGap(172, 172, 172))237. .addGroup(jPanel1Layout.createSequentialGroup()238. .addGap(62, 62, 62)239. .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE,

105, javax.swing.GroupLayout.PREFERRED_SIZE)240. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE))241. );242. jPanel1Layout.setVerticalGroup(243. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)244. .addGroup(jPanel1Layout.createSequentialGroup()245. .addContainerGap()246. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayou

t.Alignment.BASELINE)247. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

248. .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

249. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

250. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

251. .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

Page 50: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

252. .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

253. .addGap(21, 21, 21)254. .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

255. .addGap(18, 18, 18)256. .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE,

125, Short.MAX_VALUE)257. .addGap(34, 34, 34)258. .addComponent(jButton6)259. .addContainerGap())260. );

261. jTabbedPane2.addTab("Product", jPanel1);

262. jTextField6.setText("Enter name");263. jTextField6.setToolTipText("Enter name");264. jTextField6.addMouseListener(new java.awt.event.MouseAdapter() {265. public void mouseClicked(java.awt.event.MouseEvent evt) {266. jTextField6MouseClicked(evt);267. }268. });

269. jButton7.setText("Add category");270. jButton7.addActionListener(new java.awt.event.ActionListener() {271. public void actionPerformed(java.awt.event.ActionEvent evt) {272. jButton7ActionPerformed(evt);273. }274. });

275. javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);

276. jPanel2.setLayout(jPanel2Layout);277. jPanel2Layout.setHorizontalGroup(278. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)279. .addGroup(jPanel2Layout.createSequentialGroup()280. .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayou

t.Alignment.LEADING)281. .addGroup(jPanel2Layout.createSequentialGroup()282. .addGap(18, 18, 18)283. .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE,

218, javax.swing.GroupLayout.PREFERRED_SIZE))284. .addGroup(jPanel2Layout.createSequentialGroup()285. .addGap(65, 65, 65)286. .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE,

105, javax.swing.GroupLayout.PREFERRED_SIZE)))287. .addContainerGap(35, Short.MAX_VALUE))288. );289. jPanel2Layout.setVerticalGroup(290. jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)291. .addGroup(jPanel2Layout.createSequentialGroup()292. .addGap(28, 28, 28)293. .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE,

30, javax.swing.GroupLayout.PREFERRED_SIZE)294. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE

D, 203, Short.MAX_VALUE)295. .addComponent(jButton7)296. .addGap(30, 30, 30))297. );

Page 51: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

298. jTabbedPane2.addTab("Category", jPanel2);

299. javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());

300. jFrame1.getContentPane().setLayout(jFrame1Layout);301. jFrame1Layout.setHorizontalGroup(302. jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)303. .addGroup(jFrame1Layout.createSequentialGroup()304. .addContainerGap()305. .addComponent(jTabbedPane2,

javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)

306. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

307. );308. jFrame1Layout.setVerticalGroup(309. jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignmen

t.LEADING)310. .addGroup(jFrame1Layout.createSequentialGroup()311. .addGap(32, 32, 32)312. .addComponent(jTabbedPane2)313. .addContainerGap())314. );

315. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

316. setTitle("Stock");317. setLocationByPlatform(true);318. setResizable(false);

319. jList1.setModel(new javax.swing.AbstractListModel() {320. String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item

5" };321. public int getSize() { return strings.length; }322. public Object getElementAt(int i) { return strings[i]; }323. });324. jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELE

CTION);325. jList1.setVisibleRowCount(10);326. jList1.addMouseListener(new java.awt.event.MouseAdapter() {327. public void mouseClicked(java.awt.event.MouseEvent evt) {328. jList1MouseClicked(evt);329. }330. });331. jScrollPane1.setViewportView(jList1);

332. jTable1.setModel(new javax.swing.table.DefaultTableModel(333. new Object [][] {334. {"ID", null},335. {"Name", null},336. {"Barcode", null},337. {"Price", null},338. {"Quantity", null},339. {"Category", null}340. },341. new String [] {342. "Property name", "Value"343. }344. ) {345. Class[] types = new Class [] {346. java.lang.String.class, java.lang.String.class347. };348. boolean[] canEdit = new boolean [] {

Page 52: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

349. false, true350. };

351. public Class getColumnClass(int columnIndex) {352. return types [columnIndex];353. }

354. public boolean isCellEditable(int rowIndex, int columnIndex) {355. return canEdit [columnIndex];356. }357. });358. jTable1.setToolTipText("");359. jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLU

MN);360. jTable1.setEnabled(false);361. jTable1.setRowHeight(30);362. jTable1.setRowMargin(0);363. jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SEL

ECTION);364. jTable1.getTableHeader().setReorderingAllowed(false);365. jScrollPane2.setViewportView(jTable1);

366. jTextField1.setEditable(false);

367. jTextPane1.setEditable(false);368. jTextPane1.setText("Description:");369. jScrollPane3.setViewportView(jTextPane1);

370. jButton1.setText("Refresh");371. jButton1.addActionListener(new java.awt.event.ActionListener() {372. public void actionPerformed(java.awt.event.ActionEvent evt) {373. jButton1ActionPerformed(evt);374. }375. });

376. jButton2.setText("Add");377. jButton2.addActionListener(new java.awt.event.ActionListener() {378. public void actionPerformed(java.awt.event.ActionEvent evt) {379. jButton2ActionPerformed(evt);380. }381. });

382. jButton3.setText("Find");383. jButton3.addActionListener(new java.awt.event.ActionListener() {384. public void actionPerformed(java.awt.event.ActionEvent evt) {385. jButton3ActionPerformed(evt);386. }387. });

388. jButton4.setText("Edit");389. jButton4.addActionListener(new java.awt.event.ActionListener() {390. public void actionPerformed(java.awt.event.ActionEvent evt) {391. jButton4ActionPerformed(evt);392. }393. });

394. jButton5.setText("Remove");395. jButton5.addActionListener(new java.awt.event.ActionListener() {396. public void actionPerformed(java.awt.event.ActionEvent evt) {397. jButton5ActionPerformed(evt);398. }399. });

400. jTextField7.setText("Find...");

Page 53: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

401. jTextField7.setToolTipText("Searching word");402. jTextField7.addMouseListener(new java.awt.event.MouseAdapter() {403. public void mouseClicked(java.awt.event.MouseEvent evt) {404. jTextField7MouseClicked(evt);405. }406. });

407. jMenu1.setText("About");408. jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {409. public void mouseClicked(java.awt.event.MouseEvent evt) {410. jMenu1MouseClicked(evt);411. }412. });413. jMenuBar1.add(jMenu1);

414. setJMenuBar(jMenuBar1);

415. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

416. getContentPane().setLayout(layout);417. layout.setHorizontalGroup(418. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI

NG)419. .addGroup(layout.createSequentialGroup()420. .addContainerGap()421. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Align

ment.LEADING)422. .addComponent(jScrollPane1)423. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 107,

Short.MAX_VALUE))424. .addGap(18, 18, 18)425. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Align

ment.LEADING, false)426. .addGroup(layout.createSequentialGroup()427. .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE,

85, javax.swing.GroupLayout.PREFERRED_SIZE)428. .addGap(21, 21, 21)429. .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE,

85, javax.swing.GroupLayout.PREFERRED_SIZE)430. .addGap(18, 18, 18)431. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,

85, javax.swing.GroupLayout.PREFERRED_SIZE)432. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE

D, 28, Short.MAX_VALUE)433. .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE,

111, javax.swing.GroupLayout.PREFERRED_SIZE)434. .addGap(18, 18, 18)435. .addComponent(jButton3))436. .addComponent(jScrollPane2)437. .addComponent(jTextField1)438. .addComponent(jScrollPane3))439. .addGap(28, 28, 28))440. );441. layout.setVerticalGroup(442. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI

NG)443. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,

layout.createSequentialGroup()444. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)445. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Align

ment.LEADING)446. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

Page 54: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

447. .addComponent(jButton2)448. .addComponent(jButton3)449. .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

450. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

451. .addComponent(jButton1)452. .addComponent(jButton5)453. .addComponent(jButton4)))454. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE

D)455. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Align

ment.TRAILING, false)456. .addComponent(jScrollPane1)457. .addGroup(layout.createSequentialGroup()458. .addComponent(jScrollPane2,

javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)

459. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

460. .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)

461. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

462. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))

463. .addContainerGap())464. );

465. pack();466. }// </editor-fold>//GEN-END:initComponents467. /**

List click action @param evt

468. */469. private void jList1MouseClicked(java.awt.event.MouseEvent evt)

{//GEN-FIRST:event_jList1MouseClicked470. // TODO add your handling code here:471. if(!editing){472. int index = jList1.locationToIndex(evt.getPoint());473. String name = (String) jList1.getModel().getElementAt(index);474. Product product = null;475. for(Iterator it = products.iterator(); it.hasNext(); ){476. product = (Product)it.next();477. if(product.getName().equals(name))478. break;479. }480. loadProductIntoTable(product);481. }482. }//GEN-LAST:event_jList1MouseClicked

483. /** Remove button action @param evt

484. */485. private void jButton5ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton5ActionPerformed486. // TODO add your handling code here:487. if(editing){488. editing = false;489. editEnded();490. try{

Page 55: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

491. loadData();492. } catch(SQLException sqle){493. showMessage("An error occured. " + sqle.getMessage());494. writeError(sqle);495. }496. return;497. }498. try{499. int index = ((Integer)jTable1.getModel().getValueAt(0,

1)).intValue();500. Product p = new Product();501. p.setId(index);502. if(ps.removeProduct(p)){503. showMessage("Product with ID " + p.getId() + " was successfully

removed.");504. }505. loadData();506. } catch(NullPointerException npe){507. showMessage("Select product to remove.");508. } catch(SQLException sqle){509. showMessage("An error occured. " + sqle.getMessage());510. writeError(sqle);511. }512. }//GEN-LAST:event_jButton5ActionPerformed513. /**

Add button @param evt

514. */515. private void jButton2ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton2ActionPerformed516. jFrame1.setVisible(true);517. }//GEN-LAST:event_jButton2ActionPerformed518. /**

Actions performing when edit started519. */520. private void editStarted(){521. jButton4.setText("Confirm");522. jButton2.setEnabled(false);523. jButton1.setEnabled(false);524. jButton5.setText("Cancel");525. jButton3.setEnabled(false);526. jList1.setEnabled(false);527. jTextField7.setEnabled(false);528. jTable1.setEnabled(true);529. jTextField1.setEditable(true);530. }531. /**

Actions performing when edit finished532. */533. private void editEnded(){534. jButton4.setText("Edit");535. jButton2.setEnabled(true);536. jButton1.setEnabled(true);537. jButton5.setText("Remove");538. jButton3.setEnabled(true);539. jList1.setEnabled(true);540. jTextField7.setEnabled(true);541. jTextField1.setEditable(false);542. jTable1.setEnabled(false);543. }

544. private void writeError(Exception e){545. System.err.println(new Date().toString() + ": " + e);546. }

Page 56: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

547. /** Edit button actions @param evt

548. */549. private void jButton4ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton4ActionPerformed550. if(jTable1.getModel().getValueAt(0, 1) == null){551. showMessage("Choose editing product first.");552. return;553. }554. editing = !editing;555. if(editing){556. editStarted();557. } else{558. editEnded();559. try{560. int id = ((Integer)jTable1.getModel().getValueAt(0, 1));561. String name = (String) jTable1.getModel().getValueAt(1, 1);562. int barcode = Integer.parseInt(jTable1.getModel().getValueAt(2,

1).toString());563. double price = Double.parseDouble(jTable1.getModel().getValueAt(3,

1).toString());564. int quantity = Integer.parseInt(jTable1.getModel().getValueAt(4,

1).toString());565. String categoryName = (String) jTable1.getModel().getValueAt(5,

1);566. Category category = findCategoryByName(categoryName);567. String description = jTextField1.getText();568. Product product = new Product(id, name, description, price,

quantity, barcode, category);569. if(ps.updateProduct(product)){570. showMessage("Product \"" + name + "\" successfully updated.");571. loadData();572. }573. } catch(SQLException sqle){574. showMessage("An error occured. " + sqle.getMessage());575. } catch(Exception e){576. showMessage("Wrong data entered. " + e.getMessage());577. // System.out.println(e);578. }

579. }

580. }//GEN-LAST:event_jButton4ActionPerformed581. /**

Method for searching category in categories list @param name @return

582. */583. private Category findCategoryByName(String name){584. Category category;585. for(Iterator it = categories.iterator(); it.hasNext(); ){586. category = (Category) it.next();587. if(category.getName().equals(name))588. return category;589. }590. return null;591. }592. /**

Refresh button actions @param evt

593. */594. private void jButton1ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton1ActionPerformed

Page 57: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

595. try{596. loadData();597. } catch(SQLException sqle){598. showMessage("An error occured." + sqle.getMessage());599. sqle.printStackTrace();600. }601. }//GEN-LAST:event_jButton1ActionPerformed602. /**

Find button actions @param evt

603. */604. private void jButton3ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton3ActionPerformed605. String keyWord = jTextField7.getText();606. ArrayList<Product> byId = null;607. ArrayList<Product> byName = null;608. try{609. byName = (ArrayList<Product>) ps.findByName(keyWord);

610. byId = (ArrayList<Product>) ps.findById(Integer.parseInt(keyWord));

611. //byId.addAll(byName);612. for(Iterator it = byName.iterator(); it.hasNext(); ){613. if(!byId.contains((Product)it.next())){614. byId.add((Product)it.next());615. }616. }617. loadProductsIntoList(byId);

618. } catch(NumberFormatException nfe){619. loadProductsIntoList(byName);620. } catch(SQLException sqle){621. showMessage("An error occured. " + sqle.getMessage());622. }623. clearTable();624. }//GEN-LAST:event_jButton3ActionPerformed

625. private void jTextField7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField7MouseClicked

626. jTextField7.setText(null);627. }//GEN-LAST:event_jTextField7MouseClicked

628. private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField2MouseClicked

629. jTextField2.setText(null);630. }//GEN-LAST:event_jTextField2MouseClicked

631. private void jTextField4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField4MouseClicked

632. jTextField4.setText(null);633. }//GEN-LAST:event_jTextField4MouseClicked

634. private void jTextField5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField5MouseClicked

635. jTextField5.setText(null);636. }//GEN-LAST:event_jTextField5MouseClicked

637. private void jTextField3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField3MouseClicked

638. jTextField3.setText(null);639. }//GEN-LAST:event_jTextField3MouseClicked

640. private void jTextArea1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MouseClicked

Page 58: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

641. jTextArea1.setText(null);642. }//GEN-LAST:event_jTextArea1MouseClicked643. /**

Product add method @param evt

644. */645. private void jButton6ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton6ActionPerformed646. try{647. String name = jTextField2.getText();648. int barcode = Integer.parseInt(jTextField3.getText());649. double price = Double.parseDouble(jTextField4.getText());650. int quantity = Integer.parseInt(jTextField5.getText());651. String description = jTextArea1.getText();652. String categoryName = (String)jComboBox1.getSelectedItem();653. Category category = findCategoryByName(categoryName);654. if(category == null)655. throw new Exception("Wrong data entered.");656. Product product = new Product(-1, name, description, 657. price, quantity, barcode, category);658. if(ps.addProduct(product)){659. showMessage("Product " + name + " successfully added.");660. } else{661. showMessage("Could not add product " + name + ".");662. }663. loadData();664. cleanAddProductForm();665. } catch(SQLException sqle){666. showMessage("An error occured. " + sqle.getMessage());667. } catch(NumberFormatException nfe){668. writeError(nfe);669. showMessage("Wrong data entered. " + nfe.getMessage() + " It must

be a number.");670. } catch(Exception e){671. showMessage("An error occured. " + e.getMessage());672. writeError(e);673. }674. }//GEN-LAST:event_jButton6ActionPerformed675. /**

Method for showing message 'msg' @param msg

676. */677. private void showMessage(String msg){678. JOptionPane.showMessageDialog(null, msg);679. }680. /**

Add category method @param evt

681. */682. private void jButton7ActionPerformed(java.awt.event.ActionEvent

evt) {//GEN-FIRST:event_jButton7ActionPerformed683. try{684. String name = jTextField6.getText();685. Category category = new Category(-1, name);686. if(cs.addCategory(category)){687. showMessage("Category " + name + " successfully added.");688. } else{689. showMessage("Could not add ategory " + name + ".");690. }691. loadData();692. cleanAddCategoryForm();693. } catch(SQLException sqle){694. showMessage("This category already exists.");695. } catch(Exception e){

Page 59: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

696. writeError(e);697. }698. }//GEN-LAST:event_jButton7ActionPerformed

699. private void jTextField6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField6MouseClicked

700. jTextField6.setText(null);701. }//GEN-LAST:event_jTextField6MouseClicked702. /**

About menu button actions @param evt

703. */704. private void jMenu1MouseClicked(java.awt.event.MouseEvent evt)

{//GEN-FIRST:event_jMenu1MouseClicked705. showMessage("Done by Khamit Mateyev, Kuanysh Konysbek and Maksat

Makhali.\nUiTM, Shah Alam, Malaysia. 2016.");706. }//GEN-LAST:event_jMenu1MouseClicked

707. /** @param args the command line arguments

708. */709. public static void main(String args[]){710. /* Set the Nimbus look and feel */711. //<editor-fold defaultstate="collapsed" desc=" Look and feel

setting code (optional) ">712. /* If Nimbus (introduced in Java SE 6) is not available, stay with

the default look and feel. For details see

http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

713. */714. try {715. for (javax.swing.UIManager.LookAndFeelInfo info :

javax.swing.UIManager.getInstalledLookAndFeels()) {716. if ("Nimbus".equals(info.getName())) {717. javax.swing.UIManager.setLookAndFeel(info.getClassName());718. break;719. }720. }721. } catch (ClassNotFoundException ex) {722. java.util.logging.Logger.getLogger(MainWindow.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);723. } catch (InstantiationException ex) {724. java.util.logging.Logger.getLogger(MainWindow.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);725. } catch (IllegalAccessException ex) {726. java.util.logging.Logger.getLogger(MainWindow.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);727. } catch (javax.swing.UnsupportedLookAndFeelException ex) {728. java.util.logging.Logger.getLogger(MainWindow.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);729. }730. //</editor-fold>

731. /* Create and display the form */732. java.awt.EventQueue.invokeLater(new Runnable() {733. public void run(){734. try{735. new MainWindow().setVisible(true);736. } catch(Exception e){737. e.printStackTrace();738. } 739. }740. });

Page 60: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

741. }742. private MainWindow mainWindow;743. // Variables declaration - do not modify//GEN-BEGIN:variables744. private javax.swing.JButton jButton1;745. private javax.swing.JButton jButton2;746. private javax.swing.JButton jButton3;747. private javax.swing.JButton jButton4;748. private javax.swing.JButton jButton5;749. private javax.swing.JButton jButton6;750. private javax.swing.JButton jButton7;751. private javax.swing.JComboBox jComboBox1;752. private javax.swing.JFrame jFrame1;753. private javax.swing.JList jList1;754. private javax.swing.JMenu jMenu1;755. private javax.swing.JMenuBar jMenuBar1;756. private javax.swing.JPanel jPanel1;757. private javax.swing.JPanel jPanel2;758. private javax.swing.JScrollPane jScrollPane1;759. private javax.swing.JScrollPane jScrollPane2;760. private javax.swing.JScrollPane jScrollPane3;761. private javax.swing.JScrollPane jScrollPane4;762. private javax.swing.JTabbedPane jTabbedPane2;763. private javax.swing.JTable jTable1;764. private javax.swing.JTextArea jTextArea1;765. private javax.swing.JTextField jTextField1;766. private javax.swing.JTextField jTextField2;767. private javax.swing.JTextField jTextField3;768. private javax.swing.JTextField jTextField4;769. private javax.swing.JTextField jTextField5;770. private javax.swing.JTextField jTextField6;771. private javax.swing.JTextField jTextField7;772. private javax.swing.JTextPane jTextPane1;773. // End of variables declaration//GEN-END:variables774. }

Page 61: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3.8 createDB.sql

1. Database: stock

2. DROP DATABASE stock;

2. CREATE DATABASE stock3. WITH OWNER = postgres

a. ENCODING = 'UTF8'b. TABLESPACE = pg_defaultc. LC_COLLATE = 'English_United States.1252'd. LC_CTYPE = 'English_United States.1252'e. CONNECTION LIMIT = -1;

4. create table categories(5. category_id serial primary key,6. name varchar(100) not null unique7. );

8. create table products(9. product_id serial primary key,10. name varchar(100),11. description varchar(100),12. price float,13. quantity int not null,14. barcode int not null unique,15. category_id int, foreign key(category_id) references

categories(category_id)16. );

Page 62: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

4. Testing of system including test data and output system

Figure 1.1 – Main window of the application.

Figure 1.1

One the left side is list of products. On the right top table with information about a chosen product, and under it description of the product.

On the top only one menu button. It is ‘About’ button, by clicking it user can see information about developers (figure 1.2).

Page 63: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 1.2

Under menu control button, which give main functionality of the system:

1. “Add” button. When user presses it, add form will appear (figure 2.1 and figure 2.1). There are two pages: one for add product, second for category add.

Page 64: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.1

Figure 2.2

Page 65: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

There are several conditions:

1. Price must be a double number (figure 2.3).2. Barcode must be an integer number (figure 2.4).3. Quantity must be an integer number (figure 2.5).4. Category name must be an unique name (figure 2.6)

If user enters right values, then success messages will be shown (figures 2.7 and 2.8).

Figure 2.3

Page 66: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.4

Page 67: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.5

Page 68: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.6

Page 69: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.7

Page 70: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 2.8

2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit” button. Table and description textfiled becomes editable, “Edit” button’s name changed to “Confirm” by pressing it user confirms changes, “Remove” button’s text changed to “Cancel” by clicking it user cancels changes. See figure 3.1.

If product to be changes is not chosen, then appropriate message will be shown (figure 3.2).

After user pressed “Confirm” button appropriate message will be shown (figure 3.3).

Page 71: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 3.1

Page 72: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 3.2

Page 73: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 3.3

Page 74: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

3. “Remove” button. Removes a chosen product.

If user presses this button, then chosen product will be removed and appropriate message will be shown (figure 4.1). If product to remove is not chosen, then appropriate message will be shown (figure 4.2).

Figure 4.1

Page 75: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

Figure 4.2

Page 76: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”

4. “Refresh” button. If user presses it, then all data will be reloaded from database again.

5. “Find” button. If user presses it, then all products, which names consists keyword in textfield, will be shown on list one the left side (figure 5.1). To load all products user need to press “Refresh” button.

Figure 5.1

6. All unpredicted exception will be written into log file named “log.log”.

Page 77: read.pudn.comread.pudn.com/downloads758/doc/3017031/JAVA_PROJ…  · Web view2. “Edit” button. Need to edit a product. After user clicks on the product and clicks “Edit”