Guest User

logIn

a guest
Oct 21st, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.40 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ScheduleManager.pkg;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.util.Scanner;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JTextField;
  16.  
  17. /**
  18.  *
  19.  * @author hanaezz
  20.  */
  21. public class logIn extends javax.swing.JFrame {
  22.  
  23.     File file;
  24.    
  25.     /**
  26.      * Creates new form logIn
  27.      */
  28.     public logIn() throws IOException {
  29.         String path="//Users//hanaezz//NetBeansProjects//ScheduleManager//src//ScheduleManager//pkg//info.txt";
  30.              file = new File(path); //creates new File
  31.                 if (file.exists()) {
  32.                     initComponents();
  33.                 } else { //make a new file if it's not already existent
  34.                     createUserPass createInfo = new createUserPass();
  35.                     createInfo.setVisible(true);
  36.                     createInfo.setAlwaysOnTop(true);
  37.                     this.dispose();
  38.                     this.setVisible(false);
  39.                 }
  40.     }
  41.  
  42.     /**
  43.      * This method is called from within the constructor to initialize the form.
  44.      * WARNING: Do NOT modify this code. The content of this method is always
  45.      * regenerated by the Form Editor.
  46.      */
  47.     @SuppressWarnings("unchecked")
  48.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  49.     private void initComponents() {
  50.  
  51.         passwordField = new javax.swing.JPasswordField();
  52.         usernameField = new javax.swing.JTextField();
  53.         jLabel2 = new javax.swing.JLabel();
  54.         jLabel3 = new javax.swing.JLabel();
  55.         logInButton = new javax.swing.JButton();
  56.         quitButton = new javax.swing.JButton();
  57.         image = new javax.swing.JLabel();
  58.  
  59.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  60.         setTitle("Log-In");
  61.         setBackground(new java.awt.Color(255, 255, 255));
  62.  
  63.         jLabel2.setText("Username");
  64.  
  65.         jLabel3.setText("Password");
  66.  
  67.         logInButton.setText("log in");
  68.         logInButton.addActionListener(new java.awt.event.ActionListener() {
  69.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  70.                 logInButtonActionPerformed(evt);
  71.             }
  72.         });
  73.  
  74.         quitButton.setText("quit");
  75.  
  76.         image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ScheduleManager/pkg/King_Abdulaziz_University_(emblem).jpg"))); // NOI18N
  77.  
  78.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  79.         getContentPane().setLayout(layout);
  80.         layout.setHorizontalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  83.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  84.                 .addComponent(logInButton)
  85.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  86.                 .addComponent(quitButton)
  87.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  88.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  89.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  90.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91.                     .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
  92.                     .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
  93.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  95.                     .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
  96.                     .addComponent(passwordField))
  97.                 .addGap(17, 17, 17))
  98.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  99.                 .addContainerGap(62, Short.MAX_VALUE)
  100.                 .addComponent(image)
  101.                 .addGap(51, 51, 51))
  102.         );
  103.         layout.setVerticalGroup(
  104.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  105.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  106.                 .addComponent(image)
  107.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  108.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  109.                     .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  110.                     .addComponent(jLabel2))
  111.                 .addGap(18, 18, 18)
  112.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  113.                     .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  114.                     .addComponent(jLabel3))
  115.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  116.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  117.                     .addComponent(quitButton)
  118.                     .addComponent(logInButton))
  119.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  120.         );
  121.  
  122.         pack();
  123.     }// </editor-fold>                        
  124.  
  125.     private void logInButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  126.         int loginAttempts = 0;
  127.         String enteredUsername = usernameField.getText();
  128.         String enteredPassword = passwordField.getText();
  129.         String encryptedEnteredUsername = encrypt(enteredUsername);
  130.         String encryptedEnteredPassword = encrypt(enteredPassword);
  131.         String truePassword = "";
  132.         String trueUsername = "";
  133.         String securityAnswer = "";
  134.         Scanner sc;
  135.         try {
  136.             sc = new Scanner(new File("\\src\\Schedulemanager\\pkg\\info.txt"));
  137.             trueUsername = sc.nextLine();
  138.             truePassword = sc.nextLine();
  139.             securityAnswer = sc.nextLine();
  140.             sc.close();
  141.         } catch (FileNotFoundException ex) {
  142.             Logger.getLogger(logIn.class.getName()).log(Level.SEVERE, null, ex);
  143.         }
  144.         System.out.println("the boolean is " + encryptedEnteredUsername.equals(trueUsername));
  145.         if ((encryptedEnteredUsername.equals(trueUsername))
  146.                 && (encryptedEnteredPassword.equals(truePassword))) {
  147.             MainScreen main = new MainScreen();
  148.             main.setVisible(true);
  149.             main.setAlwaysOnTop(true);
  150.             this.setVisible(false);
  151.             this.dispose();
  152.         } else if (loginAttempts < 5) {
  153.             String errorMessage = "Incorrect User/Password Combination. Attempt "
  154.                     + loginAttempts + " of 5.";
  155.             JOptionPane.showMessageDialog(null,
  156.                     errorMessage,
  157.                     "ERROR",
  158.                     JOptionPane.WARNING_MESSAGE);
  159.         } else if (loginAttempts == 5) {
  160.             String errorMessage = "Security Question: What was your favourite class in college?";
  161.             String s = (String) JTextField.showInputDialog(
  162.                     null,
  163.                     errorMessage,
  164.                     "ERROR",
  165.                     JOptionPane.PLAIN_MESSAGE,
  166.                     JOptionPane.WARNING_MESSAGE,
  167.                     null,
  168.                     securityAnswer);
  169.  
  170.             if ((s != null) && (s.length() > 0)) { //if something was entered returned
  171.                 String encryptedAnswer = encrypt(s);
  172.                 if(encryptedAnswer.equals(securityAnswer)){
  173.                 return;
  174.                 }
  175.             } else {
  176.                 String quitMessage = "Incorrect. Exiting.";
  177.             JOptionPane.showMessageDialog(null,
  178.                     errorMessage,
  179.                     "ERROR",
  180.                     JOptionPane.WARNING_MESSAGE);
  181.             }
  182.         }
  183.     }                                          
  184.  
  185.     /**
  186.      * @param args the command line arguments
  187.      */
  188.     public static void main(String args[]) {
  189.         /* Set the Nimbus look and feel */
  190.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  191.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  192.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  193.          */
  194.         try {
  195.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  196.                 if ("Nimbus".equals(info.getName())) {
  197.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  198.                     break;
  199.                 }
  200.             }
  201.         } catch (ClassNotFoundException ex) {
  202.             java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203.         } catch (InstantiationException ex) {
  204.             java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  205.         } catch (IllegalAccessException ex) {
  206.             java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  207.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  208.             java.util.logging.Logger.getLogger(logIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  209.         }
  210.         //</editor-fold>
  211.  
  212.         /* Create and display the form */
  213.         java.awt.EventQueue.invokeLater(new Runnable() {
  214.             public void run() {
  215.                 try{
  216.                 new logIn().setVisible(true);
  217.                 }
  218.                 catch(Exception e){
  219.                     e.printStackTrace();
  220.                 }
  221.             }
  222.         });
  223.     }
  224.  
  225.     public static String encrypt(String x) { //caesar cipher shifts letters by 3 places
  226.         char[] message = x.toCharArray();
  227.         for (int i = 0; i < message.length; i++) {
  228.             char character = message[i];
  229.             character = (char) (character + 3);
  230.             if (character > 'z') {
  231.                 character = (char) (character - 26);
  232.             } else if (character < 'a') {
  233.                 character = (char) (character + 26);
  234.             }
  235.             message[i] = character;
  236.         }
  237.         String output = new String(message);
  238.         System.out.println("from " + x + " to " + output);
  239.         return output;
  240.     }
  241.  
  242.     // Variables declaration - do not modify                    
  243.     private javax.swing.JLabel image;
  244.     private javax.swing.JLabel jLabel2;
  245.     private javax.swing.JLabel jLabel3;
  246.     private javax.swing.JButton logInButton;
  247.     private javax.swing.JPasswordField passwordField;
  248.     private javax.swing.JButton quitButton;
  249.     private javax.swing.JTextField usernameField;
  250.     // End of variables declaration                  
  251. }
Add Comment
Please, Sign In to add comment