0% found this document useful (0 votes)
29 views

06-Task-Performance-1-ARG

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

06-Task-Performance-1-ARG

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class TaskPerf6 {

private static final String RECORDS_FILE = "C:\\Users\\Norman\\Documents\\


NetBeansProjects\\Comprog\\TextStorage\\records.txt";

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("Select an option:");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
register(scanner);
break;
case 2:
login(scanner);
break;
case 3:
System.out.println("Exiting program...");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 3.");
}
}
}

private static void register(Scanner scanner) {


try {
FileWriter writer = new FileWriter(RECORDS_FILE, true);

System.out.print("Enter your desired username: ");


String username = scanner.next();

// Check if username already exists


if (usernameExists(username)) {
System.out.println("Username already exists. Please choose another
one.");
return;
}

System.out.print("Enter your password: ");


String password = scanner.next();

// Write username and password to records file


writer.write(username + " " + password + "\n");
writer.close();
System.out.println("Registration successful.");
} catch (IOException e) {
System.out.println("An error occurred while registering.");
e.printStackTrace();
}
}

private static void login(Scanner scanner) {


try {
System.out.print("Enter your username: ");
String username = scanner.next();
System.out.print("Enter your password: ");
String password = scanner.next();

// Validate username and password


if (validateCredentials(username, password)) {
System.out.println("Successfully logged in.");
} else {
System.out.println("Incorrect username or password.");
}
} catch (IOException e) {
System.out.println("An error occurred while logging in.");
e.printStackTrace();
}
}

private static boolean usernameExists(String username) throws IOException {


File file = new File(RECORDS_FILE);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(" ");
if (parts.length >= 1 && parts[0].equals(username)) {
return true;
}
}
return false;
}

private static boolean validateCredentials(String username, String password)


throws IOException {
File file = new File(RECORDS_FILE);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(" ");
if (parts.length >= 2 && parts[0].equals(username) &&
parts[1].equals(password)) {
return true;
}
}
return false;
}
}

You might also like