Instructions
∙ Name your Java package as: “com.lastname.duka”.
∙ Name your database using your names as: “db_first_last_studentNumber”. ∙ Name all your tables in your database using this format: “tbl_tableName”. ∙ Add a copyright message (tiny text) on all your GUIs.
Milestone:
For this unit, you are required to develop a GUI application using Java. The application should connect to a database and should be able to perform any CRUD operation.
Main goal:
Develop a Java application with GUI for a primary school called Duka.
Functional Requirements:
The application should allow:
i. A shop attendant to be able to login to upload stock of items (with prices). ii. A buyer be able to search for items, get their prices and make an order.
Non-functional Requirements:
The application should:
i. Have a user-friendly GUI for both shop attendant and buyer.
ii. Not crash in case a user makes a mistake, instead it should provide a clarification/warning.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class Main {
public static final String LOGIN = "admin";
public static final String PASSWORD = "admin";
public static void createAdministrationFrame(JFrame owner) {
JDialog adminFrame = new JDialog(owner);
adminFrame.setName("Admin");
DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setMaximumSize(new Dimension(200,400));
JButton addButton = new JButton("Add");
JButton updateButton = new JButton("Update");
JButton deleteButton = new JButton("Delete");
JPanel buttonsPanel = new JPanel();
BoxLayout buttonsLayout = new BoxLayout(buttonsPanel, BoxLayout.X_AXIS);
buttonsPanel.setLayout(buttonsLayout);
buttonsPanel.add(addButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonsPanel.add(updateButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonsPanel.add(deleteButton);
JLabel idLabel = new JLabel("ID");
idLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
idLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField idTextField = new JTextField();
idTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
idTextField.setMaximumSize(new Dimension(120, 20));
JPanel idPanel = new JPanel();
BoxLayout idLayout = new BoxLayout(idPanel, BoxLayout.Y_AXIS);
idPanel.setLayout(idLayout);
idPanel.add(idLabel);
idPanel.add(idTextField);
JLabel nameLabel = new JLabel("Name");
nameLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
nameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField nameTextField = new JTextField();
nameTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
nameTextField.setMaximumSize(new Dimension(120, 20));
JPanel namePanel = new JPanel();
BoxLayout nameLayout = new BoxLayout(namePanel, BoxLayout.Y_AXIS);
namePanel.setLayout(nameLayout);
namePanel.add(nameLabel);
namePanel.add(nameTextField);
JLabel priceLabel = new JLabel("Price");
priceLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
priceLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField priceTextField = new JTextField();
priceTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
priceTextField.setMaximumSize(new Dimension(120, 20));
JPanel pricePanel = new JPanel();
BoxLayout priceLayout = new BoxLayout(pricePanel, BoxLayout.Y_AXIS);
pricePanel.setLayout(priceLayout);
pricePanel.add(priceLabel);
pricePanel.add(priceTextField);
JPanel inputPanel = new JPanel();
BoxLayout inputLayout = new BoxLayout(inputPanel, BoxLayout.X_AXIS);
inputPanel.setLayout(inputLayout);
//inputPanel.add(Box.createHorizontalGlue());
inputPanel.add(idPanel);
inputPanel.add(Box.createRigidArea(new Dimension(10, 0)));
inputPanel.add(namePanel);
inputPanel.add(Box.createRigidArea(new Dimension(10, 0)));
inputPanel.add(pricePanel);
//inputPanel.add(Box.createHorizontalGlue());
JPanel mainPanel = new JPanel();
BoxLayout mainLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(mainLayout);
mainPanel.add(Box.createVerticalGlue());
mainPanel.add(scrollPane);
mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
mainPanel.add(inputPanel);
mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
mainPanel.add(buttonsPanel);
mainPanel.add(Box.createVerticalGlue());
adminFrame.add(mainPanel);
adminFrame.setSize(450, 650);
adminFrame.setModal(true);
adminFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
adminFrame.setVisible(true);
}
public static void createBuyerFrame() {
JDialog buyerFrame = new JDialog();
JLabel buyer = new JLabel("Buyer");
JPanel panel = new JPanel();
panel.add(buyer);
buyerFrame.add(panel);
buyerFrame.setSize(200, 200);
buyerFrame.setModal(true);
buyerFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
buyerFrame.setVisible(true);
}
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Duka");
JLabel loginLabel = new JLabel("Login");
loginLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
loginLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField loginTextField = new JTextField();
loginTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
loginTextField.setMaximumSize(new Dimension(120, 20));
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
passwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField passwordTextField = new JTextField();
passwordTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
passwordTextField.setMaximumSize(new Dimension(120, 20));
JButton loginButton = new JButton("Login");
loginButton.addActionListener(e -> {
if (loginTextField.getText().equals(LOGIN) && passwordTextField.getText().equals(PASSWORD)) {
loginTextField.setText("");
passwordTextField.setText("");
createAdministrationFrame(mainFrame);
}
});
JButton buyButton = new JButton("Buy");
buyButton.addActionListener(e -> {
});
JPanel panel = new JPanel();
BoxLayout mainLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
JPanel buttonsPanel = new JPanel();
BoxLayout buttonsLayout = new BoxLayout(buttonsPanel, BoxLayout.X_AXIS);
buttonsPanel.setLayout(buttonsLayout);
buttonsPanel.add(loginButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonsPanel.add(buyButton);
panel.setLayout(mainLayout);
panel.add(Box.createVerticalGlue());
panel.add(loginLabel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(loginTextField);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(passwordLabel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(passwordTextField);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(buttonsPanel);
panel.add(Box.createVerticalGlue());
mainFrame.add(panel);
mainFrame.setSize(250, 350);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Comments
Leave a comment