FinalProject.Employee_Login.java Source code

Java tutorial

Introduction

Here is the source code for FinalProject.Employee_Login.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package FinalProject;

/**
 *
 * @author Daanyaal
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Arrays;
import java.util.Scanner;

import org.apache.commons.io.FileUtils;

public class Employee_Login extends javax.swing.JFrame {

    /**
     * Creates new form Employee_Login
     */

    public Employee_Login() {
        initComponents();
        setLocationRelativeTo(this);
        Error_Message.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Employee_Input = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Password_Input = new javax.swing.JPasswordField();
        Back_Button = new javax.swing.JButton();
        Submit_Button = new javax.swing.JButton();
        Error_Message = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("KG Blank Space Solid", 0, 12)); // NOI18N
        jLabel1.setText("Employee ID:");

        Employee_Input.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }

            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                Employee_InputInputMethodTextChanged(evt);
            }
        });
        Employee_Input.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Employee_InputActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("KG Blank Space Solid", 0, 12)); // NOI18N
        jLabel2.setText("Password:");

        Password_Input.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Password_InputActionPerformed(evt);
            }
        });

        Back_Button.setFont(new java.awt.Font("KG Blank Space Solid", 0, 12)); // NOI18N
        Back_Button.setText("Back");
        Back_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Back_ButtonActionPerformed(evt);
            }
        });

        Submit_Button.setFont(new java.awt.Font("KG Blank Space Solid", 0, 12)); // NOI18N
        Submit_Button.setText("Submit");
        Submit_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Submit_ButtonActionPerformed(evt);
            }
        });

        Error_Message.setForeground(new java.awt.Color(255, 0, 51));
        Error_Message.setText("Incorrect ID and/ or Password");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(Back_Button)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Submit_Button).addGap(35, 35, 35))
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(24, 24, 24).addComponent(Error_Message))
                        .addGroup(layout.createSequentialGroup().addGap(36, 36, 36).addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel2).addComponent(jLabel1)
                                .addComponent(Employee_Input, javax.swing.GroupLayout.DEFAULT_SIZE, 140,
                                        Short.MAX_VALUE)
                                .addComponent(Password_Input, javax.swing.GroupLayout.DEFAULT_SIZE, 140,
                                        Short.MAX_VALUE))))
                        .addContainerGap(24, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(24, 24, 24).addComponent(jLabel1).addGap(0, 0, 0)
                        .addComponent(Employee_Input, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(jLabel2).addGap(0, 0, 0)
                        .addComponent(Password_Input, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Error_Message, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Back_Button).addComponent(Submit_Button))
                        .addGap(18, 18, 18)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Password_InputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Password_InputActionPerformed

    }//GEN-LAST:event_Password_InputActionPerformed

    private void Submit_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Submit_ButtonActionPerformed
        String Password = Arrays.toString(Password_Input.getPassword());
        String password = Password.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\, ", "");
        String username = Employee_Input.getText();

        try {
            File user = new File("Username.txt");
            File pass = new File("Password.txt");
            Scanner scanner = new Scanner(user);
            FileReader frU = new FileReader(user);
            LineNumberReader u = new LineNumberReader(frU);
            FileReader frP = new FileReader(pass);
            LineNumberReader p = new LineNumberReader(frP);

            int linenumberU = 0;
            while (scanner.hasNextLine() && u.readLine() != null) {
                linenumberU++;
                String lineFromFile = scanner.nextLine();

                if (lineFromFile.contains(username)) // a match!
                {
                    break;
                }
            }

            String pssLine = (String) FileUtils.readLines(pass).get(linenumberU - 1);
            String usrLine = (String) FileUtils.readLines(user).get(linenumberU - 1);

            if (username.equals(usrLine) && password.equals(pssLine)) {
                this.setVisible(false);
                Employee_Interface f = new Employee_Interface();
                f.setVisible(true);
                f.ID_Number.setText(usrLine + "!");
            }

            else {
                Error_Message.setVisible(true);
            }

        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_Submit_ButtonActionPerformed

    private void Employee_InputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Employee_InputActionPerformed

    }//GEN-LAST:event_Employee_InputActionPerformed

    private void Back_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Back_ButtonActionPerformed
        this.setVisible(false);
        Home_Interface f = new Home_Interface();
        f.setVisible(true);
    }//GEN-LAST:event_Back_ButtonActionPerformed

    private void Employee_InputInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_Employee_InputInputMethodTextChanged

    }//GEN-LAST:event_Employee_InputInputMethodTextChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Employee_Login.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Employee_Login.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Employee_Login.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Employee_Login.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Employee_Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back_Button;
    private javax.swing.JTextField Employee_Input;
    private javax.swing.JLabel Error_Message;
    private javax.swing.JPasswordField Password_Input;
    private javax.swing.JButton Submit_Button;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}