├── out └── production │ └── INVENTORY │ ├── Buyer.class │ ├── Item.class │ ├── Main.class │ ├── AdminDAO.class │ ├── BuyerDAO.class │ ├── BuyerGUI.class │ ├── ItemDAO.class │ ├── ItemGUI.class │ ├── LoginGUI.class │ ├── Supplier.class │ ├── BuyerGUI$1.class │ ├── HeaderPanel.class │ ├── PurchaseGUI.class │ ├── RestockGUI.class │ ├── SupplierDAO.class │ ├── SupplierGUI.class │ ├── HeaderPanel$1.class │ ├── PurchaseItem.class │ ├── RestockGUI$1.class │ ├── AdminDashboard$1.class │ ├── AdminDashboard.class │ ├── BuyerTableModel.class │ ├── ItemTableModel.class │ ├── PurchaseItemDAO.class │ ├── SupplierTableModel.class │ ├── BuyerPurchaseHistory.class │ ├── BuyerPurchaseHistory$1.class │ ├── BuyerPurchaseHistory$2.class │ ├── PurchaseItemTableModel.class │ ├── BuyerGUI$AddButtonListener.class │ ├── ItemGUI$AddButtonListener.class │ ├── ItemGUI$ClearButtonListner.class │ ├── LoginGUI$ArrowKeyListener.class │ ├── PurchaseGUI$ItemTableModel.class │ ├── BuyerGUI$ClearButtonListner.class │ ├── ItemGUI$DeleteButtonListener.class │ ├── ItemGUI$UpdateButtonListener.class │ ├── LoginGUI$LoginButtonListener.class │ ├── BuyerGUI$DeleteButtonListener.class │ ├── BuyerGUI$UpdateButtonListener.class │ ├── PurchaseGUI$AddButtonListener.class │ ├── SupplierGUI$AddButtonListener.class │ ├── SupplierGUI$ClearButtonListener.class │ ├── RestockGUI$RestockButtonListener.class │ ├── SupplierGUI$DeleteButtonListener.class │ ├── SupplierGUI$UpdateButtonListener.class │ └── PurchaseGUI$CompleteButtonListener.class ├── src ├── Main.java ├── Buyer.java ├── Supplier.java ├── SupplierTableModel.java ├── PurchaseItemTableModel.java ├── BuyerTableModel.java ├── ItemTableModel.java ├── Item.java ├── PurchaseItem.java ├── BuyerDAO.java ├── AdminDAO.java ├── SupplierDAO.java ├── PurchaseItemDAO.java ├── ItemDAO.java ├── AdminDashboard.java ├── HeaderPanel.java ├── BuyerPurchaseHistory.java ├── LoginGUI.java ├── RestockGUI.java ├── SupplierGUI.java ├── BuyerGUI.java ├── ItemGUI.java └── PurchaseGUI.java ├── LICENSE ├── INVENTORY.iml └── EADCW.sql /out/production/INVENTORY/Buyer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/Buyer.class -------------------------------------------------------------------------------- /out/production/INVENTORY/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/Item.class -------------------------------------------------------------------------------- /out/production/INVENTORY/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/Main.class -------------------------------------------------------------------------------- /out/production/INVENTORY/AdminDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/AdminDAO.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerDAO.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemDAO.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/LoginGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/LoginGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/Supplier.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/Supplier.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI$1.class -------------------------------------------------------------------------------- /out/production/INVENTORY/HeaderPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/HeaderPanel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/RestockGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/RestockGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierDAO.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierGUI.class -------------------------------------------------------------------------------- /out/production/INVENTORY/HeaderPanel$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/HeaderPanel$1.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseItem.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseItem.class -------------------------------------------------------------------------------- /out/production/INVENTORY/RestockGUI$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/RestockGUI$1.class -------------------------------------------------------------------------------- /out/production/INVENTORY/AdminDashboard$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/AdminDashboard$1.class -------------------------------------------------------------------------------- /out/production/INVENTORY/AdminDashboard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/AdminDashboard.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerTableModel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemTableModel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseItemDAO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseItemDAO.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierTableModel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerPurchaseHistory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerPurchaseHistory.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerPurchaseHistory$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerPurchaseHistory$1.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerPurchaseHistory$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerPurchaseHistory$2.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseItemTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseItemTableModel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI$AddButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI$AddButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemGUI$AddButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemGUI$AddButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemGUI$ClearButtonListner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemGUI$ClearButtonListner.class -------------------------------------------------------------------------------- /out/production/INVENTORY/LoginGUI$ArrowKeyListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/LoginGUI$ArrowKeyListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseGUI$ItemTableModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseGUI$ItemTableModel.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI$ClearButtonListner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI$ClearButtonListner.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemGUI$DeleteButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemGUI$DeleteButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/ItemGUI$UpdateButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/ItemGUI$UpdateButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/LoginGUI$LoginButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/LoginGUI$LoginButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI$DeleteButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI$DeleteButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/BuyerGUI$UpdateButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/BuyerGUI$UpdateButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseGUI$AddButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseGUI$AddButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierGUI$AddButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierGUI$AddButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierGUI$ClearButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierGUI$ClearButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/RestockGUI$RestockButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/RestockGUI$RestockButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierGUI$DeleteButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierGUI$DeleteButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/SupplierGUI$UpdateButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/SupplierGUI$UpdateButtonListener.class -------------------------------------------------------------------------------- /out/production/INVENTORY/PurchaseGUI$CompleteButtonListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShashimalMadhuwantha/INVENTO/HEAD/out/production/INVENTORY/PurchaseGUI$CompleteButtonListener.class -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | 6 | LoginGUI loginGUI = new LoginGUI(); 7 | loginGUI.setVisible(true); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Shashimal Madhuwantha 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Buyer.java: -------------------------------------------------------------------------------- 1 | public class Buyer { 2 | private int buyerID; 3 | private String firstName; 4 | private String lastName; 5 | private String phoneNumber; 6 | 7 | public Buyer(int buyerID, String firstName, String lastName, String phoneNumber) { 8 | this.buyerID = buyerID; 9 | this.firstName = firstName; 10 | this.lastName = lastName; 11 | this.phoneNumber = phoneNumber; 12 | } 13 | 14 | public int getBuyerID() { 15 | return buyerID; 16 | } 17 | 18 | public void setBuyerID(int buyerID) { 19 | this.buyerID = buyerID; 20 | } 21 | 22 | public String getFirstName() { 23 | return firstName; 24 | } 25 | 26 | public void setFirstName(String firstName) { 27 | this.firstName = firstName; 28 | } 29 | 30 | public String getLastName() { 31 | return lastName; 32 | } 33 | 34 | public void setLastName(String lastName) { 35 | this.lastName = lastName; 36 | } 37 | 38 | public String getPhoneNumber() { 39 | return phoneNumber; 40 | } 41 | 42 | public void setPhoneNumber(String phoneNumber) { 43 | this.phoneNumber = phoneNumber; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Supplier.java: -------------------------------------------------------------------------------- 1 | public class Supplier { 2 | private int id; 3 | private String name; 4 | private String address; 5 | private String contactNumber; 6 | 7 | public Supplier(int id, String name, String address, String contactNumber) { 8 | this.id = id; 9 | this.name = name; 10 | this.address = address; 11 | this.contactNumber = contactNumber; 12 | } 13 | 14 | // Getters and setters 15 | public int getId() { 16 | return id; 17 | } 18 | 19 | public void setId(int id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getAddress() { 32 | return address; 33 | } 34 | 35 | public void setAddress(String address) { 36 | this.address = address; 37 | } 38 | 39 | public String getContactNumber() { 40 | return contactNumber; 41 | } 42 | 43 | public void setContactNumber(String contactNumber) { 44 | this.contactNumber = contactNumber; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return name; // Display supplier name in GUI components 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /INVENTORY.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/SupplierTableModel.java: -------------------------------------------------------------------------------- 1 | import javax.swing.table.AbstractTableModel; 2 | import java.util.List; 3 | 4 | public class SupplierTableModel extends AbstractTableModel { 5 | private List suppliers; 6 | private String[] columnNames = {"Supplier ID", "Supplier Name", "Address", "Contact Number"}; 7 | 8 | public void setSuppliers(List suppliers) { 9 | this.suppliers = suppliers; 10 | fireTableDataChanged(); 11 | } 12 | 13 | public Supplier getSupplierAt(int rowIndex) { 14 | return suppliers.get(rowIndex); 15 | } 16 | 17 | @Override 18 | public int getRowCount() { 19 | return suppliers == null ? 0 : suppliers.size(); 20 | } 21 | 22 | @Override 23 | public int getColumnCount() { 24 | return columnNames.length; 25 | } 26 | 27 | @Override 28 | public Object getValueAt(int rowIndex, int columnIndex) { 29 | Supplier supplier = suppliers.get(rowIndex); 30 | switch (columnIndex) { 31 | case 0: 32 | return supplier.getId(); 33 | case 1: 34 | return supplier.getName(); 35 | case 2: 36 | return supplier.getAddress(); 37 | case 3: 38 | return supplier.getContactNumber(); 39 | default: 40 | return null; 41 | } 42 | } 43 | 44 | @Override 45 | public String getColumnName(int column) { 46 | return columnNames[column]; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /EADCW.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE DATABASE inventory; 3 | 4 | 5 | USE inventory; 6 | 7 | 8 | CREATE TABLE admin ( 9 | id INT AUTO_INCREMENT PRIMARY KEY, 10 | username VARCHAR(50) NOT NULL, 11 | password VARCHAR(255) NOT NULL 12 | ); 13 | 14 | 15 | 16 | INSERT INTO admin (username, password) VALUES ('shashimal', '2003'); 17 | 18 | CREATE TABLE Supplier ( 19 | SupplierID INT AUTO_INCREMENT PRIMARY KEY, 20 | SupplierName VARCHAR(255) NOT NULL, 21 | Address VARCHAR(255), 22 | ContactNumber VARCHAR(50) 23 | ); 24 | INSERT INTO Supplier (SupplierName, Address, ContactNumber) 25 | VALUES ('Supplier Name', 'Supplier Address', 'Supplier Contact Number'); 26 | 27 | CREATE TABLE Item ( 28 | ItemID INT AUTO_INCREMENT PRIMARY KEY, 29 | Name VARCHAR(100) NOT NULL, 30 | Description TEXT, 31 | Price DECIMAL(10, 2) NOT NULL, 32 | Quantity INT NOT NULL, 33 | SupplierID INT, 34 | FOREIGN KEY (SupplierID) REFERENCES Supplier(SupplierID) 35 | ); 36 | 37 | CREATE TABLE Buyer ( 38 | BuyerID INT AUTO_INCREMENT PRIMARY KEY, 39 | FirstName VARCHAR(50) NOT NULL, 40 | LastName VARCHAR(50) NOT NULL, 41 | PhoneNumber VARCHAR(20) 42 | ); 43 | 44 | CREATE TABLE PurchaseItem ( 45 | PurchaseID INT AUTO_INCREMENT PRIMARY KEY, 46 | BuyerID INT, 47 | ItemID INT, 48 | PurchaseDate DATE, 49 | Quantity INT, 50 | TotalPrice DECIMAL(10, 2), 51 | Status VARCHAR(20) NOT NULL DEFAULT 'Pending', 52 | FOREIGN KEY (BuyerID) REFERENCES Buyer(BuyerID), 53 | FOREIGN KEY (ItemID) REFERENCES Item(ItemID) 54 | ); 55 | use inventory; 56 | INSERT INTO admin (username, password) VALUES ('admin', 'admin123'); 57 | select * from admin; -------------------------------------------------------------------------------- /src/PurchaseItemTableModel.java: -------------------------------------------------------------------------------- 1 | import javax.swing.table.AbstractTableModel; 2 | import java.math.BigDecimal; 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PurchaseItemTableModel extends AbstractTableModel { 7 | private final String[] columnNames = {"Purchase ID", "Buyer ID", "Item ID", "Date", "Quantity", "Total Price", "Status"}; 8 | private List items; 9 | 10 | public void setItems(List items) { 11 | this.items = items; 12 | fireTableDataChanged(); 13 | } 14 | 15 | @Override 16 | public int getRowCount() { 17 | return items == null ? 0 : items.size(); 18 | } 19 | 20 | @Override 21 | public int getColumnCount() { 22 | return columnNames.length; 23 | } 24 | 25 | @Override 26 | public Object getValueAt(int rowIndex, int columnIndex) { 27 | PurchaseItem item = items.get(rowIndex); 28 | switch (columnIndex) { 29 | case 0: 30 | return item.getPurchaseID(); 31 | case 1: 32 | return item.getBuyerID(); 33 | case 2: 34 | return item.getItemID(); 35 | case 3: 36 | return item.getPurchaseDate(); 37 | case 4: 38 | return item.getQuantity(); 39 | case 5: 40 | return item.getTotalPrice(); 41 | case 6: 42 | return item.getStatus(); 43 | default: 44 | return null; 45 | } 46 | } 47 | 48 | @Override 49 | public String getColumnName(int column) { 50 | return columnNames[column]; 51 | } 52 | } -------------------------------------------------------------------------------- /src/BuyerTableModel.java: -------------------------------------------------------------------------------- 1 | import javax.swing.table.AbstractTableModel; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class BuyerTableModel extends AbstractTableModel { 6 | private List buyers; 7 | private final String[] columnNames = {"Buyer ID", "First Name", "Last Name", "Phone Number"}; 8 | 9 | public BuyerTableModel() { 10 | this.buyers = new ArrayList<>(); 11 | } 12 | 13 | @Override 14 | public int getRowCount() { 15 | return buyers.size(); 16 | } 17 | 18 | @Override 19 | public int getColumnCount() { 20 | return columnNames.length; 21 | } 22 | 23 | @Override 24 | public Object getValueAt(int rowIndex, int columnIndex) { 25 | Buyer buyer = buyers.get(rowIndex); 26 | switch (columnIndex) { 27 | case 0: 28 | return buyer.getBuyerID(); 29 | case 1: 30 | return buyer.getFirstName(); 31 | case 2: 32 | return buyer.getLastName(); 33 | case 3: 34 | return buyer.getPhoneNumber(); 35 | default: 36 | return null; 37 | } 38 | } 39 | 40 | @Override 41 | public String getColumnName(int column) { 42 | return columnNames[column]; 43 | } 44 | 45 | public void setBuyers(List buyers) { 46 | this.buyers = buyers; 47 | fireTableDataChanged(); 48 | } 49 | 50 | public Buyer getBuyerAt(int rowIndex) { 51 | if (rowIndex >= 0 && rowIndex < buyers.size()) { 52 | return buyers.get(rowIndex); 53 | } else { 54 | return null; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/ItemTableModel.java: -------------------------------------------------------------------------------- 1 | import javax.swing.table.AbstractTableModel; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class ItemTableModel extends AbstractTableModel { 6 | private List items; 7 | private String[] columnNames = {"Item ID", "Name", "Description", "Price", "Quantity", "Supplier ID"}; 8 | 9 | public ItemTableModel() { 10 | this.items = new ArrayList<>(); 11 | } 12 | 13 | @Override 14 | public int getRowCount() { 15 | return items.size(); 16 | } 17 | 18 | @Override 19 | public int getColumnCount() { 20 | return columnNames.length; 21 | } 22 | 23 | @Override 24 | public Object getValueAt(int rowIndex, int columnIndex) { 25 | Item item = items.get(rowIndex); 26 | switch (columnIndex) { 27 | case 0: 28 | return item.getId(); 29 | case 1: 30 | return item.getName(); 31 | case 2: 32 | return item.getDescription(); 33 | case 3: 34 | return item.getPrice(); 35 | case 4: 36 | return item.getQuantity(); 37 | case 5: 38 | return item.getSupplierID(); 39 | default: 40 | throw new IllegalArgumentException("Invalid column index"); 41 | } 42 | } 43 | 44 | @Override 45 | public String getColumnName(int column) { 46 | return columnNames[column]; 47 | } 48 | 49 | public void setItems(List items) { 50 | this.items = items; 51 | fireTableDataChanged(); 52 | } 53 | 54 | public Item getItemAt(int rowIndex) { 55 | return items.get(rowIndex); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Item.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | 3 | public class Item { 4 | private int id; // Item ID 5 | private String name; 6 | private String description; 7 | private BigDecimal price; 8 | private int quantity; 9 | private int supplierID; 10 | 11 | // Constructor 12 | public Item(int id, String name, String description, BigDecimal price, int quantity, int supplierID) { 13 | this.id = id; 14 | this.name = name; 15 | this.description = description; 16 | this.price = price; 17 | this.quantity = quantity; 18 | this.supplierID = supplierID; 19 | } 20 | 21 | // Getters and Setters 22 | public int getId() { 23 | return id; 24 | } 25 | 26 | public void setId(int id) { 27 | this.id = id; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public String getDescription() { 39 | return description; 40 | } 41 | 42 | public void setDescription(String description) { 43 | this.description = description; 44 | } 45 | 46 | public BigDecimal getPrice() { 47 | return price; 48 | } 49 | 50 | public void setPrice(BigDecimal price) { 51 | this.price = price; 52 | } 53 | 54 | public int getQuantity() { 55 | return quantity; 56 | } 57 | 58 | public void setQuantity(int quantity) { 59 | this.quantity = quantity; 60 | } 61 | 62 | public int getSupplierID() { 63 | return supplierID; 64 | } 65 | 66 | public void setSupplierID(int supplierID) { 67 | this.supplierID = supplierID; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/PurchaseItem.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.util.Date; 3 | 4 | public class PurchaseItem { 5 | private int purchaseID; 6 | private int buyerID; 7 | private int itemID; 8 | private Date purchaseDate; 9 | private int quantity; 10 | private BigDecimal totalPrice; 11 | private String status; 12 | 13 | public PurchaseItem(int purchaseID, int buyerID, int itemID, Date purchaseDate, int quantity, BigDecimal totalPrice, String status) { 14 | this.purchaseID = purchaseID; 15 | this.buyerID = buyerID; 16 | this.itemID = itemID; 17 | this.purchaseDate = purchaseDate; 18 | this.quantity = quantity; 19 | this.totalPrice = totalPrice; 20 | this.status = status; 21 | } 22 | 23 | // Getters and Setters 24 | public int getPurchaseID() { return purchaseID; } 25 | public void setPurchaseID(int purchaseID) { this.purchaseID = purchaseID; } 26 | 27 | public int getBuyerID() { return buyerID; } 28 | public void setBuyerID(int buyerID) { this.buyerID = buyerID; } 29 | 30 | public int getItemID() { return itemID; } 31 | public void setItemID(int itemID) { this.itemID = itemID; } 32 | 33 | public Date getPurchaseDate() { return purchaseDate; } 34 | public void setPurchaseDate(Date purchaseDate) { this.purchaseDate = purchaseDate; } 35 | 36 | public int getQuantity() { return quantity; } 37 | public void setQuantity(int quantity) { this.quantity = quantity; } 38 | 39 | public BigDecimal getTotalPrice() { return totalPrice; } 40 | public void setTotalPrice(BigDecimal totalPrice) { this.totalPrice = totalPrice; } 41 | 42 | public String getStatus() { return status; } 43 | public void setStatus(String status) { this.status = status; } 44 | } 45 | -------------------------------------------------------------------------------- /src/BuyerDAO.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class BuyerDAO { 6 | private Connection connection; 7 | 8 | public BuyerDAO() { 9 | 10 | try { 11 | String url = "jdbc:mysql://localhost:3308/inventory"; 12 | String user = "root"; 13 | String password = "2003"; 14 | connection = DriverManager.getConnection(url, user, password); 15 | } catch (SQLException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | 20 | public boolean addBuyer(Buyer buyer) { 21 | String sql = "INSERT INTO Buyer (FirstName, LastName, PhoneNumber) VALUES (?, ?, ?)"; 22 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 23 | stmt.setString(1, buyer.getFirstName()); 24 | stmt.setString(2, buyer.getLastName()); 25 | stmt.setString(3, buyer.getPhoneNumber()); 26 | return stmt.executeUpdate() > 0; 27 | } catch (SQLException e) { 28 | e.printStackTrace(); 29 | return false; 30 | } 31 | } 32 | 33 | public boolean updateBuyer(Buyer buyer) { 34 | String sql = "UPDATE Buyer SET FirstName = ?, LastName = ?, PhoneNumber = ? WHERE BuyerID = ?"; 35 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 36 | stmt.setString(1, buyer.getFirstName()); 37 | stmt.setString(2, buyer.getLastName()); 38 | stmt.setString(3, buyer.getPhoneNumber()); 39 | stmt.setInt(4, buyer.getBuyerID()); 40 | return stmt.executeUpdate() > 0; 41 | } catch (SQLException e) { 42 | e.printStackTrace(); 43 | return false; 44 | } 45 | } 46 | 47 | public boolean deleteBuyer(int buyerID) { 48 | String sql = "DELETE FROM Buyer WHERE BuyerID = ?"; 49 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 50 | stmt.setInt(1, buyerID); 51 | return stmt.executeUpdate() > 0; 52 | } catch (SQLException e) { 53 | e.printStackTrace(); 54 | return false; 55 | } 56 | } 57 | 58 | public List getAllBuyers() { 59 | List buyers = new ArrayList<>(); 60 | String sql = "SELECT * FROM Buyer"; 61 | try (Statement stmt = connection.createStatement(); 62 | ResultSet rs = stmt.executeQuery(sql)) { 63 | while (rs.next()) { 64 | buyers.add(new Buyer( 65 | rs.getInt("BuyerID"), 66 | rs.getString("FirstName"), 67 | rs.getString("LastName"), 68 | rs.getString("PhoneNumber") 69 | )); 70 | } 71 | } catch (SQLException e) { 72 | e.printStackTrace(); 73 | } 74 | return buyers; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/AdminDAO.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | 7 | public class AdminDAO { 8 | private Connection connection; 9 | 10 | public AdminDAO() throws SQLException { 11 | String url = "jdbc:mysql://localhost:3308/INVENTORY"; 12 | String username = "root"; 13 | String password = "2003"; 14 | connection = DriverManager.getConnection(url, username, password); 15 | } 16 | 17 | public boolean validateLogin(String username, String password) throws SQLException { 18 | String query = "SELECT * FROM admin WHERE username = ? AND password = ?"; 19 | try (PreparedStatement statement = connection.prepareStatement(query)) { 20 | statement.setString(1, username); 21 | statement.setString(2, password); 22 | try (ResultSet resultSet = statement.executeQuery()) { 23 | return resultSet.next(); 24 | } 25 | } 26 | } 27 | 28 | public int getUserId(String username) throws SQLException { 29 | String query = "SELECT id FROM admin WHERE username = ?"; 30 | try (PreparedStatement statement = connection.prepareStatement(query)) { 31 | statement.setString(1, username); 32 | try (ResultSet resultSet = statement.executeQuery()) { 33 | if (resultSet.next()) { 34 | return resultSet.getInt("id"); // Fetch user ID 35 | } 36 | } 37 | } 38 | return -1; 39 | } 40 | 41 | public String fetchUsername(int userId) throws SQLException { 42 | String username = "Admin"; 43 | String query = "SELECT username FROM admin WHERE id = ?"; 44 | try (PreparedStatement statement = connection.prepareStatement(query)) { 45 | statement.setInt(1, userId); 46 | try (ResultSet resultSet = statement.executeQuery()) { 47 | if (resultSet.next()) { 48 | username = resultSet.getString("username"); 49 | } 50 | } 51 | } 52 | return username; 53 | } 54 | 55 | public boolean updateAdmin(int userId, String newUsername, String newPassword) throws SQLException { 56 | String query = "UPDATE admin SET username = ?, password = ? WHERE id = ?"; 57 | try (PreparedStatement statement = connection.prepareStatement(query)) { 58 | statement.setString(1, newUsername); 59 | statement.setString(2, newPassword); 60 | statement.setInt(3, userId); 61 | int rowsUpdated = statement.executeUpdate(); 62 | return rowsUpdated > 0; 63 | } 64 | } 65 | 66 | public void close() throws SQLException { 67 | if (connection != null && !connection.isClosed()) { 68 | connection.close(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/SupplierDAO.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public class SupplierDAO { 10 | private static final String URL = "jdbc:mysql://localhost:3308/inventory"; 11 | private static final String USER = "root"; 12 | private static final String PASSWORD = "2003"; 13 | 14 | private Connection connection; 15 | 16 | public SupplierDAO() { 17 | try { 18 | connection = DriverManager.getConnection(URL, USER, PASSWORD); 19 | } catch (SQLException e) { 20 | e.printStackTrace(); 21 | throw new RuntimeException("Failed to connect to the database."); 22 | } 23 | } 24 | 25 | // Method to add a new supplier 26 | public boolean addSupplier(String name, String address, String contactNumber) { 27 | String query = "INSERT INTO Supplier (SupplierName, Address, ContactNumber) VALUES (?, ?, ?)"; 28 | try (PreparedStatement stmt = connection.prepareStatement(query)) { 29 | stmt.setString(1, name); 30 | stmt.setString(2, address); 31 | stmt.setString(3, contactNumber); 32 | int rowsInserted = stmt.executeUpdate(); 33 | return rowsInserted > 0; 34 | } catch (SQLException e) { 35 | e.printStackTrace(); 36 | return false; 37 | } 38 | } 39 | 40 | // Method to update an existing supplier 41 | public boolean updateSupplier(int supplierID, String name, String address, String contactNumber) { 42 | String query = "UPDATE Supplier SET SupplierName = ?, Address = ?, ContactNumber = ? WHERE SupplierID = ?"; 43 | try (PreparedStatement stmt = connection.prepareStatement(query)) { 44 | stmt.setString(1, name); 45 | stmt.setString(2, address); 46 | stmt.setString(3, contactNumber); 47 | stmt.setInt(4, supplierID); 48 | int rowsUpdated = stmt.executeUpdate(); 49 | return rowsUpdated > 0; 50 | } catch (SQLException e) { 51 | e.printStackTrace(); 52 | return false; 53 | } 54 | } 55 | 56 | // Method to delete a supplier 57 | public boolean deleteSupplier(int supplierID) { 58 | String query = "DELETE FROM Supplier WHERE SupplierID = ?"; 59 | try (PreparedStatement stmt = connection.prepareStatement(query)) { 60 | stmt.setInt(1, supplierID); 61 | int rowsDeleted = stmt.executeUpdate(); 62 | return rowsDeleted > 0; 63 | } catch (SQLException e) { 64 | e.printStackTrace(); 65 | return false; 66 | } 67 | } 68 | 69 | // Method to get a list of all suppliers 70 | public List getAllSuppliers() { 71 | List suppliers = new ArrayList<>(); 72 | String query = "SELECT * FROM Supplier"; 73 | try (PreparedStatement stmt = connection.prepareStatement(query); 74 | ResultSet rs = stmt.executeQuery()) { 75 | while (rs.next()) { 76 | int id = rs.getInt("SupplierID"); 77 | String name = rs.getString("SupplierName"); 78 | String address = rs.getString("Address"); 79 | String contactNumber = rs.getString("ContactNumber"); 80 | suppliers.add(new Supplier(id, name, address, contactNumber)); 81 | } 82 | } catch (SQLException e) { 83 | e.printStackTrace(); 84 | } 85 | return suppliers; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/PurchaseItemDAO.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.sql.*; 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PurchaseItemDAO { 7 | private Connection connection; 8 | 9 | public PurchaseItemDAO() { 10 | try { 11 | String url = "jdbc:mysql://localhost:3308/inventory"; 12 | String username = "root"; 13 | String password = "2003"; 14 | connection = DriverManager.getConnection(url, username, password); 15 | } catch (SQLException e) { 16 | e.printStackTrace(); 17 | throw new RuntimeException("Failed to connect to the database", e); 18 | } 19 | } 20 | 21 | public List getPendingPurchaseItems() { 22 | List items = new ArrayList<>(); 23 | String query = "SELECT * FROM PurchaseItem WHERE Status = 'Pending'"; 24 | 25 | try (PreparedStatement stmt = connection.prepareStatement(query); 26 | ResultSet rs = stmt.executeQuery()) { 27 | 28 | while (rs.next()) { 29 | PurchaseItem item = new PurchaseItem( 30 | rs.getInt("PurchaseID"), 31 | rs.getInt("BuyerID"), 32 | rs.getInt("ItemID"), 33 | rs.getDate("PurchaseDate"), 34 | rs.getInt("Quantity"), 35 | rs.getBigDecimal("TotalPrice"), 36 | rs.getString("Status") 37 | ); 38 | items.add(item); 39 | } 40 | } catch (SQLException e) { 41 | e.printStackTrace(); // Handle exception 42 | } 43 | return items; 44 | } 45 | 46 | public boolean addPurchaseItem(PurchaseItem purchaseItem) { 47 | String query = "INSERT INTO PurchaseItem (BuyerID, ItemID, PurchaseDate, Quantity, TotalPrice, Status) VALUES (?, ?, ?, ?, ?, 'Pending')"; 48 | 49 | try (PreparedStatement stmt = connection.prepareStatement(query)) { 50 | stmt.setInt(1, purchaseItem.getBuyerID()); 51 | stmt.setInt(2, purchaseItem.getItemID()); 52 | stmt.setDate(3, new java.sql.Date(purchaseItem.getPurchaseDate().getTime())); 53 | stmt.setInt(4, purchaseItem.getQuantity()); 54 | stmt.setBigDecimal(5, purchaseItem.getTotalPrice()); 55 | return stmt.executeUpdate() > 0; 56 | } catch (SQLException e) { 57 | e.printStackTrace(); // Handle exception 58 | } 59 | return false; 60 | } 61 | 62 | 63 | 64 | 65 | public boolean updatePurchaseStatus(List purchaseIDs, String status) { 66 | String query = "UPDATE PurchaseItem SET Status = ? WHERE PurchaseID = ?"; 67 | try (PreparedStatement statement = connection.prepareStatement(query)) { 68 | connection.setAutoCommit(false); // Start transaction 69 | for (int purchaseID : purchaseIDs) { 70 | statement.setString(1, status); 71 | statement.setInt(2, purchaseID); 72 | statement.addBatch(); 73 | } 74 | int[] result = statement.executeBatch(); 75 | connection.commit(); 76 | return result.length == purchaseIDs.size(); 77 | } catch (SQLException e) { 78 | e.printStackTrace(); 79 | try { 80 | connection.rollback(); 81 | } catch (SQLException rollbackEx) { 82 | rollbackEx.printStackTrace(); 83 | } 84 | } 85 | return false; 86 | } 87 | 88 | public BigDecimal getPendingItemsTotal() { 89 | BigDecimal total = BigDecimal.ZERO; 90 | String query = "SELECT SUM(TotalPrice) AS Total FROM PurchaseItem WHERE Status = 'Pending'"; 91 | 92 | try (PreparedStatement stmt = connection.prepareStatement(query); 93 | ResultSet rs = stmt.executeQuery()) { 94 | 95 | if (rs.next()) { 96 | total = rs.getBigDecimal("Total"); 97 | if (total == null) { 98 | total = BigDecimal.ZERO; 99 | } 100 | } 101 | } catch (SQLException e) { 102 | e.printStackTrace(); 103 | } 104 | return total; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/ItemDAO.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class ItemDAO { 6 | private Connection connection; 7 | 8 | public ItemDAO() { 9 | try { 10 | 11 | String url = "jdbc:mysql://localhost:3308/inventory"; 12 | String username = "root"; 13 | String password = "2003"; 14 | connection = DriverManager.getConnection(url, username, password); 15 | } catch (SQLException e) { 16 | e.printStackTrace(); 17 | throw new RuntimeException("Failed to connect to the database"); 18 | } 19 | } 20 | 21 | public boolean updateItemQuantity(int itemId, int newQuantity) throws SQLException { 22 | 23 | String sql = "UPDATE Item SET Quantity = ? WHERE ItemID = ?"; 24 | 25 | 26 | try (PreparedStatement pstmt = connection.prepareStatement(sql)) { 27 | 28 | pstmt.setInt(1, newQuantity); 29 | pstmt.setInt(2, itemId); 30 | int rowsAffected = pstmt.executeUpdate(); 31 | return rowsAffected > 0; 32 | } catch (SQLException e) { 33 | // Handle any SQL exceptions 34 | e.printStackTrace(); 35 | throw new SQLException("Error updating item quantity.", e); 36 | } 37 | } 38 | 39 | public boolean addItem(Item item) { 40 | String sql = "INSERT INTO item (Name, Description, Price, Quantity, SupplierID) VALUES (?, ?, ?, ?, ?)"; 41 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 42 | stmt.setString(1, item.getName()); 43 | stmt.setString(2, item.getDescription()); 44 | stmt.setBigDecimal(3, item.getPrice()); 45 | stmt.setInt(4, item.getQuantity()); 46 | stmt.setInt(5, item.getSupplierID()); 47 | int rowsInserted = stmt.executeUpdate(); 48 | return rowsInserted > 0; 49 | } catch (SQLException e) { 50 | e.printStackTrace(); 51 | return false; 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | public boolean updateItem(Item item) { 59 | String sql = "UPDATE item SET Name = ?, Description = ?, Price = ?, Quantity = ?, SupplierID = ? WHERE ItemID = ?"; 60 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 61 | stmt.setString(1, item.getName()); 62 | stmt.setString(2, item.getDescription()); 63 | stmt.setBigDecimal(3, item.getPrice()); 64 | stmt.setInt(4, item.getQuantity()); 65 | stmt.setInt(5, item.getSupplierID()); 66 | stmt.setInt(6, item.getId()); // Use getItemID() here 67 | int rowsUpdated = stmt.executeUpdate(); 68 | return rowsUpdated > 0; 69 | } catch (SQLException e) { 70 | e.printStackTrace(); 71 | return false; 72 | } 73 | } 74 | 75 | public boolean deleteItem(int itemId) { 76 | String sql = "DELETE FROM item WHERE ItemID = ?"; 77 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 78 | stmt.setInt(1, itemId); 79 | int rowsDeleted = stmt.executeUpdate(); 80 | return rowsDeleted > 0; 81 | } catch (SQLException e) { 82 | e.printStackTrace(); 83 | return false; 84 | } 85 | } 86 | 87 | public List getAllItems() { 88 | List items = new ArrayList<>(); 89 | String sql = "SELECT * FROM item"; 90 | try (Statement stmt = connection.createStatement(); 91 | ResultSet rs = stmt.executeQuery(sql)) { 92 | while (rs.next()) { 93 | Item item = new Item( 94 | rs.getInt("ItemID"), 95 | rs.getString("Name"), 96 | rs.getString("Description"), 97 | rs.getBigDecimal("Price"), 98 | rs.getInt("Quantity"), 99 | rs.getInt("SupplierID") 100 | ); 101 | items.add(item); 102 | } 103 | } catch (SQLException e) { 104 | e.printStackTrace(); 105 | } 106 | return items; 107 | } 108 | 109 | 110 | 111 | public Item getItemById(int itemId) { 112 | String sql = "SELECT * FROM item WHERE ItemID = ?"; 113 | try (PreparedStatement stmt = connection.prepareStatement(sql)) { 114 | stmt.setInt(1, itemId); 115 | try (ResultSet rs = stmt.executeQuery()) { 116 | if (rs.next()) { 117 | return new Item( 118 | rs.getInt("ItemID"), 119 | rs.getString("Name"), 120 | rs.getString("Description"), 121 | rs.getBigDecimal("Price"), 122 | rs.getInt("Quantity"), 123 | rs.getInt("SupplierID") 124 | ); 125 | } 126 | } 127 | } catch (SQLException e) { 128 | e.printStackTrace(); 129 | } 130 | return null; 131 | } 132 | 133 | 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/AdminDashboard.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | import java.sql.SQLException; 6 | 7 | public class AdminDashboard extends JFrame { 8 | private int userId; 9 | private AdminDAO adminDAO; 10 | 11 | 12 | public AdminDashboard(int userId) { 13 | this.userId = userId; 14 | setTitle("Admin Dashboard"); 15 | setSize(1200, 800); // Set the size of the dashboard window 16 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 | setLocationRelativeTo(null); 18 | setLayout(new BorderLayout()); 19 | 20 | try { 21 | adminDAO = new AdminDAO(); 22 | } catch (SQLException e) { 23 | e.printStackTrace(); 24 | JOptionPane.showMessageDialog(null, "Failed to connect to the database."); 25 | System.exit(1); 26 | } 27 | 28 | 29 | 30 | HeaderPanel headerPanel = new HeaderPanel(userId); 31 | Container container = getContentPane(); 32 | 33 | Container container1 = new JPanel(); 34 | container1.setLayout(new BorderLayout()); 35 | 36 | 37 | JPanel uppanel = createUpdateFormPanel(); 38 | JPanel welcome = createWelcomePanel(); 39 | container1.add(welcome,BorderLayout.NORTH); 40 | container1.add(uppanel,BorderLayout.CENTER); 41 | 42 | 43 | container.add(headerPanel,BorderLayout.NORTH); 44 | container.add(container1,BorderLayout.CENTER); 45 | setVisible(true); 46 | } 47 | 48 | private JPanel createWelcomePanel() { 49 | JPanel panel = new JPanel(); 50 | panel.setLayout(new BorderLayout()); 51 | panel.setBackground(Color.WHITE); 52 | 53 | 54 | String username = ""; 55 | try { 56 | username = adminDAO.fetchUsername(userId); 57 | } catch (SQLException e) { 58 | e.printStackTrace(); 59 | JOptionPane.showMessageDialog(this, "Failed to fetch username."); 60 | } 61 | 62 | 63 | JLabel welcomeLabel = new JLabel("Welcome to the Dashboard, " + username); 64 | welcomeLabel.setFont(new Font("Helvetica Neue", Font.PLAIN, 24)); 65 | welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER); 66 | welcomeLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0)); 67 | welcomeLabel.setForeground(new Color(0, 102, 204)); // Dark blue color 68 | 69 | panel.add(welcomeLabel, BorderLayout.CENTER); 70 | return panel; 71 | } 72 | 73 | private JPanel createUpdateFormPanel() { 74 | JPanel panel = new JPanel(); 75 | panel.setLayout(new GridBagLayout()); 76 | panel.setBackground(Color.WHITE); 77 | GridBagConstraints gbc = new GridBagConstraints(); 78 | gbc.insets = new Insets(10, 10, 10, 10); 79 | gbc.fill = GridBagConstraints.HORIZONTAL; 80 | 81 | 82 | JLabel usernameLabel = new JLabel("Username:"); 83 | gbc.gridx = 0; 84 | gbc.gridy = 0; 85 | gbc.anchor = GridBagConstraints.EAST; 86 | panel.add(usernameLabel, gbc); 87 | 88 | JTextField usernameField = new JTextField(20); 89 | gbc.gridx = 1; 90 | gbc.anchor = GridBagConstraints.WEST; 91 | panel.add(usernameField, gbc); 92 | 93 | 94 | JLabel passwordLabel = new JLabel("Password:"); 95 | gbc.gridx = 0; 96 | gbc.gridy = 1; 97 | gbc.anchor = GridBagConstraints.EAST; 98 | panel.add(passwordLabel, gbc); 99 | 100 | JPasswordField passwordField = new JPasswordField(20); 101 | gbc.gridx = 1; 102 | gbc.anchor = GridBagConstraints.WEST; 103 | panel.add(passwordField, gbc); 104 | 105 | JLabel conpasswordLabel = new JLabel("Confirm Password:"); 106 | gbc.gridx = 0; 107 | gbc.gridy = 2; 108 | gbc.anchor = GridBagConstraints.EAST; 109 | panel.add(conpasswordLabel, gbc); 110 | 111 | JPasswordField conpasswordField = new JPasswordField(20); 112 | gbc.gridx = 1; 113 | gbc.anchor = GridBagConstraints.WEST; 114 | panel.add(conpasswordField, gbc); 115 | 116 | JButton updateButton = new JButton("Change Password"); 117 | updateButton.setBackground(new Color(0, 102, 204)); // Dark blue button 118 | updateButton.setForeground(Color.WHITE); // White text 119 | gbc.gridx = 0; 120 | gbc.gridy = 3; 121 | gbc.gridwidth = 2; 122 | gbc.anchor = GridBagConstraints.CENTER; 123 | panel.add(updateButton, gbc); 124 | 125 | 126 | updateButton.addActionListener(new ActionListener() { 127 | @Override 128 | public void actionPerformed(ActionEvent e) { 129 | int response = JOptionPane.showConfirmDialog(AdminDashboard.this, 130 | "Are you sure you want to change the username and password?", 131 | "Confirm Update", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 132 | 133 | if (response == JOptionPane.YES_OPTION) { 134 | String newUsername = usernameField.getText().trim(); 135 | String newPassword = new String(passwordField.getPassword()).trim(); 136 | String confirmPassword = new String(conpasswordField.getPassword()).trim(); 137 | 138 | if (newUsername.isEmpty() || newPassword.isEmpty()) { 139 | JOptionPane.showMessageDialog(AdminDashboard.this, "Username and password cannot be empty."); 140 | return; 141 | } 142 | 143 | if (!newPassword.equals(confirmPassword)) { 144 | JOptionPane.showMessageDialog(AdminDashboard.this, "Passwords do not match."); 145 | return; 146 | } 147 | 148 | try { 149 | boolean success = adminDAO.updateAdmin(userId, newUsername, newPassword); 150 | if (success) { 151 | JOptionPane.showMessageDialog(AdminDashboard.this, "Profile updated successfully."); 152 | } else { 153 | JOptionPane.showMessageDialog(AdminDashboard.this, "Failed to update profile."); 154 | } 155 | } catch (SQLException ex) { 156 | ex.printStackTrace(); 157 | JOptionPane.showMessageDialog(AdminDashboard.this, "Database error: " + ex.getMessage()); 158 | } 159 | } 160 | } 161 | }); 162 | 163 | return panel; 164 | } 165 | 166 | 167 | } 168 | -------------------------------------------------------------------------------- /src/HeaderPanel.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | import java.awt.event.MouseAdapter; 6 | import java.awt.event.MouseEvent; 7 | 8 | class HeaderPanel extends JPanel implements ActionListener { 9 | 10 | private int userID; 11 | 12 | public HeaderPanel( int userID) { 13 | 14 | this.userID = userID; 15 | setLayout(new BorderLayout()); 16 | setBackground(new Color(15, 150, 195)); 17 | 18 | // Left side - Inventory Management System label 19 | JLabel titleLabel = new JLabel("INVENTORY MANAGEMENT SYSTEM"); 20 | titleLabel.setFont(new Font("Arial", Font.BOLD, 24)); 21 | titleLabel.setForeground(new Color(255, 255, 255, 255)); 22 | add(titleLabel, BorderLayout.WEST); 23 | 24 | // Right side - Navigation items 25 | JPanel navPanel = new JPanel(); 26 | navPanel.setBackground(new Color(15, 150, 195)); 27 | navPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 28 | 29 | String[] navItems = {"Dashboard", "Item", "Supplier", "Purchasing", "Buyers", "PurchaseHistory","Restocking"}; 30 | for (String item : navItems) { 31 | JButton navButton = new JButton(item); 32 | navButton.setFont(new Font("Arial", Font.PLAIN, 18)); 33 | navButton.setForeground(Color.WHITE); 34 | navButton.setBackground(new Color(0, 102, 204)); 35 | navButton.setFocusPainted(false); // Removes the focus border 36 | navButton.setBorder(BorderFactory.createCompoundBorder( 37 | BorderFactory.createLineBorder(Color.WHITE, 1), 38 | BorderFactory.createEmptyBorder(5, 15, 5, 15) 39 | )); 40 | navButton.setActionCommand(item); // Set action command 41 | 42 | 43 | navButton.addMouseListener(new MouseAdapter() { 44 | @Override 45 | public void mouseEntered(MouseEvent e) { 46 | navButton.setBackground(new Color(77, 116, 209)); 47 | navButton.setForeground(new Color(255, 255, 255)); 48 | } 49 | 50 | @Override 51 | public void mouseExited(MouseEvent e) { 52 | navButton.setBackground(new Color(0, 102, 204)); 53 | navButton.setForeground(Color.WHITE); 54 | } 55 | }); 56 | 57 | navButton.addActionListener(this); // Add action listener 58 | navPanel.add(navButton); 59 | } 60 | 61 | add(navPanel, BorderLayout.EAST); 62 | } 63 | 64 | @Override 65 | public void actionPerformed(ActionEvent e) { 66 | String command = e.getActionCommand(); 67 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 68 | try { 69 | switch (command) { 70 | case "Supplier": 71 | 72 | SwingUtilities.getWindowAncestor(this).setVisible(false); 73 | SupplierGUI supplierGUI = new SupplierGUI(userID); 74 | 75 | supplierGUI.setSize(screenSize); 76 | 77 | // Center the window on the screen 78 | supplierGUI.setLocationRelativeTo(null); 79 | supplierGUI.setVisible(true); 80 | break; 81 | 82 | case "Item": 83 | SwingUtilities.getWindowAncestor(this).setVisible(false); 84 | ItemGUI itemGUI = new ItemGUI(userID); 85 | 86 | itemGUI.setSize(screenSize); 87 | 88 | // Center the window on the screen 89 | itemGUI.setLocationRelativeTo(null); 90 | itemGUI.setVisible(true); 91 | break; 92 | case "Dashboard": 93 | // Create and show the AdminDashboard with userID 94 | SwingUtilities.getWindowAncestor(this).setVisible(false); 95 | AdminDashboard adminDashboard = new AdminDashboard(userID); 96 | 97 | adminDashboard.setSize(screenSize); 98 | 99 | // Center the window on the screen 100 | adminDashboard.setLocationRelativeTo(null); 101 | adminDashboard.setVisible(true); 102 | break; 103 | 104 | case "Purchasing": 105 | SwingUtilities.getWindowAncestor(this).setVisible(false); 106 | PurchaseGUI purchaseGUI = new PurchaseGUI(userID); 107 | 108 | purchaseGUI.setSize(screenSize); 109 | 110 | // Center the window on the screen 111 | purchaseGUI.setLocationRelativeTo(null); 112 | purchaseGUI.setVisible(true); 113 | break; 114 | 115 | case "Buyers": 116 | SwingUtilities.getWindowAncestor(this).setVisible(false); 117 | BuyerGUI buyerGUI = new BuyerGUI(userID); 118 | 119 | buyerGUI.setSize(screenSize); 120 | 121 | // Center the window on the screen 122 | buyerGUI.setLocationRelativeTo(null); 123 | buyerGUI.setVisible(true); 124 | break; 125 | 126 | case "PurchaseHistory": 127 | // Ensure InventoryGUI is correctly initialized 128 | SwingUtilities.getWindowAncestor(this).setVisible(false); 129 | BuyerPurchaseHistory purchaseHistoryGUI = new BuyerPurchaseHistory(userID); 130 | // Set the size of the window to full screen 131 | Dimension screenSize1 = Toolkit.getDefaultToolkit().getScreenSize(); 132 | purchaseHistoryGUI.setSize(screenSize1); 133 | 134 | // Center the window on the screen 135 | purchaseHistoryGUI.setLocationRelativeTo(null); 136 | purchaseHistoryGUI.setVisible(true); 137 | break; 138 | 139 | 140 | case "Restocking": 141 | // Ensure InventoryGUI is correctly initialized 142 | SwingUtilities.getWindowAncestor(this).setVisible(false); 143 | RestockGUI restockGUI = new RestockGUI(userID); 144 | // Set the size of the window to full screen 145 | restockGUI.setSize(screenSize); 146 | // Center the window on the screen 147 | restockGUI.setLocationRelativeTo(null); 148 | restockGUI.setVisible(true); 149 | break; 150 | 151 | default: 152 | JOptionPane.showMessageDialog(this, "Unknown section: " + command); 153 | break; 154 | } 155 | } catch (Exception ex) { 156 | ex.printStackTrace(); 157 | JOptionPane.showMessageDialog(this, "An error occurred while switching views: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 158 | } 159 | } 160 | 161 | 162 | } 163 | -------------------------------------------------------------------------------- /src/BuyerPurchaseHistory.java: -------------------------------------------------------------------------------- 1 | import com.toedter.calendar.JCalendar; // Import for the JCalendar component 2 | import javax.swing.*; 3 | import javax.swing.border.EmptyBorder; 4 | import javax.swing.table.DefaultTableModel; 5 | import java.awt.*; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.sql.*; 9 | import java.util.Date; 10 | 11 | public class BuyerPurchaseHistory extends JFrame { 12 | private JTextField searchField; 13 | private JTextField dateField; 14 | private JButton searchButton; 15 | private JButton dateButton; 16 | private JTable resultsTable; 17 | private DefaultTableModel tableModel; 18 | 19 | 20 | private static final String URL = "jdbc:mysql://localhost:3308/inventory"; 21 | private static final String USER = "root"; 22 | private static final String PASSWORD = "2003"; 23 | 24 | public BuyerPurchaseHistory(int userId) { 25 | // Set up the frame 26 | setTitle("Buyer Purchase History"); 27 | setSize(1200, 800); 28 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 29 | setLocationRelativeTo(null); 30 | setLayout(new BorderLayout()); 31 | 32 | // Create header panel 33 | JPanel headerPanel = new HeaderPanel(userId); 34 | add(headerPanel, BorderLayout.NORTH); 35 | 36 | // Create components 37 | searchField = new JTextField(20); 38 | dateField = new JTextField(10); 39 | dateField.setEditable(false); 40 | dateButton = new JButton("Select Date"); 41 | searchButton = new JButton("Search"); 42 | 43 | 44 | searchButton.setBackground(new Color(30, 144, 255)); 45 | searchButton.setForeground(Color.WHITE); 46 | searchButton.setFocusPainted(false); 47 | dateButton.setBackground(new Color(30, 144, 255)); 48 | dateButton.setForeground(Color.WHITE); 49 | dateButton.setFocusPainted(false); 50 | 51 | 52 | tableModel = new DefaultTableModel(); 53 | tableModel.setColumnIdentifiers(new String[]{"First Name", "Last Name", "Purchase ID", "Item ID", "Purchase Date", "Quantity", "Total Price", "Status"}); 54 | resultsTable = new JTable(tableModel); 55 | resultsTable.setFillsViewportHeight(true); 56 | resultsTable.setRowHeight(30); 57 | resultsTable.setFont(new Font("Arial", Font.PLAIN, 14)); 58 | resultsTable.setSelectionBackground(new Color(173, 216, 230)); // Light blue selection color 59 | 60 | // Style table headers 61 | resultsTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16)); 62 | resultsTable.getTableHeader().setBackground(new Color(0, 102, 204)); // Dark blue header 63 | resultsTable.getTableHeader().setForeground(Color.WHITE); 64 | resultsTable.getTableHeader().setReorderingAllowed(false); 65 | 66 | 67 | JPanel searchPanel = new JPanel(); 68 | searchPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 69 | searchPanel.setBackground(Color.WHITE); 70 | searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 71 | searchPanel.add(new JLabel("Search by Name:")); 72 | searchPanel.add(searchField); 73 | searchPanel.add(new JLabel("Selected Date:")); 74 | searchPanel.add(dateField); 75 | searchPanel.add(dateButton); 76 | searchPanel.add(searchButton); 77 | 78 | 79 | JScrollPane scrollPane = new JScrollPane(resultsTable); 80 | scrollPane.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 2)); 81 | 82 | JPanel container = new JPanel(); 83 | container.setLayout(new BorderLayout()); 84 | container.add(searchPanel, BorderLayout.NORTH); 85 | container.add(scrollPane, BorderLayout.CENTER); 86 | add(container, BorderLayout.CENTER); 87 | 88 | 89 | dateButton.addActionListener(new ActionListener() { 90 | @Override 91 | public void actionPerformed(ActionEvent e) { 92 | JCalendar calendar = new JCalendar(); // Initialize JCalendar 93 | int result = JOptionPane.showConfirmDialog(null, calendar, "Select Date", JOptionPane.OK_CANCEL_OPTION); 94 | if (result == JOptionPane.OK_OPTION) { 95 | Date selectedDate = calendar.getDate(); 96 | dateField.setText(new java.text.SimpleDateFormat("yyyy-MM-dd").format(selectedDate)); 97 | } 98 | } 99 | }); 100 | 101 | 102 | searchButton.addActionListener(new ActionListener() { 103 | @Override 104 | public void actionPerformed(ActionEvent e) { 105 | searchPurchaseHistory(); 106 | } 107 | }); 108 | } 109 | 110 | private void searchPurchaseHistory() { 111 | String searchName = searchField.getText().trim(); 112 | String selectedDate = dateField.getText().trim(); // Get selected date as a string 113 | 114 | StringBuilder query = new StringBuilder("SELECT b.FirstName, b.LastName, p.PurchaseID, p.ItemID, p.PurchaseDate, p.Quantity, p.TotalPrice, p.Status " + 115 | "FROM Buyer b " + 116 | "INNER JOIN PurchaseItem p ON b.BuyerID = p.BuyerID " + 117 | "WHERE 1=1 "); 118 | 119 | // Append the conditions based on the inputs 120 | if (!searchName.isEmpty()) { 121 | query.append("AND CONCAT(b.FirstName, ' ', b.LastName) LIKE ? "); 122 | } 123 | if (!selectedDate.isEmpty()) { 124 | query.append("AND p.PurchaseDate = ? "); 125 | } 126 | 127 | query.append("ORDER BY p.PurchaseDate DESC"); 128 | try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD); 129 | PreparedStatement statement = connection.prepareStatement(query.toString())) { 130 | 131 | int paramIndex = 1; 132 | 133 | // Set parameters based on the inputs 134 | if (!searchName.isEmpty()) { 135 | statement.setString(paramIndex++, "%" + searchName + "%"); 136 | } 137 | if (!selectedDate.isEmpty()) { 138 | statement.setString(paramIndex++, selectedDate); 139 | } 140 | 141 | ResultSet resultSet = statement.executeQuery(); 142 | tableModel.setRowCount(0); // Clear existing data 143 | 144 | while (resultSet.next()) { 145 | Object[] row = new Object[]{ 146 | resultSet.getString("FirstName"), 147 | resultSet.getString("LastName"), 148 | resultSet.getInt("PurchaseID"), 149 | resultSet.getInt("ItemID"), 150 | resultSet.getDate("PurchaseDate"), 151 | resultSet.getInt("Quantity"), 152 | resultSet.getBigDecimal("TotalPrice"), 153 | resultSet.getString("Status") 154 | }; 155 | tableModel.addRow(row); 156 | } 157 | 158 | } catch (SQLException e) { 159 | e.printStackTrace(); 160 | JOptionPane.showMessageDialog(this, "Error retrieving data.", "Error", JOptionPane.ERROR_MESSAGE); 161 | } 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /src/LoginGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | import java.awt.event.KeyAdapter; 6 | import java.awt.event.KeyEvent; 7 | import java.sql.SQLException; 8 | 9 | public class LoginGUI extends JFrame { 10 | private JTextField usernameField; 11 | private JPasswordField passwordField; 12 | private JButton loginButton; 13 | private AdminDAO adminDAO; 14 | 15 | public LoginGUI() { 16 | try { 17 | adminDAO = new AdminDAO(); // Initialize AdminDAO for DB operations 18 | } catch (SQLException e) { 19 | e.printStackTrace(); 20 | JOptionPane.showMessageDialog(null, "Failed to connect to the database."); 21 | System.exit(1); 22 | } 23 | setSize(1200, 800); 24 | setTitle("Inventory Management System - Login"); 25 | setExtendedState(JFrame.MAXIMIZED_BOTH); // Maximize the screen size 26 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 | setLocationRelativeTo(null); // Center the window 28 | 29 | // Set a custom background color 30 | getContentPane().setBackground(new Color(240, 248, 255)); // Light blue color 31 | 32 | // Create and style the UI components 33 | usernameField = new JTextField(15); 34 | usernameField.setFont(new Font("Arial", Font.PLAIN, 16)); 35 | usernameField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 2)); 36 | 37 | passwordField = new JPasswordField(15); 38 | passwordField.setFont(new Font("Arial", Font.PLAIN, 16)); 39 | passwordField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 2)); 40 | 41 | loginButton = new JButton("Login"); 42 | loginButton.setFont(new Font("Arial", Font.BOLD, 16)); 43 | loginButton.setBackground(new Color(0, 102, 204)); 44 | loginButton.setForeground(Color.WHITE); // White text 45 | loginButton.setFocusPainted(false); 46 | loginButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); 47 | 48 | // Create and style labels 49 | JLabel usernameLabel = new JLabel("Username:"); 50 | usernameLabel.setFont(new Font("Arial", Font.BOLD, 18)); 51 | usernameLabel.setForeground(new Color(0, 102, 204)); // Dark blue color 52 | usernameLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); 53 | 54 | JLabel passwordLabel = new JLabel("Password:"); 55 | passwordLabel.setFont(new Font("Arial", Font.BOLD, 18)); 56 | passwordLabel.setForeground(new Color(0, 102, 204)); // Dark blue color 57 | passwordLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); 58 | 59 | // Create and style the title label 60 | JLabel titleLabel = new JLabel("Login"); 61 | titleLabel.setFont(new Font("Arial", Font.BOLD, 36)); 62 | titleLabel.setForeground(new Color(0, 102, 204)); // Dark blue color 63 | titleLabel.setHorizontalAlignment(JLabel.CENTER); 64 | 65 | // Create a panel for the form and add a border around it 66 | JPanel formPanel = new JPanel(new GridBagLayout()); 67 | formPanel.setBorder(BorderFactory.createCompoundBorder( 68 | BorderFactory.createLineBorder(new Color(0, 102, 204), 2), 69 | BorderFactory.createEmptyBorder(20, 20, 20, 20) // Padding inside the border 70 | )); 71 | formPanel.setOpaque(false); // Transparent background 72 | GridBagConstraints gbc = new GridBagConstraints(); 73 | gbc.insets = new Insets(15, 15, 15, 15); 74 | 75 | // Add components to the form panel 76 | gbc.gridx = 0; 77 | gbc.gridy = 0; 78 | gbc.gridwidth = 2; // Span across two columns 79 | gbc.anchor = GridBagConstraints.CENTER; 80 | formPanel.add(titleLabel, gbc); 81 | 82 | gbc.gridx = 0; 83 | gbc.gridy = 1; 84 | gbc.gridwidth = 1; // Reset to one column 85 | gbc.anchor = GridBagConstraints.WEST; 86 | formPanel.add(usernameLabel, gbc); 87 | 88 | gbc.gridx = 1; 89 | gbc.anchor = GridBagConstraints.EAST; 90 | formPanel.add(usernameField, gbc); 91 | 92 | gbc.gridx = 0; 93 | gbc.gridy = 2; 94 | gbc.anchor = GridBagConstraints.WEST; 95 | formPanel.add(passwordLabel, gbc); 96 | 97 | gbc.gridx = 1; 98 | gbc.anchor = GridBagConstraints.EAST; 99 | formPanel.add(passwordField, gbc); 100 | 101 | gbc.gridx = 1; 102 | gbc.gridy = 3; 103 | gbc.anchor = GridBagConstraints.CENTER; 104 | formPanel.add(loginButton, gbc); 105 | 106 | // Center the form panel within the frame 107 | setLayout(new GridBagLayout()); 108 | add(formPanel); 109 | 110 | // Add ActionListener to the login button 111 | loginButton.addActionListener(new LoginButtonListener()); 112 | 113 | // Add ActionListener to the password field for "Enter" key 114 | passwordField.addActionListener(new LoginButtonListener()); 115 | 116 | usernameField.addKeyListener(new ArrowKeyListener()); 117 | passwordField.addKeyListener(new ArrowKeyListener()); 118 | 119 | // Display the window 120 | setVisible(true); 121 | } 122 | 123 | private class ArrowKeyListener extends KeyAdapter { 124 | @Override 125 | public void keyPressed(KeyEvent e) { 126 | if (e.getKeyCode() == KeyEvent.VK_DOWN) { 127 | // Focus the password field when the down arrow is pressed 128 | passwordField.requestFocus(); 129 | } else if (e.getKeyCode() == KeyEvent.VK_UP) { 130 | // Focus the username field when the up arrow is pressed 131 | usernameField.requestFocus(); 132 | } 133 | } 134 | } 135 | 136 | private class LoginButtonListener implements ActionListener { 137 | @Override 138 | public void actionPerformed(ActionEvent e) { 139 | validateAndLogin(); 140 | } 141 | } 142 | 143 | private void validateAndLogin() { 144 | String username = usernameField.getText(); 145 | String password = new String(passwordField.getPassword()); 146 | 147 | try { 148 | if (adminDAO.validateLogin(username, password)) { 149 | JOptionPane.showMessageDialog(LoginGUI.this, "Login successful!"); 150 | openAdminDashboard(adminDAO.getUserId(username)); // Pass user ID to the dashboard 151 | } else { 152 | JOptionPane.showMessageDialog(LoginGUI.this, "Invalid username or password."); 153 | } 154 | } catch (SQLException ex) { 155 | ex.printStackTrace(); 156 | JOptionPane.showMessageDialog(LoginGUI.this, "Database error: " + ex.getMessage()); 157 | } 158 | } 159 | 160 | private void openAdminDashboard(int userId) { 161 | 162 | // Hide the login frame 163 | this.setVisible(false); 164 | 165 | 166 | SwingUtilities.invokeLater(() -> { 167 | AdminDashboard dashboard = new AdminDashboard(userId); 168 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 169 | dashboard.setSize(screenSize); 170 | // Center the window on the screen 171 | dashboard.setLocationRelativeTo(null); 172 | }); 173 | } 174 | 175 | public static void main(String[] args) { 176 | SwingUtilities.invokeLater(() -> new LoginGUI().setVisible(true)); 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/RestockGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.event.ListSelectionEvent; 3 | import javax.swing.event.ListSelectionListener; 4 | import javax.swing.table.DefaultTableCellRenderer; 5 | import javax.swing.table.JTableHeader; 6 | import java.awt.*; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.math.BigDecimal; 10 | import java.util.List; 11 | 12 | public class RestockGUI extends JFrame { 13 | private ItemDAO itemDAO; 14 | private JTextField itemIDField; 15 | private JTextField restockQuantityField; 16 | private JTable itemTable; 17 | private ItemTableModel tableModel; 18 | 19 | public RestockGUI(int userId) { 20 | super("Restocking"); 21 | HeaderPanel headerPanel = new HeaderPanel(userId); 22 | setLayout(new BorderLayout()); 23 | add(headerPanel, BorderLayout.NORTH); 24 | 25 | itemDAO = new ItemDAO(); 26 | 27 | JPanel inputPanel = new JPanel(); 28 | inputPanel.setLayout(new GridBagLayout()); 29 | inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Restock Item")); 30 | GridBagConstraints gbc = new GridBagConstraints(); 31 | gbc.insets = new Insets(10, 10, 10, 10); 32 | gbc.fill = GridBagConstraints.HORIZONTAL; 33 | gbc.gridx = 0; 34 | gbc.gridy = 0; 35 | gbc.anchor = GridBagConstraints.WEST; 36 | 37 | // Labels and fields 38 | JLabel itemIDLabel = new JLabel("Item ID:"); 39 | itemIDLabel.setFont(new Font("Arial", Font.BOLD, 14)); 40 | inputPanel.add(itemIDLabel, gbc); 41 | 42 | itemIDField = new JTextField(20); 43 | itemIDField.setFont(new Font("Arial", Font.PLAIN, 14)); 44 | itemIDField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 45 | gbc.gridx = 1; 46 | inputPanel.add(itemIDField, gbc); 47 | 48 | gbc.gridx = 0; 49 | gbc.gridy = 1; 50 | JLabel restockQuantityLabel = new JLabel("Restock Quantity:"); 51 | restockQuantityLabel.setFont(new Font("Arial", Font.BOLD, 14)); 52 | inputPanel.add(restockQuantityLabel, gbc); 53 | 54 | restockQuantityField = new JTextField(20); 55 | restockQuantityField.setFont(new Font("Arial", Font.PLAIN, 14)); 56 | restockQuantityField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 57 | gbc.gridx = 1; 58 | inputPanel.add(restockQuantityField, gbc); 59 | 60 | JButton restockButton = new JButton("Restock Item"); 61 | restockButton.setFont(new Font("Arial", Font.BOLD, 14)); 62 | restockButton.setBackground(new Color(0, 102, 204)); 63 | restockButton.setForeground(Color.WHITE); 64 | restockButton.setFocusPainted(false); 65 | restockButton.addActionListener(new RestockButtonListener()); 66 | gbc.gridx = 0; 67 | gbc.gridy = 2; 68 | gbc.gridwidth = 2; 69 | gbc.anchor = GridBagConstraints.CENTER; 70 | inputPanel.add(restockButton, gbc); 71 | 72 | add(inputPanel, BorderLayout.WEST); 73 | 74 | // Table for displaying items 75 | tableModel = new ItemTableModel(); 76 | itemTable = new JTable(tableModel); 77 | itemTable.setFont(new Font("Arial", Font.PLAIN, 14)); 78 | itemTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 79 | itemTable.setRowHeight(40); // Increase row height for better visibility 80 | itemTable.setBorder(BorderFactory.createLineBorder(Color.GRAY)); 81 | 82 | JTableHeader header = itemTable.getTableHeader(); 83 | header.setFont(new Font("Arial", Font.BOLD, 14)); 84 | header.setBackground(new Color(0, 102, 204)); 85 | header.setForeground(Color.WHITE); 86 | 87 | // Customize cell renderer to align text 88 | DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) itemTable.getTableHeader().getDefaultRenderer(); 89 | renderer.setHorizontalAlignment(SwingConstants.CENTER); 90 | 91 | JScrollPane scrollPane = new JScrollPane(itemTable); 92 | scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Items List")); 93 | add(scrollPane, BorderLayout.CENTER); 94 | 95 | // Set maximum size for the panel 96 | setPreferredSize(new Dimension(1200, 800)); 97 | setMinimumSize(new Dimension(1200, 800)); 98 | setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); 99 | 100 | loadItems(); 101 | 102 | itemTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 103 | @Override 104 | public void valueChanged(ListSelectionEvent event) { 105 | int selectedRow = itemTable.getSelectedRow(); 106 | if (!event.getValueIsAdjusting() && selectedRow != -1) { 107 | // Get the item ID from the selected row 108 | int itemId = (int) tableModel.getValueAt(selectedRow, 0); // Assuming ID is in the first column 109 | itemIDField.setText(String.valueOf(itemId)); 110 | } 111 | } 112 | }); 113 | } 114 | 115 | private void loadItems() { 116 | List items = itemDAO.getAllItems(); 117 | tableModel.setItems(items); 118 | } 119 | 120 | private class RestockButtonListener implements ActionListener { 121 | @Override 122 | public void actionPerformed(ActionEvent e) { 123 | try { 124 | int itemId = Integer.parseInt(itemIDField.getText()); 125 | int restockQuantity = Integer.parseInt(restockQuantityField.getText()); 126 | 127 | // Get the current quantity from the database 128 | Item item = itemDAO.getItemById(itemId); 129 | if (item == null) { 130 | JOptionPane.showMessageDialog(RestockGUI.this, "Item not found."); 131 | return; 132 | } 133 | 134 | int currentQuantity = item.getQuantity(); 135 | int newTotalQuantity = currentQuantity + restockQuantity; 136 | 137 | // Ask for confirmation before proceeding 138 | int confirmation = JOptionPane.showConfirmDialog(RestockGUI.this, 139 | "Are you sure you want to restock this item?\n" + 140 | "Current Quantity: " + currentQuantity + "\n" + 141 | "Restock Quantity: " + restockQuantity + "\n" + 142 | "New Total Quantity: " + newTotalQuantity, 143 | "Confirm Restock", 144 | JOptionPane.YES_NO_OPTION); 145 | 146 | // Proceed with restocking if the user confirms 147 | if (confirmation == JOptionPane.YES_OPTION) { 148 | if (itemDAO.updateItemQuantity(itemId, newTotalQuantity)) { 149 | JOptionPane.showMessageDialog(RestockGUI.this, "Item restocked successfully. New Quantity: " + newTotalQuantity); 150 | loadItems(); 151 | clearFields(); 152 | } else { 153 | JOptionPane.showMessageDialog(RestockGUI.this, "Failed to restock item."); 154 | } 155 | } else { 156 | JOptionPane.showMessageDialog(RestockGUI.this, "Restock action canceled."); 157 | } 158 | } catch (Exception ex) { 159 | JOptionPane.showMessageDialog(RestockGUI.this, "Error: " + ex.getMessage()); 160 | } 161 | } 162 | } 163 | 164 | 165 | private void clearFields() { 166 | itemIDField.setText(""); 167 | restockQuantityField.setText(""); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/SupplierGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.table.DefaultTableCellRenderer; 3 | import javax.swing.table.JTableHeader; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | import java.util.List; 8 | 9 | public class SupplierGUI extends JFrame { 10 | private SupplierDAO supplierDAO; 11 | private JTextField nameField; 12 | private JTextField addressField; 13 | private JTextField contactField; 14 | private JTable supplierTable; 15 | private SupplierTableModel tableModel; 16 | 17 | public SupplierGUI(int userid) { 18 | super("Supplier Management"); 19 | // Create the HeaderPanel with parameters 20 | HeaderPanel headerPanel = new HeaderPanel(userid); 21 | setLayout(new BorderLayout()); 22 | add(headerPanel, BorderLayout.NORTH); 23 | 24 | supplierDAO = new SupplierDAO(); 25 | 26 | // Panel for input fields and buttons 27 | JPanel inputPanel = new JPanel(); 28 | inputPanel.setLayout(new GridBagLayout()); 29 | inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Supplier Details")); 30 | GridBagConstraints gbc = new GridBagConstraints(); 31 | gbc.insets = new Insets(10, 10, 10, 10); 32 | gbc.fill = GridBagConstraints.HORIZONTAL; 33 | gbc.gridx = 0; 34 | gbc.gridy = 0; 35 | gbc.anchor = GridBagConstraints.WEST; 36 | 37 | // Labels and fields 38 | JLabel nameLabel = new JLabel("Supplier Name:"); 39 | nameLabel.setFont(new Font("Arial", Font.BOLD, 14)); 40 | inputPanel.add(nameLabel, gbc); 41 | 42 | nameField = new JTextField(20); 43 | nameField.setFont(new Font("Arial", Font.PLAIN, 14)); 44 | nameField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 45 | gbc.gridx = 1; 46 | inputPanel.add(nameField, gbc); 47 | 48 | gbc.gridx = 0; 49 | gbc.gridy = 1; 50 | JLabel addressLabel = new JLabel("Address:"); 51 | addressLabel.setFont(new Font("Arial", Font.BOLD, 14)); 52 | inputPanel.add(addressLabel, gbc); 53 | 54 | addressField = new JTextField(20); 55 | addressField.setFont(new Font("Arial", Font.PLAIN, 14)); 56 | addressField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 57 | gbc.gridx = 1; 58 | inputPanel.add(addressField, gbc); 59 | 60 | gbc.gridx = 0; 61 | gbc.gridy = 2; 62 | JLabel contactLabel = new JLabel("Contact Number:"); 63 | contactLabel.setFont(new Font("Arial", Font.BOLD, 14)); 64 | inputPanel.add(contactLabel, gbc); 65 | 66 | contactField = new JTextField(20); 67 | contactField.setFont(new Font("Arial", Font.PLAIN, 14)); 68 | contactField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 69 | gbc.gridx = 1; 70 | inputPanel.add(contactField, gbc); 71 | 72 | JButton addButton = new JButton("Add Supplier"); 73 | addButton.setFont(new Font("Arial", Font.BOLD, 14)); 74 | addButton.setBackground(new Color(0, 102, 204)); 75 | addButton.setForeground(Color.WHITE); 76 | addButton.setFocusPainted(false); 77 | addButton.addActionListener(new AddButtonListener()); 78 | gbc.gridx = 0; 79 | gbc.gridy = 3; 80 | gbc.gridwidth = 2; 81 | gbc.anchor = GridBagConstraints.CENTER; 82 | inputPanel.add(addButton, gbc); 83 | 84 | JButton updateButton = new JButton("Update Supplier"); 85 | updateButton.setFont(new Font("Arial", Font.BOLD, 14)); 86 | updateButton.setBackground(new Color(0, 102, 204)); 87 | updateButton.setForeground(Color.WHITE); 88 | updateButton.setFocusPainted(false); 89 | updateButton.addActionListener(new UpdateButtonListener()); 90 | gbc.gridy = 4; 91 | inputPanel.add(updateButton, gbc); 92 | 93 | JButton deleteButton = new JButton("Delete Supplier"); 94 | deleteButton.setFont(new Font("Arial", Font.BOLD, 14)); 95 | deleteButton.setBackground(new Color(0, 102, 204)); 96 | deleteButton.setForeground(Color.WHITE); 97 | deleteButton.setFocusPainted(false); 98 | deleteButton.addActionListener(new DeleteButtonListener()); 99 | gbc.gridy = 5; 100 | inputPanel.add(deleteButton, gbc); 101 | 102 | JButton clearButton = new JButton("Clear"); 103 | clearButton.setFont(new Font("Arial", Font.BOLD, 14)); 104 | clearButton.setBackground(new Color(0, 102, 204)); 105 | clearButton.setForeground(Color.WHITE); 106 | clearButton.setFocusPainted(false); 107 | clearButton.addActionListener(new ClearButtonListener()); 108 | gbc.gridy = 6; 109 | inputPanel.add(clearButton, gbc); 110 | 111 | add(inputPanel, BorderLayout.WEST); 112 | 113 | // Table for displaying suppliers 114 | tableModel = new SupplierTableModel(); 115 | supplierTable = new JTable(tableModel); 116 | supplierTable.setFont(new Font("Arial", Font.PLAIN, 14)); 117 | supplierTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 118 | supplierTable.setRowHeight(40); // Increase row height for better visibility 119 | supplierTable.setBorder(BorderFactory.createLineBorder(Color.GRAY)); 120 | 121 | // Customize the table header 122 | JTableHeader header = supplierTable.getTableHeader(); 123 | header.setFont(new Font("Arial", Font.BOLD, 14)); 124 | header.setBackground(new Color(0, 102, 204)); 125 | header.setForeground(Color.WHITE); 126 | 127 | // Customize cell renderer to align text 128 | DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) supplierTable.getTableHeader().getDefaultRenderer(); 129 | renderer.setHorizontalAlignment(SwingConstants.CENTER); 130 | 131 | JScrollPane scrollPane = new JScrollPane(supplierTable); 132 | scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Suppliers List")); 133 | add(scrollPane, BorderLayout.CENTER); 134 | 135 | // Set maximum size for the panel 136 | setPreferredSize(new Dimension(1200, 800)); 137 | setMinimumSize(new Dimension(1200, 800)); 138 | setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); 139 | 140 | loadSuppliers(); 141 | 142 | supplierTable.getSelectionModel().addListSelectionListener(e -> { 143 | if (!e.getValueIsAdjusting()) { 144 | int selectedRow = supplierTable.getSelectedRow(); 145 | if (selectedRow >= 0) { 146 | Supplier selectedSupplier = tableModel.getSupplierAt(selectedRow); 147 | populateFields(selectedSupplier); 148 | } 149 | } 150 | }); 151 | } 152 | 153 | private void loadSuppliers() { 154 | List suppliers = supplierDAO.getAllSuppliers(); 155 | tableModel.setSuppliers(suppliers); 156 | } 157 | 158 | private class AddButtonListener implements ActionListener { 159 | @Override 160 | public void actionPerformed(ActionEvent e) { 161 | String name = nameField.getText(); 162 | String address = addressField.getText(); 163 | String contact = contactField.getText(); 164 | if (supplierDAO.addSupplier(name, address, contact)) { 165 | JOptionPane.showMessageDialog(SupplierGUI.this, "Supplier added successfully."); 166 | loadSuppliers(); 167 | clearFields(); 168 | } else { 169 | JOptionPane.showMessageDialog(SupplierGUI.this, "Failed to add supplier."); 170 | } 171 | } 172 | } 173 | 174 | private class UpdateButtonListener implements ActionListener { 175 | @Override 176 | public void actionPerformed(ActionEvent e) { 177 | int selectedRow = supplierTable.getSelectedRow(); 178 | if (selectedRow >= 0) { 179 | Supplier supplier = tableModel.getSupplierAt(selectedRow); 180 | String name = nameField.getText(); 181 | String address = addressField.getText(); 182 | String contact = contactField.getText(); 183 | if (supplierDAO.updateSupplier(supplier.getId(), name, address, contact)) { 184 | JOptionPane.showMessageDialog(SupplierGUI.this, "Supplier updated successfully."); 185 | loadSuppliers(); 186 | clearFields(); 187 | } else { 188 | JOptionPane.showMessageDialog(SupplierGUI.this, "Failed to update supplier."); 189 | } 190 | } else { 191 | JOptionPane.showMessageDialog(SupplierGUI.this, "Select a supplier to update."); 192 | } 193 | } 194 | } 195 | 196 | private class DeleteButtonListener implements ActionListener { 197 | @Override 198 | public void actionPerformed(ActionEvent e) { 199 | int selectedRow = supplierTable.getSelectedRow(); 200 | if (selectedRow >= 0) { 201 | Supplier supplier = tableModel.getSupplierAt(selectedRow); 202 | int confirmation = JOptionPane.showConfirmDialog(SupplierGUI.this, 203 | "Are you sure you want to delete this supplier?", 204 | "Confirm Delete", JOptionPane.YES_NO_OPTION); 205 | if (confirmation == JOptionPane.YES_OPTION) { 206 | if (supplierDAO.deleteSupplier(supplier.getId())) { 207 | JOptionPane.showMessageDialog(SupplierGUI.this, "Supplier deleted successfully."); 208 | loadSuppliers(); 209 | clearFields(); 210 | } else { 211 | JOptionPane.showMessageDialog(SupplierGUI.this, "Failed to delete supplier."); 212 | } 213 | } 214 | } else { 215 | JOptionPane.showMessageDialog(SupplierGUI.this, "Select a supplier to delete."); 216 | } 217 | } 218 | } 219 | 220 | private class ClearButtonListener implements ActionListener{ 221 | 222 | @Override 223 | public void actionPerformed(ActionEvent e) { 224 | clearFields(); 225 | } 226 | } 227 | 228 | private void clearFields() { 229 | nameField.setText(""); 230 | addressField.setText(""); 231 | contactField.setText(""); 232 | } 233 | private void populateFields(Supplier supplier) { 234 | nameField.setText(supplier.getName()); 235 | addressField.setText(supplier.getAddress()); 236 | contactField.setText(supplier.getContactNumber()); 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /src/BuyerGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.event.ListSelectionEvent; 3 | import javax.swing.event.ListSelectionListener; 4 | import javax.swing.table.DefaultTableCellRenderer; 5 | import javax.swing.table.JTableHeader; 6 | import java.awt.*; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.util.List; 10 | 11 | public class BuyerGUI extends JFrame { 12 | private BuyerDAO buyerDAO; 13 | private JTextField firstNameField; 14 | private JTextField lastNameField; 15 | private JTextField phoneNumberField; 16 | private JTable buyerTable; 17 | private BuyerTableModel tableModel; 18 | 19 | public BuyerGUI( int userId) { 20 | 21 | super("Buyer Management"); 22 | HeaderPanel headerPanel = new HeaderPanel(userId); 23 | 24 | setLayout(new BorderLayout()); 25 | add(headerPanel, BorderLayout.NORTH); 26 | 27 | buyerDAO = new BuyerDAO(); // Initialize DAO 28 | 29 | // Panel for input fields and buttons 30 | JPanel inputPanel = new JPanel(); 31 | inputPanel.setLayout(new GridBagLayout()); 32 | inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Buyer Details")); 33 | GridBagConstraints gbc = new GridBagConstraints(); 34 | gbc.insets = new Insets(10, 10, 10, 10); 35 | gbc.fill = GridBagConstraints.HORIZONTAL; 36 | gbc.gridx = 0; 37 | gbc.gridy = 0; 38 | gbc.anchor = GridBagConstraints.WEST; 39 | 40 | // Labels and fields 41 | JLabel firstNameLabel = new JLabel("First Name:"); 42 | firstNameLabel.setFont(new Font("Arial", Font.BOLD, 14)); 43 | inputPanel.add(firstNameLabel, gbc); 44 | 45 | firstNameField = new JTextField(20); 46 | firstNameField.setFont(new Font("Arial", Font.PLAIN, 14)); 47 | firstNameField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 48 | gbc.gridx = 1; 49 | inputPanel.add(firstNameField, gbc); 50 | 51 | gbc.gridx = 0; 52 | gbc.gridy = 1; 53 | JLabel lastNameLabel = new JLabel("Last Name:"); 54 | lastNameLabel.setFont(new Font("Arial", Font.BOLD, 14)); 55 | inputPanel.add(lastNameLabel, gbc); 56 | 57 | lastNameField = new JTextField(20); 58 | lastNameField.setFont(new Font("Arial", Font.PLAIN, 14)); 59 | lastNameField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 60 | gbc.gridx = 1; 61 | inputPanel.add(lastNameField, gbc); 62 | 63 | gbc.gridx = 0; 64 | gbc.gridy = 2; 65 | JLabel phoneNumberLabel = new JLabel("Phone Number:"); 66 | phoneNumberLabel.setFont(new Font("Arial", Font.BOLD, 14)); 67 | inputPanel.add(phoneNumberLabel, gbc); 68 | 69 | phoneNumberField = new JTextField(20); 70 | phoneNumberField.setFont(new Font("Arial", Font.PLAIN, 14)); 71 | phoneNumberField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 72 | gbc.gridx = 1; 73 | inputPanel.add(phoneNumberField, gbc); 74 | 75 | JButton addButton = new JButton("Add Buyer"); 76 | addButton.setFont(new Font("Arial", Font.BOLD, 14)); 77 | addButton.setBackground(new Color(0, 102, 204)); 78 | addButton.setForeground(Color.WHITE); 79 | addButton.setFocusPainted(false); 80 | addButton.addActionListener(new AddButtonListener()); 81 | gbc.gridx = 0; 82 | gbc.gridy = 3; 83 | gbc.gridwidth = 2; 84 | gbc.anchor = GridBagConstraints.CENTER; 85 | inputPanel.add(addButton, gbc); 86 | 87 | JButton updateButton = new JButton("Update Buyer"); 88 | updateButton.setFont(new Font("Arial", Font.BOLD, 14)); 89 | updateButton.setBackground(new Color(0, 102, 204)); 90 | updateButton.setForeground(Color.WHITE); 91 | updateButton.setFocusPainted(false); 92 | updateButton.addActionListener(new UpdateButtonListener()); 93 | gbc.gridy = 4; 94 | inputPanel.add(updateButton, gbc); 95 | 96 | JButton deleteButton = new JButton("Delete Buyer"); 97 | deleteButton.setFont(new Font("Arial", Font.BOLD, 14)); 98 | deleteButton.setBackground(new Color(0, 102, 204)); 99 | deleteButton.setForeground(Color.WHITE); 100 | deleteButton.setFocusPainted(false); 101 | deleteButton.addActionListener(new DeleteButtonListener()); 102 | gbc.gridy = 5; 103 | inputPanel.add(deleteButton, gbc); 104 | 105 | JButton clearButton = new JButton("Clear "); 106 | clearButton.setFont(new Font("Arial", Font.BOLD, 14)); 107 | clearButton.setBackground(new Color(0, 102, 204)); 108 | clearButton.setForeground(Color.WHITE); 109 | clearButton.setFocusPainted(false); 110 | clearButton.addActionListener(new ClearButtonListner()); 111 | gbc.gridy = 6; 112 | inputPanel.add(clearButton, gbc); 113 | 114 | add(inputPanel, BorderLayout.WEST); 115 | 116 | // Table for displaying buyers 117 | tableModel = new BuyerTableModel(); 118 | buyerTable = new JTable(tableModel); 119 | buyerTable.setFont(new Font("Arial", Font.PLAIN, 14)); 120 | buyerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 121 | buyerTable.setRowHeight(40); // Increase row height for better visibility 122 | buyerTable.setBorder(BorderFactory.createLineBorder(Color.GRAY)); 123 | 124 | // Customize the table header 125 | JTableHeader header = buyerTable.getTableHeader(); 126 | header.setFont(new Font("Arial", Font.BOLD, 14)); 127 | header.setBackground(new Color(0, 102, 204)); 128 | header.setForeground(Color.WHITE); 129 | 130 | // Customize cell renderer to align text 131 | DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) buyerTable.getTableHeader().getDefaultRenderer(); 132 | renderer.setHorizontalAlignment(SwingConstants.CENTER); 133 | 134 | JScrollPane scrollPane = new JScrollPane(buyerTable); 135 | scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Buyer List")); 136 | add(scrollPane, BorderLayout.CENTER); 137 | 138 | loadBuyers(); // Load buyers from the database 139 | 140 | buyerTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 141 | @Override 142 | public void valueChanged(ListSelectionEvent e) { 143 | if (!e.getValueIsAdjusting()) { 144 | int selectedRow = buyerTable.getSelectedRow(); 145 | if (selectedRow >= 0) { 146 | Buyer selectedBuyer = tableModel.getBuyerAt(selectedRow); 147 | populateFields(selectedBuyer); 148 | } 149 | } 150 | } 151 | }); 152 | } 153 | 154 | private void loadBuyers() { 155 | List buyers = buyerDAO.getAllBuyers(); 156 | tableModel.setBuyers(buyers); 157 | } 158 | 159 | private class AddButtonListener implements ActionListener { 160 | @Override 161 | public void actionPerformed(ActionEvent e) { 162 | String firstName = firstNameField.getText(); 163 | String lastName = lastNameField.getText(); 164 | String phoneNumber = phoneNumberField.getText(); 165 | Buyer buyer = new Buyer(0, firstName, lastName, phoneNumber); 166 | if (buyerDAO.addBuyer(buyer)) { 167 | JOptionPane.showMessageDialog(BuyerGUI.this, "Buyer added successfully."); 168 | loadBuyers(); 169 | cleartextFields(); 170 | } else { 171 | JOptionPane.showMessageDialog(BuyerGUI.this, "Failed to add buyer."); 172 | } 173 | } 174 | } 175 | 176 | private class UpdateButtonListener implements ActionListener { 177 | @Override 178 | public void actionPerformed(ActionEvent e) { 179 | int selectedRow = buyerTable.getSelectedRow(); 180 | if (selectedRow != -1) { 181 | int buyerID = (int) buyerTable.getValueAt(selectedRow, 0); 182 | String firstName = firstNameField.getText(); 183 | String lastName = lastNameField.getText(); 184 | String phoneNumber = phoneNumberField.getText(); 185 | Buyer buyer = new Buyer(buyerID, firstName, lastName, phoneNumber); 186 | if (buyerDAO.updateBuyer(buyer)) { 187 | JOptionPane.showMessageDialog(BuyerGUI.this, "Buyer updated successfully."); 188 | loadBuyers(); 189 | cleartextFields(); 190 | } else { 191 | JOptionPane.showMessageDialog(BuyerGUI.this, "Failed to update buyer."); 192 | } 193 | } else { 194 | JOptionPane.showMessageDialog(BuyerGUI.this, "Please select a buyer to update."); 195 | } 196 | } 197 | } 198 | 199 | private void populateFields(Buyer buyer) { 200 | firstNameField.setText(buyer.getFirstName()); 201 | lastNameField.setText(buyer.getLastName()); 202 | phoneNumberField.setText(buyer.getPhoneNumber()); 203 | } 204 | 205 | private void cleartextFields() 206 | { 207 | firstNameField.setText(""); 208 | lastNameField.setText(""); 209 | phoneNumberField.setText(""); 210 | } 211 | 212 | private class DeleteButtonListener implements ActionListener { 213 | @Override 214 | public void actionPerformed(ActionEvent e) { 215 | int selectedRow = buyerTable.getSelectedRow(); 216 | if (selectedRow != -1) { 217 | // Retrieve the Buyer ID from the selected row 218 | int buyerID = (int) buyerTable.getValueAt(selectedRow, 0); 219 | 220 | // Confirm deletion 221 | int confirmation = JOptionPane.showConfirmDialog(BuyerGUI.this, 222 | "Are you sure you want to delete this Buyer?", 223 | "Confirm Delete", JOptionPane.YES_NO_OPTION); 224 | 225 | // Proceed if confirmed 226 | if (confirmation == JOptionPane.YES_OPTION) { 227 | // Try to delete the buyer 228 | if (buyerDAO.deleteBuyer(buyerID)) { 229 | JOptionPane.showMessageDialog(BuyerGUI.this, "Buyer deleted successfully."); 230 | // Reload the list of buyers to reflect the deletion 231 | loadBuyers(); 232 | } else { 233 | JOptionPane.showMessageDialog(BuyerGUI.this, "Failed to delete buyer."); 234 | } 235 | } 236 | } else { 237 | JOptionPane.showMessageDialog(BuyerGUI.this, "Please select a buyer to delete."); 238 | } 239 | } 240 | } 241 | 242 | 243 | private class ClearButtonListner implements ActionListener{ 244 | 245 | @Override 246 | public void actionPerformed(ActionEvent e) { 247 | cleartextFields(); 248 | } 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /src/ItemGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.table.DefaultTableCellRenderer; 3 | import javax.swing.table.JTableHeader; 4 | import java.awt.*; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | import java.math.BigDecimal; 8 | import java.util.List; 9 | 10 | public class ItemGUI extends JFrame { 11 | private ItemDAO itemDAO; 12 | private JTextField nameField; 13 | private JTextField descriptionField; 14 | private JTextField priceField; 15 | private JTextField quantityField; 16 | private JTextField supplierIDField; 17 | private JTable itemTable; 18 | private ItemTableModel tableModel; 19 | 20 | public ItemGUI( int userId) { 21 | 22 | super("Item Management"); 23 | HeaderPanel headerPanel = new HeaderPanel( userId); 24 | setLayout(new BorderLayout()); 25 | add(headerPanel, BorderLayout.NORTH); 26 | 27 | itemDAO = new ItemDAO(); 28 | 29 | 30 | JPanel inputPanel = new JPanel(); 31 | inputPanel.setLayout(new GridBagLayout()); 32 | inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Item Details")); 33 | GridBagConstraints gbc = new GridBagConstraints(); 34 | gbc.insets = new Insets(10, 10, 10, 10); 35 | gbc.fill = GridBagConstraints.HORIZONTAL; 36 | gbc.gridx = 0; 37 | gbc.gridy = 0; 38 | gbc.anchor = GridBagConstraints.WEST; 39 | 40 | // Labels and fields 41 | JLabel nameLabel = new JLabel("Item Name:"); 42 | nameLabel.setFont(new Font("Arial", Font.BOLD, 14)); 43 | inputPanel.add(nameLabel, gbc); 44 | 45 | nameField = new JTextField(20); 46 | nameField.setFont(new Font("Arial", Font.PLAIN, 14)); 47 | nameField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 48 | gbc.gridx = 1; 49 | inputPanel.add(nameField, gbc); 50 | 51 | gbc.gridx = 0; 52 | gbc.gridy = 1; 53 | JLabel descriptionLabel = new JLabel("Description:"); 54 | descriptionLabel.setFont(new Font("Arial", Font.BOLD, 14)); 55 | inputPanel.add(descriptionLabel, gbc); 56 | 57 | descriptionField = new JTextField(20); 58 | descriptionField.setFont(new Font("Arial", Font.PLAIN, 14)); 59 | descriptionField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 60 | gbc.gridx = 1; 61 | inputPanel.add(descriptionField, gbc); 62 | 63 | gbc.gridx = 0; 64 | gbc.gridy = 2; 65 | JLabel priceLabel = new JLabel("Price:"); 66 | priceLabel.setFont(new Font("Arial", Font.BOLD, 14)); 67 | inputPanel.add(priceLabel, gbc); 68 | 69 | priceField = new JTextField(20); 70 | priceField.setFont(new Font("Arial", Font.PLAIN, 14)); 71 | priceField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 72 | gbc.gridx = 1; 73 | inputPanel.add(priceField, gbc); 74 | 75 | gbc.gridx = 0; 76 | gbc.gridy = 3; 77 | JLabel quantityLabel = new JLabel("Quantity:"); 78 | quantityLabel.setFont(new Font("Arial", Font.BOLD, 14)); 79 | inputPanel.add(quantityLabel, gbc); 80 | 81 | quantityField = new JTextField(20); 82 | quantityField.setFont(new Font("Arial", Font.PLAIN, 14)); 83 | quantityField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 84 | gbc.gridx = 1; 85 | inputPanel.add(quantityField, gbc); 86 | 87 | gbc.gridx = 0; 88 | gbc.gridy = 4; 89 | JLabel supplierIDLabel = new JLabel("Supplier ID:"); 90 | supplierIDLabel.setFont(new Font("Arial", Font.BOLD, 14)); 91 | inputPanel.add(supplierIDLabel, gbc); 92 | 93 | supplierIDField = new JTextField(20); 94 | supplierIDField.setFont(new Font("Arial", Font.PLAIN, 14)); 95 | supplierIDField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 96 | gbc.gridx = 1; 97 | inputPanel.add(supplierIDField, gbc); 98 | 99 | JButton addButton = new JButton("Add Item"); 100 | addButton.setFont(new Font("Arial", Font.BOLD, 14)); 101 | addButton.setBackground(new Color(0, 102, 204)); 102 | addButton.setForeground(Color.WHITE); 103 | addButton.setFocusPainted(false); 104 | addButton.addActionListener(new AddButtonListener()); 105 | gbc.gridx = 0; 106 | gbc.gridy = 5; 107 | gbc.gridwidth = 2; 108 | gbc.anchor = GridBagConstraints.CENTER; 109 | inputPanel.add(addButton, gbc); 110 | 111 | JButton updateButton = new JButton("Update Item"); 112 | updateButton.setFont(new Font("Arial", Font.BOLD, 14)); 113 | updateButton.setBackground(new Color(0, 102, 204)); 114 | updateButton.setForeground(Color.WHITE); 115 | updateButton.setFocusPainted(false); 116 | updateButton.addActionListener(new UpdateButtonListener()); 117 | gbc.gridy = 6; 118 | inputPanel.add(updateButton, gbc); 119 | 120 | JButton deleteButton = new JButton("Delete Item"); 121 | deleteButton.setFont(new Font("Arial", Font.BOLD, 14)); 122 | deleteButton.setBackground(new Color(0, 102, 204)); 123 | deleteButton.setForeground(Color.WHITE); 124 | deleteButton.setFocusPainted(false); 125 | deleteButton.addActionListener(new DeleteButtonListener()); 126 | gbc.gridy = 7; 127 | inputPanel.add(deleteButton, gbc); 128 | 129 | JButton clearButton = new JButton("Clear "); 130 | clearButton.setFont(new Font("Arial", Font.BOLD, 14)); 131 | clearButton.setBackground(new Color(0, 102, 204)); 132 | clearButton.setForeground(Color.WHITE); 133 | clearButton.setFocusPainted(false); 134 | clearButton.addActionListener(new ClearButtonListner()); 135 | gbc.gridy = 8; 136 | inputPanel.add(clearButton, gbc); 137 | 138 | add(inputPanel, BorderLayout.WEST); 139 | 140 | // Table for displaying items 141 | tableModel = new ItemTableModel(); 142 | itemTable = new JTable(tableModel); 143 | itemTable.setFont(new Font("Arial", Font.PLAIN, 14)); 144 | itemTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 145 | itemTable.setRowHeight(40); // Increase row height for better visibility 146 | itemTable.setBorder(BorderFactory.createLineBorder(Color.GRAY)); 147 | 148 | 149 | JTableHeader header = itemTable.getTableHeader(); 150 | header.setFont(new Font("Arial", Font.BOLD, 14)); 151 | header.setBackground(new Color(0, 102, 204)); 152 | header.setForeground(Color.WHITE); 153 | 154 | // Customize cell renderer to align text 155 | DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) itemTable.getTableHeader().getDefaultRenderer(); 156 | renderer.setHorizontalAlignment(SwingConstants.CENTER); 157 | 158 | JScrollPane scrollPane = new JScrollPane(itemTable); 159 | scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Items List")); 160 | add(scrollPane, BorderLayout.CENTER); 161 | 162 | // Set maximum size for the panel 163 | setPreferredSize(new Dimension(1200, 800)); 164 | setMinimumSize(new Dimension(1200, 800)); 165 | setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); 166 | 167 | loadItems(); 168 | itemTable.getSelectionModel().addListSelectionListener(e -> { 169 | if (!e.getValueIsAdjusting()) { 170 | int selectedRow = itemTable.getSelectedRow(); 171 | if (selectedRow >= 0) { 172 | Item selectedItem = tableModel.getItemAt(selectedRow); 173 | populateFields(selectedItem); 174 | } 175 | } 176 | }); 177 | } 178 | 179 | private void loadItems() { 180 | List items = itemDAO.getAllItems(); 181 | tableModel.setItems(items); 182 | } 183 | 184 | private class AddButtonListener implements ActionListener { 185 | @Override 186 | public void actionPerformed(ActionEvent e) { 187 | try { 188 | Item item = new Item( 189 | 0, // ItemID will be auto-generated 190 | nameField.getText(), 191 | descriptionField.getText(), 192 | new BigDecimal(priceField.getText()), 193 | Integer.parseInt(quantityField.getText()), 194 | Integer.parseInt(supplierIDField.getText()) 195 | ); 196 | if (itemDAO.addItem(item)) { 197 | JOptionPane.showMessageDialog(ItemGUI.this, "Item added successfully."); 198 | loadItems(); 199 | clearFields(); 200 | } else { 201 | JOptionPane.showMessageDialog(ItemGUI.this, "Failed to add item."); 202 | } 203 | } catch (Exception ex) { 204 | JOptionPane.showMessageDialog(ItemGUI.this, "Error: " + ex.getMessage()); 205 | } 206 | } 207 | } 208 | 209 | private class UpdateButtonListener implements ActionListener { 210 | @Override 211 | public void actionPerformed(ActionEvent e) { 212 | int selectedRow = itemTable.getSelectedRow(); 213 | if (selectedRow >= 0) { 214 | Item item = tableModel.getItemAt(selectedRow); 215 | try { 216 | item.setName(nameField.getText()); 217 | item.setDescription(descriptionField.getText()); 218 | item.setPrice(new BigDecimal(priceField.getText())); 219 | item.setQuantity(Integer.parseInt(quantityField.getText())); 220 | item.setSupplierID(Integer.parseInt(supplierIDField.getText())); 221 | if (itemDAO.updateItem(item)) { 222 | JOptionPane.showMessageDialog(ItemGUI.this, "Item updated successfully."); 223 | loadItems(); 224 | clearFields(); 225 | } else { 226 | JOptionPane.showMessageDialog(ItemGUI.this, "Failed to update item."); 227 | } 228 | } catch (Exception ex) { 229 | JOptionPane.showMessageDialog(ItemGUI.this, "Error: " + ex.getMessage()); 230 | } 231 | } else { 232 | JOptionPane.showMessageDialog(ItemGUI.this, "Select an item to update."); 233 | } 234 | } 235 | } 236 | 237 | private class DeleteButtonListener implements ActionListener { 238 | @Override 239 | public void actionPerformed(ActionEvent e) { 240 | int selectedRow = itemTable.getSelectedRow(); 241 | if (selectedRow >= 0) { 242 | Item item = tableModel.getItemAt(selectedRow); 243 | int confirmation = JOptionPane.showConfirmDialog(ItemGUI.this, 244 | "Are you sure you want to delete this item?", 245 | "Confirm Delete", JOptionPane.YES_NO_OPTION); 246 | if (confirmation == JOptionPane.YES_OPTION) { 247 | if (itemDAO.deleteItem(item.getId())) { 248 | JOptionPane.showMessageDialog(ItemGUI.this, "Item deleted successfully."); 249 | loadItems(); 250 | clearFields(); 251 | } else { 252 | JOptionPane.showMessageDialog(ItemGUI.this, "Failed to delete item."); 253 | } 254 | } 255 | } else { 256 | JOptionPane.showMessageDialog(ItemGUI.this, "Select an item to delete."); 257 | } 258 | } 259 | } 260 | 261 | private class ClearButtonListner implements ActionListener{ 262 | 263 | @Override 264 | public void actionPerformed(ActionEvent e) { 265 | clearFields(); 266 | } 267 | } 268 | 269 | private void populateFields(Item item) { 270 | nameField.setText(item.getName()); 271 | descriptionField.setText(item.getDescription()); 272 | priceField.setText(item.getPrice().toString()); 273 | quantityField.setText(String.valueOf(item.getQuantity())); 274 | supplierIDField.setText(String.valueOf(item.getSupplierID())); 275 | } 276 | 277 | private void clearFields() { 278 | nameField.setText(""); 279 | descriptionField.setText(""); 280 | priceField.setText(""); 281 | quantityField.setText(""); 282 | supplierIDField.setText(""); 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /src/PurchaseGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.table.AbstractTableModel; 3 | import javax.swing.table.DefaultTableCellRenderer; 4 | import javax.swing.table.JTableHeader; 5 | import java.awt.*; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.math.BigDecimal; 9 | import java.sql.SQLException; 10 | import java.util.ArrayList; 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | public class PurchaseGUI extends JFrame { 15 | private PurchaseItemDAO purchaseItemDAO; 16 | private ItemDAO itemDAO; 17 | private JTextField buyerIDField; 18 | private JTextField itemIDField; 19 | private JTextField quantityField; 20 | private JTable purchaseTable; 21 | private JTable itemDetailsTable; 22 | private PurchaseItemTableModel tableModel; 23 | private ItemTableModel itemTableModel; 24 | private JLabel totalPendingLabel; // Label to show total pending amount 25 | 26 | public PurchaseGUI( int userId) { 27 | super("Purchasing"); 28 | setLayout(new BorderLayout()); 29 | purchaseItemDAO = new PurchaseItemDAO(); 30 | itemDAO = new ItemDAO(); 31 | 32 | // Create header panel 33 | JPanel headerPanel = new HeaderPanel(userId); 34 | add(headerPanel, BorderLayout.NORTH); 35 | 36 | JPanel inputPanel = new JPanel(); 37 | inputPanel.setLayout(new GridBagLayout()); 38 | inputPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Purchase Details")); 39 | GridBagConstraints gbc = new GridBagConstraints(); 40 | gbc.insets = new Insets(10, 10, 10, 10); 41 | gbc.fill = GridBagConstraints.HORIZONTAL; 42 | gbc.gridx = 0; 43 | gbc.gridy = 0; 44 | gbc.anchor = GridBagConstraints.WEST; 45 | 46 | JLabel buyerIDLabel = new JLabel("Buyer ID:"); 47 | buyerIDLabel.setFont(new Font("Arial", Font.BOLD, 14)); 48 | inputPanel.add(buyerIDLabel, gbc); 49 | 50 | buyerIDField = new JTextField(20); 51 | buyerIDField.setFont(new Font("Arial", Font.PLAIN, 14)); 52 | buyerIDField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 53 | gbc.gridx = 1; 54 | inputPanel.add(buyerIDField, gbc); 55 | 56 | gbc.gridx = 0; 57 | gbc.gridy = 1; 58 | JLabel itemIDLabel = new JLabel("Item ID:"); 59 | itemIDLabel.setFont(new Font("Arial", Font.BOLD, 14)); 60 | inputPanel.add(itemIDLabel, gbc); 61 | 62 | itemIDField = new JTextField(20); 63 | itemIDField.setFont(new Font("Arial", Font.PLAIN, 14)); 64 | itemIDField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 65 | gbc.gridx = 1; 66 | inputPanel.add(itemIDField, gbc); 67 | 68 | gbc.gridx = 0; 69 | gbc.gridy = 2; 70 | JLabel quantityLabel = new JLabel("Quantity:"); 71 | quantityLabel.setFont(new Font("Arial", Font.BOLD, 14)); 72 | inputPanel.add(quantityLabel, gbc); 73 | 74 | quantityField = new JTextField(20); 75 | quantityField.setFont(new Font("Arial", Font.PLAIN, 14)); 76 | quantityField.setBorder(BorderFactory.createLineBorder(new Color(0, 102, 204), 1)); 77 | gbc.gridx = 1; 78 | inputPanel.add(quantityField, gbc); 79 | 80 | JButton addButton = new JButton("Add Item"); 81 | addButton.setFont(new Font("Arial", Font.BOLD, 14)); 82 | addButton.setBackground(new Color(0, 102, 204)); 83 | addButton.setForeground(Color.WHITE); 84 | addButton.setFocusPainted(false); 85 | addButton.addActionListener(new AddButtonListener()); 86 | gbc.gridx = 0; 87 | gbc.gridy = 3; 88 | gbc.gridwidth = 2; 89 | gbc.anchor = GridBagConstraints.CENTER; 90 | inputPanel.add(addButton, gbc); 91 | 92 | JButton completeButton = new JButton("Complete Purchase"); 93 | completeButton.setFont(new Font("Arial", Font.BOLD, 14)); 94 | completeButton.setBackground(new Color(0, 153, 51)); 95 | completeButton.setForeground(Color.WHITE); 96 | completeButton.setFocusPainted(false); 97 | completeButton.addActionListener(new CompleteButtonListener()); 98 | gbc.gridy = 4; 99 | inputPanel.add(completeButton, gbc); 100 | 101 | totalPendingLabel = new JLabel("Total Pending Amount: $0.00"); // Initialize the label 102 | totalPendingLabel.setFont(new Font("Arial", Font.BOLD, 16)); 103 | totalPendingLabel.setForeground(new Color(0, 102, 204)); 104 | totalPendingLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); 105 | gbc.gridy = 5; 106 | inputPanel.add(totalPendingLabel, gbc); 107 | 108 | add(inputPanel, BorderLayout.CENTER); 109 | 110 | tableModel = new PurchaseItemTableModel(); 111 | purchaseTable = new JTable(tableModel); 112 | styleTable(purchaseTable); 113 | JScrollPane purchaseScrollPane = new JScrollPane(purchaseTable); 114 | purchaseScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Pending Purchases")); 115 | 116 | itemTableModel = new ItemTableModel(); 117 | itemDetailsTable = new JTable(itemTableModel); 118 | styleTable(itemDetailsTable); 119 | JScrollPane itemDetailsScrollPane = new JScrollPane(itemDetailsTable); 120 | itemDetailsScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY, 1), "Item Details")); 121 | 122 | JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, purchaseScrollPane, itemDetailsScrollPane); 123 | splitPane.setResizeWeight(0.7); // 70% width for the pending purchases table 124 | splitPane.setOneTouchExpandable(true); 125 | 126 | add(splitPane, BorderLayout.SOUTH); 127 | 128 | loadPendingItems(); 129 | loadAllItems(); // Load all items into the item details table 130 | updateTotalPending(); 131 | } 132 | 133 | private void styleTable(JTable table) { 134 | table.setFont(new Font("Arial", Font.PLAIN, 14)); 135 | table.setRowHeight(30); 136 | table.setGridColor(Color.GRAY); 137 | 138 | JTableHeader header = table.getTableHeader(); 139 | header.setFont(new Font("Arial", Font.BOLD, 14)); 140 | header.setBackground(new Color(0, 102, 204)); 141 | header.setForeground(Color.WHITE); 142 | header.setReorderingAllowed(false); 143 | 144 | DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); 145 | centerRenderer.setHorizontalAlignment(SwingConstants.CENTER); 146 | table.setDefaultRenderer(Object.class, centerRenderer); 147 | 148 | table.setSelectionBackground(new Color(0, 153, 255)); 149 | table.setSelectionForeground(Color.WHITE); 150 | } 151 | 152 | private void loadPendingItems() { 153 | List items = purchaseItemDAO.getPendingPurchaseItems(); 154 | tableModel.setItems(items); 155 | } 156 | 157 | private void loadAllItems() { 158 | List items = itemDAO.getAllItems(); 159 | itemTableModel.setItems(items); 160 | } 161 | 162 | private void updateTotalPending() { 163 | BigDecimal total = purchaseItemDAO.getPendingItemsTotal(); 164 | totalPendingLabel.setText("Total Pending Amount: $" + total.toString()); 165 | } 166 | 167 | private void refreshTable() { 168 | loadPendingItems(); 169 | } 170 | 171 | private class AddButtonListener implements ActionListener { 172 | @Override 173 | public void actionPerformed(ActionEvent e) { 174 | try { 175 | int buyerID = Integer.parseInt(buyerIDField.getText()); 176 | int itemID = Integer.parseInt(itemIDField.getText()); 177 | int quantity = Integer.parseInt(quantityField.getText()); 178 | 179 | Item item = itemDAO.getItemById(itemID); 180 | if (item != null) { 181 | // Check if the purchase quantity is less than or equal to the available quantity 182 | if (quantity <= item.getQuantity()) { 183 | BigDecimal totalPrice = item.getPrice().multiply(BigDecimal.valueOf(quantity)); 184 | PurchaseItem purchaseItem = new PurchaseItem( 185 | 0, // Auto-incremented by the database 186 | buyerID, 187 | itemID, 188 | new java.sql.Date(new Date().getTime()), 189 | quantity, 190 | totalPrice, 191 | "Pending" 192 | ); 193 | if (purchaseItemDAO.addPurchaseItem(purchaseItem)) { 194 | // Update the item quantity 195 | if (itemDAO.updateItemQuantity(itemID, item.getQuantity() - quantity)) { 196 | loadPendingItems(); 197 | 198 | itemIDField.setText(""); 199 | quantityField.setText(""); 200 | loadAllItems(); // Refresh the item details table 201 | updateTotalPending(); // Update total pending amount after adding 202 | } else { 203 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Failed to update item quantity.", "Error", JOptionPane.ERROR_MESSAGE); 204 | } 205 | } else { 206 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Failed to add purchase item.", "Error", JOptionPane.ERROR_MESSAGE); 207 | } 208 | } else { 209 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Purchase quantity exceeds available quantity.", "Error", JOptionPane.ERROR_MESSAGE); 210 | } 211 | } else { 212 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Item not found.", "Error", JOptionPane.ERROR_MESSAGE); 213 | } 214 | } catch (NumberFormatException ex) { 215 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Invalid input. Please enter numeric values.", "Error", JOptionPane.ERROR_MESSAGE); 216 | } catch (SQLException ex) { 217 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Database error occurred.", "Error", JOptionPane.ERROR_MESSAGE); 218 | ex.printStackTrace(); 219 | } 220 | } 221 | } 222 | 223 | 224 | private class CompleteButtonListener implements ActionListener { 225 | @Override 226 | public void actionPerformed(ActionEvent e) { 227 | int[] selectedRows = purchaseTable.getSelectedRows(); 228 | if (selectedRows.length > 0) { 229 | List purchaseIDs = new ArrayList<>(); 230 | for (int row : selectedRows) { 231 | int purchaseID = (int) purchaseTable.getValueAt(row, 0); 232 | purchaseIDs.add(purchaseID); 233 | } 234 | if (purchaseItemDAO.updatePurchaseStatus(purchaseIDs, "Completed")) { 235 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Purchase completed successfully."); 236 | refreshTable(); 237 | updateTotalPending(); // Update total pending amount after completion 238 | } else { 239 | JOptionPane.showMessageDialog(PurchaseGUI.this, "Failed to complete purchase.", "Error", JOptionPane.ERROR_MESSAGE); 240 | } 241 | } else { 242 | JOptionPane.showMessageDialog(PurchaseGUI.this, "No purchase selected.", "Warning", JOptionPane.WARNING_MESSAGE); 243 | } 244 | } 245 | } 246 | 247 | 248 | private class ItemTableModel extends AbstractTableModel { 249 | private final String[] columnNames = {"Item ID", "Item Name", "Price", "Quantity"}; 250 | private List items; 251 | 252 | public void setItems(List items) { 253 | this.items = items; 254 | fireTableDataChanged(); 255 | } 256 | 257 | @Override 258 | public int getRowCount() { 259 | return items == null ? 0 : items.size(); 260 | } 261 | 262 | @Override 263 | public int getColumnCount() { 264 | return columnNames.length; 265 | } 266 | 267 | @Override 268 | public Object getValueAt(int rowIndex, int columnIndex) { 269 | Item item = items.get(rowIndex); 270 | switch (columnIndex) { 271 | case 0: 272 | return item.getId(); 273 | case 1: 274 | return item.getName(); 275 | case 2: 276 | return item.getPrice(); 277 | case 3: 278 | return item.getQuantity(); 279 | default: 280 | return null; 281 | } 282 | } 283 | 284 | @Override 285 | public String getColumnName(int column) { 286 | return columnNames[column]; 287 | } 288 | } 289 | } 290 | --------------------------------------------------------------------------------