package computer.login.v2;
import java.io.*;
import java.util.*;
public class ComputerLoginV2 {
public static void main(String[] args) throws FileNotFoundException, IOException {
File user = new File("user.txt");
File pass = new File("pass.txt");
Scanner getsome = new Scanner(System.in);
Scanner checksomeu = new Scanner(user);
Scanner checksomep = new Scanner(pass);
int trys;
String loginstatus, username, password, firsttimeuser, firsttimepass1, firsttimepass2, usertry, passtry, reset;
loginstatus = "blocked";
trys = 0;
while (loginstatus.equals("blocked")) { //this is suppposed to create the new file
if (!user.getAbsoluteFile().exists()) {
user.createNewFile();
FileOutputStream userstream = new FileOutputStream(user);
}
if (!user.getAbsoluteFile().exists()){
pass.createNewFile();
FileOutputStream passstream = new FileOutputStream(pass);
}
loginstatus = "false";
} //end of problem
while (loginstatus.equals("false")) { //here on works
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
if (!checksomeu.hasNextLine() || !checksomep.hasNextLine()) { //no login already set up
System.out.println("First Time Users"
+ "\nPlease enter a user name(write this down as you will need it later):");
firsttimeuser = getsome.nextLine();
System.out.println("And a password:");
firsttimepass1 = getsome.nextLine();
System.out.println("Enter the password again");
firsttimepass2 = getsome.nextLine();
if (firsttimepass1.equals(firsttimepass2)) {
username = firsttimeuser;
password = firsttimepass1;
PrintStream console = System.out; //console defined normal output
FileOutputStream userstream = new FileOutputStream(user); //sends all file output to txt
PrintStream userprintstream = new PrintStream(userstream); //carlton print stream defined as a text file
System.setOut(userprintstream); //redefines stestem.our as to where is goes
System.out.println(username); //now text goes to the file
System.setOut(console); //switches back to the console
System.out.println("Your username has been set");//now its normal
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
PrintStream passprintstream = new PrintStream(passstream); //carlton print stream defined as a text file
System.setOut(passprintstream); //redefines stestem.our as to where is goes
System.out.println(password);
System.setOut(console); //switches back to the console
System.out.println("Your password has been set");
}
} else {
System.out.println("Please enter your username");
usertry = getsome.nextLine();
System.out.println("And your password");
passtry = getsome.nextLine();
if (usertry.equals(checksomeu.nextLine()) && passtry.equals(checksomep.nextLine())) {
loginstatus = "true";
} else {
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
System.out.println("Nice try And ");
trys = trys + 1;
if (!usertry.equals(checksomeu.nextLine()) && !passtry.equals(checksomep.nextLine())) {
System.out.println("Wow you got them both wrong");
} else {
System.out.println("This isn't the government bro, Get it completely right");
}
}
}
if (trys > 5) {
System.out.println("System Lock Out");
loginstatus = "lockout";
}
}
if (loginstatus.equals("true")) {
System.out.println("Congratulations you Logged in"
+ "\nDo you want to reset the username and password?(y/n)");
reset = getsome.nextLine();
if (reset.equals("y")) {
System.out.println("Good because that username and password was crap!");
FileOutputStream userstream = new FileOutputStream(user);
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
}
if (!reset.equals("y") && !reset.equals("n")) {
System.out.println("I'm just going to assume you meant no");
}
if (reset.equals("n")) {
System.out.println("Your settings should still be here the next time you turn me on
");
}
}
if (loginstatus.equals("lockout")) {
System.out.println("Go hack someone elses basic java program!");
}
}
}