Advertisement
Kulas_Code20

Untitled

Oct 30th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package com.files;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10.  
  11. public class Login_Register {
  12.  
  13.     Scanner input = new Scanner(System.in);
  14.     ArrayList<String> datafile = new ArrayList<String>();
  15.  
  16.     public void dataRetrieve() {
  17.         try {
  18.             Scanner retrieve = new Scanner(new File(
  19.                     "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
  20.             while (retrieve.hasNext()) {
  21.                 datafile.add(retrieve.next());
  22.             }
  23.             retrieve.close();
  24.         } catch (FileNotFoundException e) {
  25.             e.printStackTrace();
  26.         }
  27.     }
  28.  
  29.     // This is the method for register
  30.     public void resgister() {
  31.         dataRetrieve();
  32.         try {
  33.             // Getting input from the user
  34.             System.out.print("Enter your username: ");
  35.             String username = input.nextLine();
  36.             System.out.print("Enter your password: ");
  37.             String password = input.nextLine();
  38.             // String data = username + " " + password;
  39.             datafile.add(username);
  40.             datafile.add(password);
  41.             input.close();
  42.             PrintWriter writer = new PrintWriter(new FileWriter(
  43.                     "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
  44.  
  45.             for (String passingData : datafile) {
  46.                 writer.println(passingData);
  47.             }
  48.             writer.close();
  49.  
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.  
  54.     }
  55.  
  56.     // This is the method for register
  57.     public void login() {
  58.         Boolean found = false;
  59.         String tempUsername = "";
  60.         String tempPassword = "";
  61.         int attempt = 5;
  62.         try {
  63.  
  64.             Scanner reader = new Scanner(new File(
  65.                     "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
  66.             System.out.println("Login\n");
  67.             while (reader.hasNext() && !found) {
  68.                 System.out.print("Username: ");
  69.                 String username = input.nextLine();
  70.                 System.out.print("Password: ");
  71.                 String password = input.nextLine();
  72.  
  73.                 tempUsername = reader.next();
  74.                 tempPassword = reader.next();
  75.                 if (tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
  76.                     System.out.println("\n--------------------------------");
  77.                     System.out.println("\nSuccessfully logged in");
  78.                     System.out.println("Hello " + username + " Welcome.");
  79.                     found = true;
  80.                     break;
  81.                 }else {
  82.                     System.out.println("Incorrect username or password");
  83.                 }
  84.             }
  85.             input.close();
  86.             reader.close();
  87.         } catch (FileNotFoundException e) {
  88.             e.printStackTrace();
  89.         }
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.         Scanner input = new Scanner(System.in);
  94.         Login_Register lr = new Login_Register();
  95.         System.out.println("[1] - Register\n[2] - Login\nEnter your choice: ");
  96.         int choice = input.nextInt();
  97.  
  98.         switch (choice) {
  99.         case 1:
  100.             lr.resgister();
  101.             break;
  102.         case 2:
  103.             lr.login();
  104.             break;
  105.         default:
  106.             System.out.println("Invalid input!");
  107.         }
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement