Advertisement
Guest User

Will's Assessment V3

a guest
May 28th, 2017
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.79 KB | None | 0 0
  1. package code;
  2.  
  3. // Import libraries that get used
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.io.PrintStream;
  11. import java.util.ArrayList;
  12. import java.util.InputMismatchException;
  13. import java.util.Scanner;
  14.  
  15. import javax.swing.JTextArea;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18.  
  19. public class Start {
  20.  
  21.     Scanner in = new Scanner(System.in); //makes a scanner used to taking inputs
  22.     ArrayList<User> userDatabase; //defines the userDatabase array
  23.     ArrayList<Integer> nullUsers;
  24.     private User loggedInUser; //makes a loggedInUser that is only accessable within  this start class
  25.  
  26.     String emailEnter = ""; //some variables used within the program
  27.     String passwordEnter = "";
  28.     int choice = -2;
  29.     int choice2 = 0;
  30.     boolean correctWebPass = false;
  31.     boolean correctUserPass = false;
  32.     String confirm;
  33.     String webOrPass;
  34.     String nameOrPass;
  35.     String newInfo;
  36.  
  37.     boolean exit = false; //used to know when to exit the whole program
  38.     static boolean loginCorrect = false; //used to check whether login the user entered is valid
  39.  
  40.  
  41.     public static void window(){ //function for making a GUI
  42.         JFrame frame = new JFrame(); //makes frame
  43.         frame.setResizable(false); //makes unresizable
  44.         frame.setSize(400, 400); //set size
  45.         frame.setLocationRelativeTo(null); //puts in middle of screen
  46.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //end program on gui close
  47.         frame.getContentPane().setLayout(null);
  48.  
  49.         JTextArea txtConsole = new JTextArea(); //makes text area
  50.         txtConsole.setBounds(6, 6, 388, 224); //sets text area size
  51.         frame.getContentPane().add(txtConsole);
  52.         frame.setVisible(true);
  53.         txtConsole.setEditable(false);
  54.  
  55.         PrintStream out = new PrintStream( new Frame( txtConsole ) ); //puts Textarea onto frame
  56.  
  57.         System.setOut( out ); //set output of console to text area
  58.         System.setErr( out ); //set error output of console to text area
  59.     }
  60.  
  61.     public void loadArrays(){ //function to load the array into the userDatabase so it can be read.
  62.         try {
  63.             String filename = System.getProperty("user.home") + "/users.ser"; //locates file users are stored in
  64.             FileInputStream fileIn = new FileInputStream(filename); //reads file
  65.             ObjectInputStream objin = new ObjectInputStream(fileIn);// redirects file read
  66.             userDatabase = (ArrayList<User>) objin.readObject(); //puts the users and their info into an arraylist
  67.             objin.close(); //closes reader
  68.             fileIn.close();
  69.         }catch(FileNotFoundException f){ // if file isnt found make a new array and add a default user
  70.             userDatabase = new ArrayList();
  71.             User test = new User("Test@test.com", "Test1!", "Testing User");
  72.             userDatabase.add(test);
  73.         }catch(IOException i) { //catches IO error so program doesnt crash
  74.             i.printStackTrace();
  75.             return;
  76.         }catch(ClassNotFoundException c) { //if user class isnt found print a stacktrace
  77.             System.out.println("User class not found");
  78.             c.printStackTrace();
  79.             return;
  80.         }
  81.     }
  82.  
  83.     public void saveArray(){ //exports the array into the file so it is saved for next time you login
  84.         try {
  85.             String filename = System.getProperty("user.home") + "/users.ser"; //locates file
  86.             FileOutputStream fileOut =
  87.                     new FileOutputStream(filename); //write to file located above
  88.             ObjectOutputStream out = new ObjectOutputStream(fileOut);
  89.             out.writeObject(userDatabase); //export the Database to the file
  90.             out.close();
  91.             fileOut.close(); //close writer
  92.             System.out.println("Serialized data is saved in "+filename);
  93.         }catch(IOException i) { //catches IO error so program doesnt crash
  94.             i.printStackTrace();
  95.         }
  96.     }
  97.  
  98.     public void login(){ //login function
  99.         for (int tries=0;(tries<3);tries++) {
  100.             System.out.println("Please enter your email:"); //asks for email and saves it
  101.             emailEnter = in.next();
  102.             System.out.println("Please enter your password:");//asks for password and saves it
  103.             passwordEnter = in.next();
  104.             for (User u : userDatabase){ //for every user in userdatabase set 'u' to that user
  105.                 System.out.println(u); //checking users for debugging
  106.                 /*if (u == null){ //if next user is blank it means its checked all of the other users so the user isnt in the database
  107.                 }*/
  108.                 try{
  109.                     if (emailEnter.equalsIgnoreCase(u.email) && passwordEnter.equals(u.password)){ //if email (non case sensitive) and password are correct do following
  110.                         loggedInUser = u;
  111.                         System.out.println("Login successful, Welcome "+loggedInUser.alias); //welcomes user using saved alias
  112.                         loginCorrect = true; //allows the loop to end
  113.                         return;
  114.                     }
  115.                 }catch(NullPointerException e){
  116.  
  117.                 }
  118.             }
  119.  
  120.         }
  121.     }
  122.    
  123.  
  124.     public void register(){
  125.         String newEmail = "";
  126.         String newPass = "";
  127.         String newAlias = "";
  128.         boolean validregister = true;
  129.         System.out.println("What is your email?");
  130.         in.nextLine();
  131.         newEmail = in.nextLine();
  132.         System.out.println("What is your Password?");
  133.         newPass = in.nextLine();
  134.         System.out.println("What is your Name?");
  135.         newAlias= in.nextLine();
  136.         for (User u : userDatabase){
  137.             System.out.println(u);
  138.             try{
  139.                 if (newEmail.equalsIgnoreCase(u.email)){
  140.                     System.out.println("Email already registered to an account, please try a different email next time");
  141.                     validregister = false;
  142.                 }
  143.             }catch(NullPointerException e){
  144.  
  145.             }
  146.         }
  147.         if(validregister == true){
  148.             userDatabase.add(new User(newEmail, newPass, newAlias));
  149.             System.out.println("User saved, you can now login using the following credentials");
  150.             System.out.println("Email: "+newEmail);
  151.             System.out.println("Password: "+newPass);
  152.             System.out.println("");
  153.         }
  154.     }
  155.  
  156.     public void choiceLoginRego(){ //gets whether the user wants to make a new account or register for a new one
  157.         while (choice <= 0 || choice >= 3){ //ensures input is 1 or 2
  158.             System.out.println("Do you want to sign in as an existing user (1) or register as a new one(2)?");
  159.             try {
  160.                 choice = in.nextInt();
  161.             }catch(InputMismatchException e) { //doesnt allow any other input except for an int
  162.                 System.out.println("Please Enter A Valid Input");
  163.                 in.next();
  164.             }
  165.         }
  166.     }
  167.    
  168.  
  169.     public void choiceApp(){ //gets what app the user wants to use
  170.         System.out.println("What do you want to do?");
  171.         System.out.println("1. Check existing websites and passwords");
  172.         System.out.println("2. Add new website and password");
  173.         System.out.println("3. Change website name and/or password");
  174.         System.out.println("4. Change login information");
  175.         System.out.println("5. Exit");
  176.         while (choice <= 0 || choice >= 6){ //ensures input is within 1-5
  177.             System.out.println("Please enter the corresponding number for what you want to do");
  178.             try {
  179.                 choice = in.nextInt();
  180.             }catch(InputMismatchException e) { //doesnt allow any inputs except for an int
  181.                 System.out.println("Please Enter A Valid Input");
  182.                 in.next();
  183.             }
  184.         }
  185.     }
  186.    
  187.  
  188.     public void checkWebpass(){ //app to check all websites and passwords
  189.         System.out.println("You have passwords saved for the following websites:");
  190.         for (int i = 0;(i < loggedInUser.webName.size());i++){ //prints full website list by iterating through the array
  191.             System.out.println((i+1)+". "+loggedInUser.webName.get(i)); // output EG. "1. Facebook"
  192.         }
  193.         choice = 0;
  194.         while (choice <= 0 || choice >= (loggedInUser.webName.size()+1)){ // ensures input is within 1-(amount of websites in array)
  195.             System.out.println("Please enter the corresponding number for what password you want to see");
  196.             try {
  197.                 choice = in.nextInt();
  198.             }catch(InputMismatchException e) { //doesnt allow inputs except for a int
  199.                 System.out.println("Please Enter A Valid Input");
  200.                 in.next();
  201.             }
  202.         }
  203.         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
  204.     }
  205.    
  206.  
  207.     public void addWebpass(){ //function to add a website and password
  208.         int i = 1; //interger used for iterating
  209.         String addNewWeb = null; //clears website and password
  210.         String addNewPass = null;
  211.         while (correctWebPass == false){
  212.             System.out.println("What is the name of the website you want to add?");
  213.             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
  214.             addNewWeb = in.nextLine(); //saves Website from user
  215.             System.out.println("What is the password for this site?");
  216.             addNewPass = in.nextLine(); //saves password from user
  217.             System.out.println("Is this information correct?");
  218.             System.out.println("Website: "+addNewWeb);
  219.             System.out.println("Password: "+addNewPass);
  220.             System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
  221.             confirm = in.nextLine();
  222.             if(confirm.equalsIgnoreCase("Yes")){//confirms whether input was yes
  223.                 correctWebPass = true;
  224.             }else{
  225.                 if(confirm.equalsIgnoreCase("No")){//confirms whether input was no
  226.                     correctWebPass = false;
  227.                 }else{ //if input is anything else
  228.                     correctWebPass = false;//boolean that ends/continues add website loop
  229.                 }
  230.             }
  231.         }
  232.         System.out.println("The following information has been stored");
  233.         System.out.println("Website: "+addNewWeb);
  234.         System.out.println("Password: "+addNewPass);
  235.         loggedInUser.webName.add(addNewWeb);
  236.         loggedInUser.webPass.add(addNewPass);
  237.         //System.out.println(userDatabase);
  238.     }
  239.    
  240.  
  241.     public void changeWebpass(){ //app that changes the website name or website password for a specific website
  242.         while (correctWebPass == false){ //loops so that the user can retry putting in the specifics
  243.             System.out.println("What website do you want to change information for?");
  244.             for (int x = 0;(x < loggedInUser.webPass.size());x++){ //prints a line of all the websites and passwords in the arraylist
  245.                 System.out.println((x+1)+". "+loggedInUser.webName.get(x)+", Password: "+loggedInUser.webPass.get(x));
  246.             }
  247.  
  248.             choice = 0;
  249.             while (choice <= 0 || choice >= (loggedInUser.webName.size()+1)){ //ensures user inputs a number that is relative to the websites in the list
  250.                 System.out.println("What is the number of the data set you want to change?");
  251.                 try {
  252.                     choice = in.nextInt(); //gets choice that user wants
  253.                 }catch(InputMismatchException e) {
  254.                     System.out.println("Please Enter A Valid Input"); //checks whether input was an input other than an int
  255.                     in.next();
  256.                 }
  257.             }
  258.             System.out.println("You have chosen to change the "+loggedInUser.webName.get(choice-1)+" login"); //displays website that was chosen
  259.  
  260.             choice2 = 0; //resets the chocie for username or password
  261.             while (choice2 <= 0 || choice2 >= 3){ //ensures user inputs an int (1 or 2)
  262.                 System.out.println("Do you want to change the name (1) or password (2) for "+loggedInUser.webName.get(choice-1));
  263.                 try {
  264.                     choice2 = in.nextInt();
  265.                 }catch(InputMismatchException e) { //catches whether input is not an int
  266.                     System.out.println("Please Enter A Valid Input");
  267.                     in.next();
  268.                 }
  269.             }
  270.  
  271.             if (choice2 == 1){webOrPass = "Name";} //sets variable to name or password so it can be used below
  272.             if (choice2 == 2){webOrPass = "Password";}
  273.             System.out.println("You have chosen to change the "+webOrPass+" of the website"); //prints what they've chosen
  274.             System.out.println("What do you want to change the "+webOrPass+ " to?"); //asks what they want to change the password to
  275.             in.nextLine();newInfo = in.nextLine(); //clears scanner then gets the new name or password
  276.  
  277.             System.out.println("Is this information correct?");
  278.             if (choice2 == 1){ //prints new website name and password
  279.                 System.out.println("Website: "+newInfo);
  280.                 System.out.println("Password: "+loggedInUser.webPass.get(choice-1));
  281.             }
  282.             if (choice2 == 2){//prints website name and new password
  283.                 System.out.println("Website: "+loggedInUser.webName.get(choice-1));
  284.                 System.out.println("Password: "+newInfo);
  285.             }
  286.             System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
  287.             confirm = in.nextLine(); // gets whether they have put in the correct info
  288.             if(confirm.equalsIgnoreCase("Yes")){ //if answer is yes end the loop
  289.                 correctWebPass = true;
  290.             }else{
  291.                 if(confirm.equalsIgnoreCase("No")){ //if answer is no redo the loop
  292.                     correctWebPass = false;
  293.                 }else{//else redo the loop
  294.                     correctWebPass = false;
  295.                 }
  296.             }
  297.         }
  298.         System.out.println("The following information has been stored");
  299.         if (choice2 == 1){//sets website name to the new one
  300.             loggedInUser.webName.set(choice-1, newInfo);
  301.         }
  302.         if (choice2 == 2){//sets website password to the new one
  303.             loggedInUser.webPass.set(choice-1, newInfo);
  304.         }
  305.         System.out.println("Website: "+loggedInUser.webName.get(choice-1)); //prints new info
  306.         System.out.println("Password: "+loggedInUser.webPass.get(choice-1));
  307.     }
  308.    
  309.  
  310.     public void changeLogin(){
  311.         correctUserPass = false;
  312.         while (correctUserPass == false){
  313.             choice = 0;
  314.             while (choice <= 0 || choice >= 3){
  315.                 System.out.println("Do you want to change your email (1) or password (2)");
  316.                 try {
  317.                     choice = in.nextInt();
  318.                 }catch(InputMismatchException e) {
  319.                     System.out.println("Please Enter A Valid Input");
  320.                     in.next();
  321.                 }
  322.             }
  323.             if (choice == 1){nameOrPass = "Email";}
  324.             if (choice == 2){nameOrPass = "Password";}
  325.  
  326.             System.out.println("You have chosen to change your " +nameOrPass);
  327.  
  328.             System.out.println("What do you want to change your "+nameOrPass+ " to?");
  329.             newInfo = in.nextLine();
  330.  
  331.             System.out.println("Is this information correct?");
  332.             if (choice2 == 1){
  333.                 System.out.println("Email: "+newInfo);
  334.                 System.out.println("Password: "+loggedInUser.password);
  335.             }
  336.             if (choice2 == 2){
  337.                 System.out.println("Email: "+loggedInUser.email);
  338.                 System.out.println("Password: "+newInfo);
  339.             }
  340.             System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
  341.             confirm = in.nextLine();
  342.             if(confirm.equalsIgnoreCase("Yes")){
  343.                 correctUserPass = true;
  344.             }else{
  345.                 if(confirm.equalsIgnoreCase("No")){
  346.                     correctUserPass = false;
  347.                 }else{
  348.                     correctUserPass = false;
  349.                 }
  350.             }
  351.         }
  352.         System.out.println("The following information has been stored");
  353.         if (choice2 == 1){
  354.             loggedInUser.email = newInfo;
  355.         }
  356.         if (choice2 == 2){
  357.             loggedInUser.password = newInfo;
  358.         }
  359.         System.out.println("Email: "+loggedInUser.email);
  360.         System.out.println("Password: "+loggedInUser.password);
  361.         //System.out.println(userDatabase);
  362.     }
  363.    
  364.  
  365.     public void exit(){
  366.         System.out.println("Are you sure you want to exit?");
  367.         System.out.println("Yes or No? (Any other input other than 'Yes' or 'No' will be automatically counted as a 'No')");
  368.         confirm = in.nextLine();
  369.         confirm = in.nextLine();
  370.         if(confirm.equalsIgnoreCase("Yes")){
  371.             exit = true;
  372.         }else{
  373.             if(confirm.equalsIgnoreCase("No")){
  374.                 exit = false;
  375.             }else{
  376.                 exit = false;
  377.             }
  378.         }
  379.     }
  380.  
  381.     public static void main(String [] orgs) {
  382.         Start start = new Start();
  383.         //window();
  384.  
  385.         System.out.println("Testing User credentials:");
  386.         System.out.println("Email: Test@test.com");
  387.         System.out.println("Password: Test1!");
  388.         start.loadArrays();
  389.         //start.delNullUsers();
  390.         while (loginCorrect == false){
  391.             System.out.println(start.userDatabase);
  392.             start.choice = 0;
  393.             start.choiceLoginRego();
  394.             if(start.choice == 1){start.login();}
  395.             if(start.choice == 2){start.register();start.saveArray();start.loadArrays();}
  396.             //start.delNullUsers();
  397.         }
  398.         while(start.exit == false){
  399.             start.choice = 0;
  400.             start.choiceApp();
  401.             if(start.choice == 1){start.checkWebpass();}
  402.             if(start.choice == 2){start.addWebpass();}
  403.             if(start.choice == 3){start.changeWebpass();}
  404.             if(start.choice == 4){start.changeLogin();}
  405.             if(start.choice == 5){start.exit();}
  406.             start.saveArray();
  407.         }
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement