import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class Account {
private short userАge;
private boolean haveNumber;
private String password;
private String login;
private String userName;
Scanner scanner = new Scanner(System.in);
MainProcess mp = new MainProcess();
public void createAccount() {
System.out.println("Введите ваше имя: ");
userName = scanner.nextLine();
System.out.println("Придумайте логин, " + userName);
login = scanner.nextLine();
if (login.length() < 3) {
System.out.println("Логин должен быть больше 3 символов");
createAccount();
}
System.out.println("Придумайте пароль");
password = scanner.nextLine();
if (password.length() < 6) {
System.out.println("Убедитесь что длина пороля больше или равна 6 символам");
createAccount();
}
System.out.println("Введите возраст");
userАge = scanner.nextShort();
if (userАge < 18) {
System.out.println("Открыть счет возможно только с 18 лет.");
createAccount();
}
writeData();
mp.selection();
}
public String loginAccount() {
System.out.println("Для входа в аккаунт укажите логин:");
login = scanner.nextLine();
File findAccount = new File(login + ".txt");
boolean haveAccount = findAccount.exists();
try {
if (haveAccount = true) {
System.out.println("Введите пароль:");
password = scanner.nextLine();
String line = Files.readAllLines(Paths.get(login + ".txt")).get(2);
int startNum = 10;
int endNum = line.length();
line = line.substring(startNum, endNum);
boolean equal = line.equals(password);
if (equal) {
System.out.println("Вы успешно вошли в аккаунт!");
mp.mainOptins();
} else {
System.out.println("Проверьте правильнсть пароля");
}
} else {
System.out.println("Аккаунт не найден");
mp.selection();
}
} catch (IOException e) {
e.printStackTrace();
}
return login;
}
public void writeData() {
try {
File userData = new File(login + ".txt");
FileWriter writerUser = new FileWriter(userData, true);
BufferedWriter bufferWriter = new BufferedWriter(writerUser);
File tempFile = new File(login + ".txt");
boolean exists = tempFile.exists();
if (exists == true) {
System.out.println("Такой аккаунт уже существует");
mp.selection();
} else {
bufferWriter.write("name: " + userName + "\n" + "login: " + login + "\n" + "password: " + password + "\n" + "age: " + userАge + "\n");
bufferWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public String getLogin() {
return login;
}
}