Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 ScheduleManager.pkg;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- /**
- *
- * @author hanaezz
- */
- public class logIn extends javax.swing.JFrame {
- File file;
- /**
- * Creates new form logIn
- */
- public logIn() throws IOException {
- String path="//Users//hanaezz//NetBeansProjects//ScheduleManager//src//ScheduleManager//pkg//info.txt";
- file = new File(path); //creates new File
- if (file.exists()) {
- initComponents();
- } else { //make a new file if it's not already existent
- createUserPass createInfo = new createUserPass();
- createInfo.setVisible(true);
- createInfo.setAlwaysOnTop(true);
- this.dispose();
- this.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">
- private void initComponents() {
- passwordField = new javax.swing.JPasswordField();
- usernameField = new javax.swing.JTextField();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- logInButton = new javax.swing.JButton();
- quitButton = new javax.swing.JButton();
- image = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Log-In");
- setBackground(new java.awt.Color(255, 255, 255));
- jLabel2.setText("Username");
- jLabel3.setText("Password");
- logInButton.setText("log in");
- logInButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- logInButtonActionPerformed(evt);
- }
- });
- quitButton.setText("quit");
- image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ScheduleManager/pkg/King_Abdulaziz_University_(emblem).jpg"))); // NOI18N
- 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(logInButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(quitButton)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
- .addComponent(passwordField))
- .addGap(17, 17, 17))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(62, Short.MAX_VALUE)
- .addComponent(image)
- .addGap(51, 51, 51))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(image)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel2))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel3))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(quitButton)
- .addComponent(logInButton))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void logInButtonActionPerformed(java.awt.event.ActionEvent evt) {
- int loginAttempts = 0;
- String enteredUsername = usernameField.getText();
- String enteredPassword = passwordField.getText();
- String encryptedEnteredUsername = encrypt(enteredUsername);
- String encryptedEnteredPassword = encrypt(enteredPassword);
- String truePassword = "";
- String trueUsername = "";
- String securityAnswer = "";
- Scanner sc;
- try {
- sc = new Scanner(new File("\\src\\Schedulemanager\\pkg\\info.txt"));
- trueUsername = sc.nextLine();
- truePassword = sc.nextLine();
- securityAnswer = sc.nextLine();
- sc.close();
- } catch (FileNotFoundException ex) {
- Logger.getLogger(logIn.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("the boolean is " + encryptedEnteredUsername.equals(trueUsername));
- if ((encryptedEnteredUsername.equals(trueUsername))
- && (encryptedEnteredPassword.equals(truePassword))) {
- MainScreen main = new MainScreen();
- main.setVisible(true);
- main.setAlwaysOnTop(true);
- this.setVisible(false);
- this.dispose();
- } else if (loginAttempts < 5) {
- String errorMessage = "Incorrect User/Password Combination. Attempt "
- + loginAttempts + " of 5.";
- JOptionPane.showMessageDialog(null,
- errorMessage,
- "ERROR",
- JOptionPane.WARNING_MESSAGE);
- } else if (loginAttempts == 5) {
- String errorMessage = "Security Question: What was your favourite class in college?";
- String s = (String) JTextField.showInputDialog(
- null,
- errorMessage,
- "ERROR",
- JOptionPane.PLAIN_MESSAGE,
- JOptionPane.WARNING_MESSAGE,
- null,
- securityAnswer);
- if ((s != null) && (s.length() > 0)) { //if something was entered returned
- String encryptedAnswer = encrypt(s);
- if(encryptedAnswer.equals(securityAnswer)){
- return;
- }
- } else {
- String quitMessage = "Incorrect. Exiting.";
- JOptionPane.showMessageDialog(null,
- errorMessage,
- "ERROR",
- JOptionPane.WARNING_MESSAGE);
- }
- }
- }
- /**
- * @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(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(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() {
- try{
- new logIn().setVisible(true);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- });
- }
- public static String encrypt(String x) { //caesar cipher shifts letters by 3 places
- char[] message = x.toCharArray();
- for (int i = 0; i < message.length; i++) {
- char character = message[i];
- character = (char) (character + 3);
- if (character > 'z') {
- character = (char) (character - 26);
- } else if (character < 'a') {
- character = (char) (character + 26);
- }
- message[i] = character;
- }
- String output = new String(message);
- System.out.println("from " + x + " to " + output);
- return output;
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel image;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JButton logInButton;
- private javax.swing.JPasswordField passwordField;
- private javax.swing.JButton quitButton;
- private javax.swing.JTextField usernameField;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment