Advertisement
Guest User

Untitled

a guest
May 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.98 KB | None | 0 0
  1. import Core.DatabaseConnection;
  2. import Domain.User;
  3. import UI.Administration;
  4. import UI.Booking;
  5. import UI.RoomService;
  6.  
  7. import javax.swing.*;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.FocusEvent;
  12. import java.awt.event.FocusListener;
  13. import java.io.IOException;
  14.  
  15. public class MainClass {
  16.     /** Frame objects and variables used by the frame **/
  17.     private static JFrame windowsFrame = new JFrame("Hotel Management System V0.1");
  18.     private static final int FRAME_WIDTH = 1200;
  19.     private static final int FRAME_HEIGHT = 800;
  20.     private static final int FRAME_X_ADJ = 16;
  21.     private static final int FRAME_Y_ADJ = 39;
  22.  
  23.     private static Booking booking = new Booking();
  24.     private static Administration administration = new Administration();
  25.     private static RoomService roomService = new RoomService();
  26.  
  27.     private static CardLayout cardLayout = new CardLayout();
  28.  
  29.  
  30.     private static JPanel userPanel = new JPanel(cardLayout);
  31.  
  32.     private static Login loginPanel  = new Login();
  33.  
  34.     private static JMenuBar jMenuBar = new JMenuBar();
  35.  
  36.     private static JMenuItem loginItem = getItem("login");
  37.     private static JMenuItem adminItem = getItem("administration");
  38.     private static JMenuItem bookingItem = getItem("booking");
  39.  
  40.  
  41.  
  42.  
  43.     public static void main(String[] args) throws IOException {
  44.         windowsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.         windowsFrame.setSize(FRAME_WIDTH + FRAME_X_ADJ, FRAME_HEIGHT + FRAME_Y_ADJ);
  46.  
  47.         jMenuBar.add(loginItem);
  48.         jMenuBar.add(adminItem);
  49.         jMenuBar.add(bookingItem);
  50.  
  51.  
  52.         loggedOut();
  53.  
  54.         windowsFrame.setJMenuBar(jMenuBar);
  55.         windowsFrame.setLayout(cardLayout);
  56.         userPanel.add(loginPanel,"login");
  57.         userPanel.add(booking, "booking");
  58.         userPanel.add(administration, "administration");
  59.         userPanel.add(roomService, "roomservice");
  60.         userPanel.add(booking, "booking");
  61.         userPanel.add(roomService, "roomservice");
  62.  
  63.         windowsFrame.add(userPanel);
  64.  
  65.  
  66.         windowsFrame.setVisible(true);
  67.     }
  68.  
  69.     public static void loggedOut () {
  70.         adminItem.setEnabled(false);
  71.         bookingItem.setEnabled(false);
  72.  
  73.     }
  74.  
  75.     public static void loggedIn() {
  76.         if (loginPanel.currentUser.getIsAdministrator()) {
  77.             adminItem.setEnabled(true);
  78.             bookingItem.setEnabled(true);
  79.         } else {
  80.             bookingItem.setEnabled(true);
  81.             adminItem.setEnabled(false);
  82.         }
  83.     }
  84.  
  85.  
  86.     private static JMenuItem getItem (String name) {
  87.         JMenuItem item = new JMenuItem(name);
  88.         item.addActionListener(new ActionListener() {
  89.             @Override
  90.             public void actionPerformed(ActionEvent e) {
  91.                 cardLayout.show(userPanel, name);
  92.             }
  93.         });
  94.  
  95.         return item;
  96.     }
  97. }
  98.  
  99.  
  100.  class Login extends JPanel{
  101.     /** Frame specifications needed **/
  102.     private final int FRAME_HEIGHT = 600 + 39;
  103.     private final int FRAME_WIDTH = 800 + 16;
  104.  
  105.     /** Panel specifications **/
  106.     private final int PANEL_WIDTH = 300;
  107.     private final int PANEL_HEIGHT = 90;
  108.  
  109.     /** Panel components **/
  110.     private JLabel username, password;
  111.     private JTextField enterUsername;
  112.     private JPasswordField enterPassword;
  113.     private JButton login;
  114.  
  115.     /** Functionality **/
  116.     private DatabaseConnection dbConnection = new DatabaseConnection();
  117.     public User currentUser = new User();
  118.  
  119.  
  120.     /** Event Listeners **/
  121.     private FocusListener textFieldListener = new FocusListener() {
  122.         @Override
  123.         public void focusGained(FocusEvent e) {}
  124.  
  125.         @Override
  126.         public void focusLost(FocusEvent e) {
  127.             enterUsername.setText(enterUsername.getText());
  128.             enterPassword.setText(enterPassword.getText());
  129.         }
  130.     };
  131.  
  132.     private ActionListener loginListener = new ActionListener() {
  133.         @Override
  134.         public void actionPerformed(ActionEvent e) {
  135.             if (dbConnection.checkAdmin(enterUsername.getText(), enterPassword.getText())) {
  136.                 currentUser.setAdministrator(true);
  137.             } else {
  138.                 currentUser.setAdministrator(false);
  139.             }
  140.             if (dbConnection.checkLogin(enterUsername.getText(), enterPassword.getText())) {
  141.                 currentUser.setLoggedIn(true);
  142.                 MainClass.loggedIn();
  143.             }
  144.             if (!currentUser.getIsLoggedIn()) {
  145.                 JOptionPane.showMessageDialog(null, "You didn't type username/password correctly!");
  146.             }
  147.         }
  148.     };
  149.  
  150.  
  151.  
  152.  
  153.     /** Constructor **/
  154.     public Login () {
  155.         setLocation((FRAME_WIDTH / 2) - (PANEL_WIDTH / 2), (FRAME_HEIGHT / 2) - (PANEL_HEIGHT / 2));
  156.         setSize(PANEL_WIDTH, PANEL_HEIGHT);
  157.         setLayout(null);
  158.         setBorder(BorderFactory.createLineBorder(Color.BLACK));
  159.  
  160.         initializeComponent();
  161.  
  162.         setVisible(true);
  163.     }
  164.  
  165.     /** Initialize buttons etc **/
  166.     private void initializeComponent() {
  167.         // The creation of the J(ObjectName)'s
  168.         enterUsername = new JTextField();
  169.         enterPassword = new JPasswordField();
  170.         login = new JButton("Login");
  171.         username = new JLabel("Username");
  172.         password = new JLabel("Password");
  173.  
  174.         // Size and bounds of the objects
  175.         enterUsername.setBounds(90, 20, 100, 25);
  176.         enterPassword.setBounds(90, 45, 100, 25);
  177.         login.setBounds(200, 20, 75, 50);
  178.         username.setBounds(20, 20, 100, 25);
  179.         password.setBounds(20, 45, 100, 25);
  180.  
  181.         // Add action listeners to the buttons and textfields
  182.         enterUsername.addFocusListener(textFieldListener);
  183.         enterPassword.addFocusListener(textFieldListener);
  184.         login.addActionListener(loginListener);
  185.  
  186.         // Add buttons to panel
  187.         add(enterUsername);
  188.         add(enterPassword);
  189.         add(login);
  190.         add(username);
  191.         add(password);
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement