-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistration
More file actions
94 lines (82 loc) · 3.44 KB
/
Registration
File metadata and controls
94 lines (82 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package Learn_and_Build;
//BufferedReader is a class that allows efficient reading of text from a character-input stream.
import java.io.BufferedReader;
// BufferedReader is a class that allows efficient reading of text from a character-output stream..
import java.io.BufferedWriter;
// FileReader is a class that reads character files.
import java.io.FileReader;
//FileWriter is a class that writes character files.
import java.io.FileWriter;
// IOException is a class that is used to handle errors
import java.io.IOException;
//Scanner is a class that is used to read input from the console or other input sources.
import java.util.Scanner;
public class RegistrationAndLogin {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Welcome to the program!");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (choice) {
case 1:
registerUser(scanner);
break;
case 2:
loginUser(scanner);
break;
case 3:
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid choice. Try again.");
break;
}
} while (choice != 3);
scanner.close();
}
private static void registerUser(Scanner scanner) {
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Enter your email address:");
String email = scanner.nextLine();
System.out.println("Enter your password:");
String password = scanner.nextLine();
//The write() method of the BufferedWriter object is called to write the user's information to the file(txt).
try (BufferedWriter writer = new BufferedWriter(new FileWriter("users.txt", true))) {
writer.write(name + "," + email + "," + password + "\n");
System.out.println("Registration successful!");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
private static void loginUser(Scanner scanner) {
System.out.println("Enter your email address:");
String email = scanner.nextLine();
System.out.println("Enter your password:");
String password = scanner.nextLine();
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
String line;
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3 && parts[1].equals(email) && parts[2].equals(password)) {
found = true;
break;
}
}
if (found) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid email or password. Try again.");
}
} catch (IOException e) {
System.out.println("Error reading from file: " + e.getMessage());
}
}
}