Guest User

Untitled

a guest
Dec 8th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. public class PasswordKeeper{
  2. private ArrayList<SuperUser> users;
  3. private static Scanner keyboard = new Scanner(System.in);
  4.  
  5. public PasswordKeeper(){
  6. users = new ArrayList();
  7. }
  8.  
  9. public void login(){
  10. try{
  11. //readObject
  12. get();
  13. }catch(EOFException a){
  14. System.out.println("You are the First User!");
  15. }catch(IOException b){
  16. System.out.println(b);
  17. }
  18. catch(ClassNotFoundException c){
  19. System.out.println(c);
  20. }
  21. boolean loopDisplay = true;
  22. while(loopDisplay){
  23. System.out.println("1. Loginn2. Create Accountn3. Exit");
  24. int answer = keyboard.nextInt();
  25. if(answer == 1){
  26. String existingUser = "";
  27. String existingPass = "";
  28. System.out.println("Enter a Username followed by password:");
  29. existingUser = keyboard.next();
  30. existingPass = keyboard.next();
  31. SuperUser temp = new SuperUser(existingUser,existingPass);
  32. System.out.println();
  33. if(users.contains(temp)){
  34. //viewing superUser method
  35. temp.display();
  36. try{
  37. System.out.println("Saving.");
  38. save(users);
  39. }catch(IOException e){
  40. System.out.println(e);
  41. }
  42. }
  43. }
  44. public void save(ArrayList<SuperUser> obj)throws IOException{
  45.  
  46. File file = new File("userInformation.dat");
  47. FileOutputStream fileOut = new FileOutputStream(file, false);
  48. BufferedOutputStream buffedOutput = new BufferedOutputStream(fileOut);
  49. ObjectOutputStream out = new ObjectOutputStream(buffedOutput);
  50. out.writeObject(obj);
  51. out.close();
  52. fileOut.close();
  53. }
  54. public ArrayList<SuperUser> get() throws IOException, ClassNotFoundException{
  55. FileInputStream fileIn = new FileInputStream("userInformation.dat");
  56. BufferedInputStream buffedInput = new BufferedInputStream(fileIn);
  57. ObjectInputStream in = new ObjectInputStream(buffedInput);
  58. users = (ArrayList<SuperUser>) in.readObject();
  59. in.close();
  60. fileIn.close();
  61. return users;
  62. }
  63. public class SuperUser implements Serializable{
  64. private String userName;
  65. private String password;
  66. private static Scanner keyboard = new Scanner(System.in);
  67. private ArrayList<LoginInfo> info = new ArrayList();
  68.  
  69.  
  70. public SuperUser(String name, String pass){
  71. userName = name;
  72. password = pass;
  73. }
  74. public String getUser(){
  75. return userName;
  76. }
  77. public void display(){
  78.  
  79. int count = 1;
  80. if(info.size() == 0){
  81. System.out.println("You don't have any accounts saved yet!" +
  82. "nGo and add some!");
  83. }else{
  84. for(LoginInfo i: info){
  85. System.out.println(count + " " + i.getSite());
  86. count ++;
  87. }
  88. }
  89. System.out.println();
  90. System.out.println("Add,Delete or Logout " + info.size());
  91.  
  92. String next = keyboard.next();
  93. if(next.equalsIgnoreCase("add")){
  94. add();
  95. }else if(next.equalsIgnoreCase("delete")){
  96. delete();
  97. }else if(numberCheck(next)){
  98. int choice = (int) Integer.parseInt(next) - 1;
  99. edit(choice);
  100. }else if(next.equalsIgnoreCase("logout")){
  101. System.out.println(info.size());
  102. }
  103.  
  104. }
  105. public boolean numberCheck(String in){
  106. try{
  107. Integer.parseInt(in);
  108. }catch(NumberFormatException e){
  109. return false;
  110. }
  111. return true;
  112. }
  113. public void add(){
  114. System.out.println("What is the website name?:");
  115. String trash = keyboard.nextLine();
  116. String webName = keyboard.nextLine();
  117. System.out.println("The Username?:");
  118. String webUsername = keyboard.nextLine();
  119. System.out.println("The Password?:");
  120. String webPass = keyboard.nextLine();
  121. info.add(new LoginInfo(webUsername, webPass, webName));
  122. System.out.println(info.size());
  123. System.out.println("Application added!");
  124. display();
  125. }
  126. public void delete(){
  127. System.out.println("Which account # would you like to delete?");
  128. int userDelete = keyboard.nextInt();
  129. info.remove(userDelete - 1);
  130. System.out.println("Account removed.");
  131. display();
  132. }
  133.  
  134. public void edit(int index){
  135. System.out.println(info.get(index));
  136. System.out.println("Would you like to change the information? y/n");
  137. String trash = keyboard.nextLine();
  138. String yes = keyboard.nextLine();
  139.  
  140. if(String.valueOf(yes.charAt(0)).equalsIgnoreCase("y")){
  141. System.out.println("Username?");
  142. String username = keyboard.nextLine();
  143. System.out.println("Password?");
  144. String password = keyboard.nextLine();
  145. info.set(index, new LoginInfo(username, password,info.get(index).getSite()));
  146. System.out.println("Information Updated!n");
  147. display();
  148. }else{
  149. display();
  150. }
  151. }
  152.  
  153. @Override
  154. public int hashCode(){
  155. return userName.hashCode();
  156. }
  157.  
  158. @Override
  159. public boolean equals(Object other){
  160. if(other instanceof SuperUser){
  161. SuperUser toCompare = (SuperUser) other;
  162. return this.userName.equals(toCompare.getUser());
  163. }
  164. return false;
  165. }
Add Comment
Please, Sign In to add comment