Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //29.5.17
- //Digital Technology Password Safe Assessment
- //Code By William Hollywood
- package code;
- import java.awt.GraphicsEnvironment;
- import java.io.Console;
- // Import libraries that get used
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- import javax.swing.JTextArea;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Start {
- Scanner in = new Scanner(System.in); //makes a scanner used to taking inputs
- ArrayList<User> userDatabase; //defines the userDatabase array
- private User loggedInUser; //makes a loggedInUser that is only accessable within this start class
- String emailEnter = ""; //some variables used within the program
- String passwordEnter = "";
- int choice = -2;
- int choice2 = 0;
- boolean correctWebPass = false;
- boolean correctUserPass = false;
- String confirm;
- String webOrPass;
- String nameOrPass;
- String newInfo;
- boolean exit = false; //used to know when to exit the whole program
- static boolean loginCorrect = false; //used to check whether login the user entered is valid
- public static void window(){ //function for making a GUI
- JFrame frame = new JFrame(); //makes frame
- frame.setResizable(false); //makes unresizable
- frame.setSize(400, 400); //set size
- frame.setLocationRelativeTo(null); //puts in middle of screen
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //end program on gui close
- frame.getContentPane().setLayout(null);
- JTextArea txtConsole = new JTextArea(); //makes text area
- txtConsole.setBounds(6, 6, 388, 224); //sets text area size
- frame.getContentPane().add(txtConsole);
- frame.setVisible(true);
- txtConsole.setEditable(false);
- PrintStream out = new PrintStream( new Frame( txtConsole ) ); //puts Textarea onto frame
- System.setOut( out ); //set output of console to text area
- System.setErr( out ); //set error output of console to text area
- }
- public void loadArrays(){ //function to load the array into the userDatabase so it can be read.
- try {
- String filename = System.getProperty("user.home") + "/users.ser"; //locates file users are stored in
- FileInputStream fileIn = new FileInputStream(filename); //reads file
- ObjectInputStream objin = new ObjectInputStream(fileIn);// redirects file read
- userDatabase = (ArrayList<User>) objin.readObject(); //puts the users and their info into an arraylist
- objin.close(); //closes reader
- fileIn.close();
- }catch(FileNotFoundException f){ // if file isnt found make a new array and add a default user
- userDatabase = new ArrayList();
- User test = new User("Test@test.com", "TestTest1!", "Testing User");
- userDatabase.add(test);
- test.webName.add("Facebook");
- test.webName.add("Twitter");
- test.webName.add("Google");
- test.webPass.add("FacebookPass");
- test.webPass.add("TwitterPass");
- test.webPass.add("GooglePass");
- }catch(IOException i) { //catches IO error so program doesnt crash
- i.printStackTrace();
- return;
- }catch(ClassNotFoundException c) { //if user class isnt found print a stacktrace
- System.out.println("User class not found");
- c.printStackTrace();
- return;
- }
- }
- public void saveArray(){ //exports the array into the file so it is saved for next time you login
- try {
- String filename = System.getProperty("user.home") + "/users.ser"; //locates file
- FileOutputStream fileOut =
- new FileOutputStream(filename); //write to file located above
- ObjectOutputStream out = new ObjectOutputStream(fileOut);
- out.writeObject(userDatabase); //export the Database to the file
- out.close();
- fileOut.close(); //close writer
- //System.out.println("Serialized data is saved in "+filename);
- }catch(IOException i) { //catches IO error so program doesnt crash
- i.printStackTrace();
- }
- }
- public void login(){ //login function
- loginCorrect = false;
- System.out.println("Please enter your email:"); //asks for email and saves it
- emailEnter = in.next();
- System.out.println("Please enter your password:");//asks for password and saves it
- passwordEnter = in.next();
- for (User u : userDatabase){ //for every user in userdatabase set 'u' to that user
- //System.out.println(u); //checking users for debugging
- try{
- if (emailEnter.equalsIgnoreCase(u.email) && passwordEnter.equals(u.password)){ //if email (non case sensitive) and password are correct do following
- loggedInUser = u;
- System.out.println("Login successful, Welcome "+loggedInUser.alias); //welcomes user using saved alias
- loginCorrect = true; //allows the loop to end
- return;
- }
- }catch(NullPointerException e){
- }
- }
- if (loginCorrect == false){
- System.out.println("Incorrect email or password");
- }
- }
- public void register(){ //function that registers a new account
- String newEmail = ""; //clears the info that will get inputed and resets some values used
- String newPass = "";
- String newAlias = "";
- boolean passValid = false;
- boolean passUpper = false;
- boolean passLower = false;
- boolean passNumber = false;
- boolean passLen = false;
- boolean passSym = false;
- boolean passSpace = false;
- boolean validregister = true;
- boolean emailValid = false;
- boolean emailDot = false;
- boolean emailAt = false;
- boolean emailSpace = false;
- in.nextLine(); //clears scanner
- while(emailValid == false){
- System.out.println("What is your email?");
- newEmail = in.nextLine(); //gets email
- for (char c :newEmail.toCharArray()){ //converts the String input into single characters and checks them each to see if they match the following criteria
- if (c == '@'){ //check for @
- emailAt = true;
- }
- if (c == '.'){ //check for .
- emailDot = true;
- }
- if (Character.isSpaceChar(c)){ //check if a space
- emailSpace = true;
- }
- }
- if (emailAt == true && emailDot == true && emailSpace == false){//check if there is a '@' and a '.' but no spaces
- emailValid = true;
- }
- if(emailValid == false){ // if email isnt valid then do the loop again
- System.out.println("Invalid email please try again");
- System.out.println("");
- }
- }
- while(passValid == false){ // loop for checking password is valid
- System.out.println("What is your Password? (Must contain at least 1 lower case, 1 upper case, 1 number, 1 symbol, no spaces and 8 characters)");
- newPass = in.nextLine(); //gets password
- for (char c : newPass.toCharArray()){ //check every character in the input for the following
- if (Character.isUpperCase(c)){passUpper = true;} //uppercase letter
- if (Character.isLowerCase(c)){passLower = true;} //lowercase letter
- if (Character.isDigit(c)){passNumber = true;} // number
- if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)){passSym = true;} //symbol (character that isnt a digit or letter)
- if (Character.isSpaceChar(c)){passSpace = true;} //check for space
- }
- if (newPass.length() >= 8){passLen = true;} //length of input equal to or longer than 8 characters
- if (passUpper == true && passLower == true && passNumber == true && passSym == true && passSpace == false && passLen == true){ // checks to make sure all criteria are met
- passValid = true;
- }
- if(passValid == false){ //if password doesnt fill all critera say they need to reinput it
- System.out.println("Please enter a password which meets all the requirements");
- System.out.println("");
- }
- }
- System.out.println("What is your Name?");
- newAlias = in.nextLine();
- for (User u : userDatabase){ //set u to every user in userdatabase
- //System.out.println(u);
- try{
- if (newEmail.equalsIgnoreCase(u.email)){ //checks if email is already taken
- System.out.println("");
- System.out.println("Email already registered to an account, please try a different email next time");
- System.out.println("");
- validregister = false;
- }
- }catch(NullPointerException e){ //catches null users
- }
- }
- if(validregister == true){ //if email isnt taken
- userDatabase.add(new User(newEmail, newPass, newAlias));// add the information inputted above into the userdatabase
- System.out.println("User saved, you can now login using the following credentials");
- System.out.println("Email: "+newEmail);
- System.out.println("Password: "+newPass);
- System.out.println("");
- }
- }
- public void choiceLoginRego(){ //gets whether the user wants to make a new account or register for a new one
- while (choice <= 0 || choice >= 3){ //ensures input is 1 or 2
- System.out.println("Do you want to sign in as an existing user (1) or register as a new one(2)?");
- try {
- choice = in.nextInt();
- }catch(InputMismatchException e) { //doesnt allow any other input except for an int
- System.out.println("Please Enter A Valid Input");
- in.next();
- System.out.println("");
- }
- }
- }
- public void choiceApp(){ //gets what app the user wants to use
- System.out.println("What do you want to do?");
- System.out.println("1. Check existing websites and passwords");
- System.out.println("2. Add new website and password");
- System.out.println("3. Delete a stored website and password");
- System.out.println("4. Change website name and/or password");
- System.out.println("5. Change login information");
- System.out.println("6. Exit");
- while (choice <= 0 || choice >= 7){ //ensures input is within 1-5
- System.out.println("Please enter the corresponding number for what you want to do");
- try {
- choice = in.nextInt();
- }catch(InputMismatchException e) { //doesnt allow any inputs except for an int
- System.out.println("Please Enter A Valid Input");
- in.next();
- System.out.println("");
- }
- }
- }
- public void delWebpass(){
- boolean delWeb = false;
- while(delWeb == false){
- System.out.println("");
- System.out.println("Which website do you want to delete? (input 0 to cancel)");
- for (int i = 0;(i < loggedInUser.webName.size());i++){
- System.out.println((i+1)+". "+loggedInUser.webName.get(i));
- }
- int delNum = -2;
- while (delNum <= -1 || delNum >= (loggedInUser.webName.size() + 1)){ //ensures input is within 1-5
- System.out.println("Please enter the corresponding number for what you want to remove");
- try {
- delNum = in.nextInt();
- }catch(InputMismatchException e) { //doesnt allow any inputs except for an int
- System.out.println("Please Enter A Valid Input");
- in.next();
- System.out.println("");
- }
- }
- if (delNum == 0){
- delWeb = true;
- }else{
- System.out.println("You have chosen to delete "+loggedInUser.webName.get(delNum-1)+ " is this correct?");
- System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
- in.nextLine();
- confirm = in.nextLine(); // gets whether they have put in the correct info
- if(confirm.equalsIgnoreCase("Yes")){ //if answer is yes end the loop
- loggedInUser.webName.remove(delNum-1);
- loggedInUser.webPass.remove(delNum-1);
- delWeb = true;
- System.out.println("Website deleted");
- System.out.println("");
- }else{
- if(confirm.equalsIgnoreCase("No")){ //if answer is no redo the loop
- delWeb = false;
- }else{//else redo the loop
- delWeb = false;
- }
- }
- }
- }
- }
- public void checkWebpass(){ //app to check all websites and passwords
- if (loggedInUser.webName.size()>0){
- System.out.println("You have passwords saved for the following websites:");
- System.out.println("");
- for (int i = 0;(i < loggedInUser.webName.size());i++){ //prints full website list by iterating through the array
- System.out.println((i+1)+". "+loggedInUser.webName.get(i)); // output EG. "1. Facebook"
- }
- System.out.println("");
- choice = 0;
- while (choice <= 0 || choice >= (loggedInUser.webName.size()+1)){ // ensures input is within 1-(amount of websites in array)
- System.out.println("Please enter the corresponding number for what password you want to see");
- try {
- choice = in.nextInt();
- }catch(InputMismatchException e) { //doesnt allow inputs except for a int
- System.out.println("Please Enter A Valid Input");
- in.next();
- System.out.println("");
- }
- }
- System.out.println("");
- System.out.println("Your saved password for "+loggedInUser.webName.get((choice-1))+" is: "+loggedInUser.webPass.get((choice-1))); //prints selected website and password for website
- System.out.println("");
- }else{
- if(loggedInUser.webName.size()<=0){
- System.out.println("");
- System.out.println("You have no passwords saved for any websites");
- System.out.println("");
- }
- }
- }
- public void addWebpass(){ //function to add a website and password
- int i = 1; //interger used for iterating
- String addNewWeb = null; //clears website and password
- String addNewPass = null;
- correctWebPass = false;
- while (correctWebPass == false){
- System.out.println("");
- System.out.println("What is the name of the website you want to add?");
- if(i == 1){in.nextLine();i++;} //used on first runthrough to clear the scanner and add 1 to i so that it wont run again
- addNewWeb = in.nextLine(); //saves Website from user
- System.out.println("");
- System.out.println("What is the password for this site?");
- addNewPass = in.nextLine(); //saves password from user
- System.out.println("");
- System.out.println("Is this information correct?");
- System.out.println("Website: "+addNewWeb);
- System.out.println("Password: "+addNewPass);
- System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
- confirm = in.nextLine();
- if(confirm.equalsIgnoreCase("Yes")){//confirms whether input was yes
- correctWebPass = true;
- }else{
- if(confirm.equalsIgnoreCase("No")){//confirms whether input was no
- correctWebPass = false;
- }else{ //if input is anything else
- correctWebPass = false;//boolean that ends/continues add website loop
- }
- }
- }
- System.out.println("");
- System.out.println("The following information has been stored"); //Prints what info was saved
- System.out.println("Website: "+addNewWeb);
- System.out.println("Password: "+addNewPass);
- System.out.println("");
- loggedInUser.webName.add(addNewWeb);
- loggedInUser.webPass.add(addNewPass);
- //System.out.println(userDatabase);
- }
- public void changeWebpass(){ //app that changes the website name or website password for a specific website
- correctWebPass = false;
- if (loggedInUser.webName.size() == 0){
- System.out.println("");
- System.out.println("No passwords stored for any sites");
- System.out.println("");
- }else{
- while (correctWebPass == false){ //loops so that the user can retry putting in the specifics
- System.out.println("What website do you want to change information for?");
- for (int x = 0;(x < loggedInUser.webPass.size());x++){ //prints a line of all the websites and passwords in the arraylist
- System.out.println((x+1)+". "+loggedInUser.webName.get(x)+", Password: "+loggedInUser.webPass.get(x));
- }
- choice = 0;
- while (choice <= 0 || choice >= (loggedInUser.webName.size()+1)){ //ensures user inputs a number that is relative to the websites in the list
- System.out.println("");
- System.out.println("What is the number of the data set you want to change?");
- try {
- choice = in.nextInt(); //gets choice that user wants
- }catch(InputMismatchException e) {
- System.out.println("Please Enter A Valid Input"); //checks whether input was an input other than an int
- in.next();
- }
- }
- System.out.println("You have chosen to change the "+loggedInUser.webName.get(choice-1)+" login"); //displays website that was chosen
- choice2 = 0; //resets the chocie for username or password
- while (choice2 <= 0 || choice2 >= 3){ //ensures user inputs an int (1 or 2)
- System.out.println("");
- System.out.println("Do you want to change the name (1) or password (2) for "+loggedInUser.webName.get(choice-1));
- try {
- choice2 = in.nextInt();
- }catch(InputMismatchException e) { //catches whether input is not an int
- System.out.println("Please Enter A Valid Input");
- System.out.println("");
- in.next();
- }
- }
- if (choice2 == 1){webOrPass = "Name";} //sets variable to name or password so it can be used below
- if (choice2 == 2){webOrPass = "Password";}
- System.out.println("");
- System.out.println("You have chosen to change the "+webOrPass+" of the website"); //prints what they've chosen
- System.out.println("What do you want to change the "+webOrPass+ " to?"); //asks what they want to change the password to
- in.nextLine();newInfo = in.nextLine(); //clears scanner then gets the new name or password
- System.out.println("");
- System.out.println("Is this information correct?");
- if (choice2 == 1){ //prints new website name and password
- System.out.println("Website: "+newInfo);
- System.out.println("Password: "+loggedInUser.webPass.get(choice-1));
- }
- if (choice2 == 2){//prints website name and new password
- System.out.println("Website: "+loggedInUser.webName.get(choice-1));
- System.out.println("Password: "+newInfo);
- }
- System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
- confirm = in.nextLine(); // gets whether they have put in the correct info
- if(confirm.equalsIgnoreCase("Yes")){ //if answer is yes end the loop
- correctWebPass = true;
- }else{
- if(confirm.equalsIgnoreCase("No")){ //if answer is no redo the loop
- correctWebPass = false;
- }else{//else redo the loop
- correctWebPass = false;
- }
- }
- }
- System.out.println("");
- System.out.println("The following information has been stored");
- if (choice2 == 1){//sets website name to the new one
- loggedInUser.webName.set(choice-1, newInfo);
- }
- if (choice2 == 2){//sets website password to the new one
- loggedInUser.webPass.set(choice-1, newInfo);
- }
- System.out.println("Website: "+loggedInUser.webName.get(choice-1)); //prints new info
- System.out.println("Password: "+loggedInUser.webPass.get(choice-1));
- }
- }
- public void changeLogin(){ //app that changes the
- correctUserPass = false;
- while (correctUserPass == false){
- choice = 0;
- while (choice <= 0 || choice >= 3){
- System.out.println("Do you want to change your email (1) or password (2)");
- try {
- choice = in.nextInt();
- }catch(InputMismatchException e) {
- System.out.println("Please Enter A Valid Input");
- in.next();
- }
- }
- if (choice == 1){nameOrPass = "Email";}
- if (choice == 2){nameOrPass = "Password";}
- System.out.println("You have chosen to change your " +nameOrPass);
- System.out.println("What do you want to change your "+nameOrPass+ " to?");
- if (choice == 1){
- in.nextLine();
- newInfo = in.nextLine();
- }
- if(choice == 2){
- boolean passValid = false;
- boolean passUpper = false;
- boolean passLower = false;
- boolean passNumber = false;
- boolean passLen = false;
- boolean passSym = false;
- boolean passSpace = false;
- int i = 0;
- while(passValid == false){
- System.out.println("(Must contain at least 1 lower case, 1 upper case, 1 number, 1 symbol, no spaces and 8 characters)");
- if (i==0){in.nextLine();i++;}
- newInfo = in.nextLine(); //gets password
- for (char c : newInfo.toCharArray()){ //check every character in the input for the following
- if (Character.isUpperCase(c)){passUpper = true;} //uppercase letter
- if (Character.isLowerCase(c)){passLower = true;} //lowercase letter
- if (Character.isDigit(c)){passNumber = true;} // number
- if (!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)){passSym = true;} //symbol (character that isnt a digit or letter)
- if (Character.isSpaceChar(c)){passSpace = true;} //check for space
- }
- if (newInfo.length() >= 8){passLen = true;} //length of input equal to or longer than 8 characters
- if (passUpper == true && passLower == true && passNumber == true && passSym == true && passSpace == false && passLen == true){ // checks to make sure all criteria are met
- passValid = true;
- }
- if(passValid == false){ //if password doesnt fill all critera say they need to reinput it
- System.out.println("Please enter a password which meets all the requirements");
- }
- }
- }
- System.out.println("Is this information correct?");
- if (choice == 1){
- System.out.println("Email: "+newInfo);
- System.out.println("Password: "+loggedInUser.password);
- }
- if (choice == 2){
- System.out.println("Email: "+loggedInUser.email);
- System.out.println("Password: "+newInfo);
- }
- System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
- confirm = in.nextLine();
- if(confirm.equalsIgnoreCase("Yes")){
- correctUserPass = true;
- }else{
- if(confirm.equalsIgnoreCase("No")){
- correctUserPass = false;
- }else{
- correctUserPass = false;
- }
- }
- }
- System.out.println("The following information has been stored");
- if (choice == 1){
- loggedInUser.email = newInfo;
- }
- if (choice == 2){
- loggedInUser.password = newInfo;
- }
- System.out.println("Email: "+loggedInUser.email);
- System.out.println("Password: "+loggedInUser.password);
- System.out.println("");
- //System.out.println(userDatabase);
- }
- public void exit(){
- System.out.println("Are you sure you want to exit?");
- System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
- confirm = in.nextLine();
- confirm = in.nextLine();
- if(confirm.equalsIgnoreCase("Yes")){ //checks if input = yes ignoring capital letters
- exit = true;
- System.out.println("Password Safe closed, thank you for using.");
- }else{
- if(confirm.equalsIgnoreCase("No")){ //checks if input = no ignoring capital letters
- exit = false;
- }else{
- exit = false;
- }
- }
- }
- public static void main(String [] orgs) {
- Start start = new Start();
- //window();
- System.out.println("Testing User credentials:");
- System.out.println("Email: Test@test.com");
- System.out.println("Password: TestTest1!");
- start.loadArrays();
- start.saveArray();
- while (loginCorrect == false){
- //System.out.println(start.userDatabase);
- start.choice = 0;
- start.choiceLoginRego();
- if(start.choice == 1){start.login();}
- if(start.choice == 2){start.register();start.saveArray();start.loadArrays();}
- }
- while(start.exit == false){
- start.choice = 0;
- start.choiceApp();
- if(start.choice == 1){start.checkWebpass();start.choice =0;}
- if(start.choice == 2){start.addWebpass();start.choice =0;}
- if(start.choice == 3){start.delWebpass();start.choice =0;}
- if(start.choice == 4){start.changeWebpass();start.choice =0;}
- if(start.choice == 5){start.changeLogin();start.choice =0;}
- if(start.choice == 6){start.exit();start.choice =0;}
- start.saveArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement