Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Core.DatabaseConnection;
- import Domain.User;
- import UI.Administration;
- import UI.Booking;
- import UI.RoomService;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.io.IOException;
- public class MainClass {
- /** Frame objects and variables used by the frame **/
- private static JFrame windowsFrame = new JFrame("Hotel Management System V0.1");
- private static final int FRAME_WIDTH = 1200;
- private static final int FRAME_HEIGHT = 800;
- private static final int FRAME_X_ADJ = 16;
- private static final int FRAME_Y_ADJ = 39;
- private static Booking booking = new Booking();
- private static Administration administration = new Administration();
- private static RoomService roomService = new RoomService();
- private static CardLayout cardLayout = new CardLayout();
- private static JPanel userPanel = new JPanel(cardLayout);
- private static Login loginPanel = new Login();
- private static JMenuBar jMenuBar = new JMenuBar();
- private static JMenuItem loginItem = getItem("login");
- private static JMenuItem adminItem = getItem("administration");
- private static JMenuItem bookingItem = getItem("booking");
- public static void main(String[] args) throws IOException {
- windowsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- windowsFrame.setSize(FRAME_WIDTH + FRAME_X_ADJ, FRAME_HEIGHT + FRAME_Y_ADJ);
- jMenuBar.add(loginItem);
- jMenuBar.add(adminItem);
- jMenuBar.add(bookingItem);
- loggedOut();
- windowsFrame.setJMenuBar(jMenuBar);
- windowsFrame.setLayout(cardLayout);
- userPanel.add(loginPanel,"login");
- userPanel.add(booking, "booking");
- userPanel.add(administration, "administration");
- userPanel.add(roomService, "roomservice");
- userPanel.add(booking, "booking");
- userPanel.add(roomService, "roomservice");
- windowsFrame.add(userPanel);
- windowsFrame.setVisible(true);
- }
- public static void loggedOut () {
- adminItem.setEnabled(false);
- bookingItem.setEnabled(false);
- }
- public static void loggedIn() {
- if (loginPanel.currentUser.getIsAdministrator()) {
- adminItem.setEnabled(true);
- bookingItem.setEnabled(true);
- } else {
- bookingItem.setEnabled(true);
- adminItem.setEnabled(false);
- }
- }
- private static JMenuItem getItem (String name) {
- JMenuItem item = new JMenuItem(name);
- item.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- cardLayout.show(userPanel, name);
- }
- });
- return item;
- }
- }
- class Login extends JPanel{
- /** Frame specifications needed **/
- private final int FRAME_HEIGHT = 600 + 39;
- private final int FRAME_WIDTH = 800 + 16;
- /** Panel specifications **/
- private final int PANEL_WIDTH = 300;
- private final int PANEL_HEIGHT = 90;
- /** Panel components **/
- private JLabel username, password;
- private JTextField enterUsername;
- private JPasswordField enterPassword;
- private JButton login;
- /** Functionality **/
- private DatabaseConnection dbConnection = new DatabaseConnection();
- public User currentUser = new User();
- /** Event Listeners **/
- private FocusListener textFieldListener = new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {}
- @Override
- public void focusLost(FocusEvent e) {
- enterUsername.setText(enterUsername.getText());
- enterPassword.setText(enterPassword.getText());
- }
- };
- private ActionListener loginListener = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (dbConnection.checkAdmin(enterUsername.getText(), enterPassword.getText())) {
- currentUser.setAdministrator(true);
- } else {
- currentUser.setAdministrator(false);
- }
- if (dbConnection.checkLogin(enterUsername.getText(), enterPassword.getText())) {
- currentUser.setLoggedIn(true);
- MainClass.loggedIn();
- }
- if (!currentUser.getIsLoggedIn()) {
- JOptionPane.showMessageDialog(null, "You didn't type username/password correctly!");
- }
- }
- };
- /** Constructor **/
- public Login () {
- setLocation((FRAME_WIDTH / 2) - (PANEL_WIDTH / 2), (FRAME_HEIGHT / 2) - (PANEL_HEIGHT / 2));
- setSize(PANEL_WIDTH, PANEL_HEIGHT);
- setLayout(null);
- setBorder(BorderFactory.createLineBorder(Color.BLACK));
- initializeComponent();
- setVisible(true);
- }
- /** Initialize buttons etc **/
- private void initializeComponent() {
- // The creation of the J(ObjectName)'s
- enterUsername = new JTextField();
- enterPassword = new JPasswordField();
- login = new JButton("Login");
- username = new JLabel("Username");
- password = new JLabel("Password");
- // Size and bounds of the objects
- enterUsername.setBounds(90, 20, 100, 25);
- enterPassword.setBounds(90, 45, 100, 25);
- login.setBounds(200, 20, 75, 50);
- username.setBounds(20, 20, 100, 25);
- password.setBounds(20, 45, 100, 25);
- // Add action listeners to the buttons and textfields
- enterUsername.addFocusListener(textFieldListener);
- enterPassword.addFocusListener(textFieldListener);
- login.addActionListener(loginListener);
- // Add buttons to panel
- add(enterUsername);
- add(enterPassword);
- add(login);
- add(username);
- add(password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement