Advertisement
Kulas_Code20

Untitled

Oct 29th, 2021
84
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.taskperf6;
  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("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
  19.             while(retrieve.hasNext()){
  20.                 datafile.add(retrieve.next());
  21.             }
  22.             retrieve.close();
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.    
  28.     //This is the method for register
  29.     public void resgister() {
  30.         dataRetrieve();
  31.         try {
  32.             //Getting input from the user
  33.             System.out.print("Enter your username: ");
  34.             String username = input.next();
  35.             System.out.print("Enter your password: ");
  36.             String password =  input.next();
  37.             String data = username + "," + password;
  38.             datafile.add(data);
  39.             input.close();
  40.             PrintWriter writer = new PrintWriter(new FileWriter("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
  41.            
  42.             for(String passingData:datafile) {
  43.                 writer.println(passingData);
  44.             }
  45.             writer.close();
  46.            
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.        
  51.     }
  52.     //This is the method for register
  53.     public void login() {
  54.         Boolean found = false;
  55.         String tempUsername = "";
  56.         String tempPassword = "";
  57.         try {
  58.             Scanner reader = new Scanner(new File("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
  59.             System.out.println("Login\n");
  60.            
  61.             //reader.useDelimiter(",\n");
  62.             while(reader.hasNext()) {
  63.                 System.out.print("Username: ");
  64.                 String username = input.nextLine();
  65.                 System.out.print("Password: ");
  66.                 String password = input.nextLine();
  67.                 reader.useDelimiter(",");
  68.                 tempUsername = reader.nextLine();
  69.                 tempPassword = reader.nextLine();
  70.                 if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
  71.                     found = true;
  72.                     System.out.println("Successfully logged in");
  73.                     System.out.println("Hello " + username + " Welcome.");
  74.                     break;
  75.                 } else {
  76.                     System.out.println("Incorrect username or password.");
  77.                 }
  78. //              System.out.println(tempUsername.toString());
  79. //              System.out.println(tempPassword.toString());
  80.             }
  81.             input.close();
  82.             reader.close();
  83.         } catch (FileNotFoundException e) {
  84.             e.printStackTrace();
  85.         }  
  86.     }
  87.    
  88.     public static void main(String[] args) {
  89.         Scanner input = new Scanner(System.in);
  90.         Login_Register lr = new Login_Register();
  91.         System.out.println("[1] - Register\n[2] - Login\nEnter your choice: ");
  92.         int choice = input.nextInt();
  93.        
  94.         switch(choice){
  95.             case 1:
  96.                 lr.resgister();
  97.                 break;
  98.             case 2:
  99.                 lr.login();
  100.                 break;
  101.             default:
  102.             System.out.println("Invalid input!");
  103.         }
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement