import java.io.*;
public class PasswordManager {
private static PrintStream o = System.out;
private BufferedReader in;
private int task = -1;
private String input = "";
private PropertiesHelp ph = new PropertiesHelp("datastore.txt");
public PasswordManager() {
greet();
askForTask();
}
public static void main(String[] args) {
new PasswordManager();
}
public void askForTask() {
o.println("Type a number for wanted task, please!");
o.println("After any input you have to press ENTER.");
o.println("If you want ...");
o.println();
o.println("e) exit Super PasswordManager 2000 -> type 0");
o.println("a) write new user data -> type 1");
o.println("b) get password for a given username -> type 2");
o.println("c) check password for a given username -> type 3");
o.println("d) check whether user exists or not-> type 4");
o.println("d) print user data list -> type 5");
o.println();
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
task = Integer.valueOf(input);
} catch (NumberFormatException e) {
o.println("Your input was invalid. Try it again, please!");
o.println();
task = 99;
}
switch (task) {
case 0: {
o.println("Bye till next session");
System.exit(0);
}
break;
case 1: {
enterNewUserdata();
}
break;
case 2: {
getPasswordforUsername();
}
break;
case 3: {
checkPassword();
}
break;
case 4: {
containsUser();
}
break;
case 5: {
listUserData();
}
break;
default:
askForTask();
}
}
public void greet() {
o.println("Hallo! Welcome to the prompt of Super PasswordManager 2000");
o.println("Please, press ENTER to go on.");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
o.println();
}
public void enterNewUserdata() {
String username = "";
String password = "";
o.println("Type a user name, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
username = input;
o.println("Type the password for this user, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
password = input;
if (username != null && username.length() > 0 && password != null
&& password.length() > 0) {
ph.setProperty(username, password);
o.println("user data is succesfully saved.");
}
o.println();
askForTask();
}
public void getPasswordforUsername() {
String username = "";
o.println("Type a user name, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
username = input;
if (username != null && username.length() > 0) {
if (ph.containsKey(username))
o.println("password: " + ph.getProperty(username));
else
o.println("User don't exists in database.");
}
o.println();
askForTask();
}
public void checkPassword() {
String username = "";
String password = "";
o.println("Type a user name, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
username = input;
o.println("Type the password for this user, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
password = input;
if (username != null && username.length() > 0 && password != null
&& password.length() > 0) {
if (ph.containsKey(username))
if (ph.getProperty(username).equals(password))
o.println("Yes, password is right.");
else
o.println("No, password is wrong.");
else
o.println("User don't exists.");
} else
o.println("Input is invalid, try it again, please!");
o.println();
askForTask();
}
public void containsUser() {
String username = "";
o.println("Type a user name, please!");
try {
in = new BufferedReader(new InputStreamReader(System.in));
input = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
username = input;
if (username != null && username.length() > 0) {
if (ph.containsKey(username))
o.println("User exists in database.");
else
o.println("User don't exists in database.");
}
o.println();
askForTask();
}
public void listUserData() {
ph.printOut();
o.println();
askForTask();
}
}