Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.taskperf6;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Login_Register {
- Scanner input = new Scanner(System.in);
- ArrayList<String> datafile = new ArrayList<String>();
- public void dataRetrieve() {
- try {
- Scanner retrieve = new Scanner(new File("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
- while(retrieve.hasNext()){
- datafile.add(retrieve.next());
- }
- retrieve.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- //This is the method for register
- public void resgister() {
- dataRetrieve();
- try {
- //Getting input from the user
- System.out.print("Enter your username: ");
- String username = input.next();
- System.out.print("Enter your password: ");
- String password = input.next();
- String data = username + "," + password;
- datafile.add(data);
- input.close();
- PrintWriter writer = new PrintWriter(new FileWriter("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
- for(String passingData:datafile) {
- writer.println(passingData);
- }
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //This is the method for register
- public void login() {
- Boolean found = false;
- String tempUsername = "";
- String tempPassword = "";
- try {
- Scanner reader = new Scanner(new File("C:\\Users\\Fujitsu\\Documents\\Java Files\\JAVA OOP\\src\\com\\taskperf6\\records.txt"));
- System.out.println("Login\n");
- //reader.useDelimiter(",\n");
- while(reader.hasNext()) {
- System.out.print("Username: ");
- String username = input.nextLine();
- System.out.print("Password: ");
- String password = input.nextLine();
- reader.useDelimiter(",");
- tempUsername = reader.nextLine();
- tempPassword = reader.nextLine();
- if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
- found = true;
- System.out.println("Successfully logged in");
- System.out.println("Hello " + username + " Welcome.");
- break;
- } else {
- System.out.println("Incorrect username or password.");
- }
- // System.out.println(tempUsername.toString());
- // System.out.println(tempPassword.toString());
- }
- input.close();
- reader.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- Login_Register lr = new Login_Register();
- System.out.println("[1] - Register\n[2] - Login\nEnter your choice: ");
- int choice = input.nextInt();
- switch(choice){
- case 1:
- lr.resgister();
- break;
- case 2:
- lr.login();
- break;
- default:
- System.out.println("Invalid input!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement