Advertisement
Guest User

kels code

a guest
Feb 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. package classIsInSession;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Facebook{
  8.  
  9.  
  10. ArrayList<FacebookUser> fbArray= new ArrayList<FacebookUser>();
  11. transient Scanner myScan=new Scanner(System.in);
  12. transient FacebookUser currentUser= new FacebookUser(null, null);
  13. Facebook fbObject;
  14. ArrayList<FacebookUser> hugeFriends=new ArrayList<FacebookUser>(); //for getRecommendations()
  15. int r=0; //for getRecommendations()
  16. ArrayList<FacebookUser> groupFriends = new ArrayList<FacebookUser>(); //also for getR...
  17. FacebookUser split = new FacebookUser(null, null); //must i keep going? (getR...)
  18.  
  19. public Facebook() {
  20. // FacebookUser whatUser=new FacebookUser("auspicious","theOne");
  21. // hugeFriends.add(whatUser);
  22. }
  23.  
  24.  
  25. public void quit() {
  26. serialize(); //stores data
  27.  
  28. System.exit(0);
  29. }
  30.  
  31. public void serialize(){ //writes fbArray to a file
  32.  
  33. try{
  34. ObjectOutputStream ObjectOut= new ObjectOutputStream(new FileOutputStream("notFacebookUsers.dat", true));
  35. ObjectOut.writeObject(fbObject);
  36. ObjectOut.close();
  37.  
  38. }catch(IOException ioe){
  39. System.out.println("PC: I caught an IOException @serialize method!");
  40. ioe.printStackTrace();
  41. }
  42. }
  43.  
  44.  
  45. public void deSerialize(){ //puts info from file into fbObject
  46.  
  47. try {
  48. ObjectInputStream ObjectIn= new ObjectInputStream(new FileInputStream("notFacebookUsers.dat"));
  49. this.fbObject = (Facebook) ObjectIn.readObject();
  50. ObjectIn.close();
  51. } catch (ClassNotFoundException e1) {
  52. System.out.print("PC: Class not found.");
  53. } catch (FileNotFoundException e) {
  54. System.out.print("PC: File not found.");
  55. } catch(IOException ioe){
  56. System.out.println("PC: I caught an IOException @deSerialize()!");
  57. }
  58. }
  59. //ignore some code until driver
  60. public void listUsers() {
  61.  
  62. System.out.println("\tList Of Users");
  63. System.out.println(fbArray.toString());
  64. }
  65.  
  66. public void addUser() {
  67. String username;
  68. String password="";
  69. String passwordHint="";
  70.  
  71. System.out.println("Now adding a new user: ");
  72. System.out.println("Enter the new username: ");
  73. username=myScan.nextLine();
  74. String y = fbArray.toString();
  75.  
  76. if (y.contains(username)) {
  77. System.out.println("User already exists.");
  78. }
  79. else {
  80. System.out.println("Enter the new password: ");
  81. password=myScan.nextLine();
  82. System.out.println("Enter the new password hint: ");
  83. passwordHint=myScan.nextLine();
  84.  
  85. FacebookUser user=new FacebookUser(username, password);
  86. user.setPasswordHint(passwordHint);
  87.  
  88. System.out.println("...\nCreated new user: "+user.getUsername()+", Password: "+user.getPassword()+", Hint: "+user.getPasswordHint());
  89. System.out.println("Please save this information for your future.\n");
  90. fbArray.add(user);
  91. }
  92.  
  93. }
  94.  
  95. public void deleteUser() {
  96.  
  97. System.out.println("Enter the username of the account you want to delete: ");
  98. String username=myScan.nextLine();
  99.  
  100. for(int i=0;i<=fbArray.size();i++) {
  101. String y = fbArray.get(i).getUsername();
  102.  
  103. if(!y.equals(username)) { }//do nothing until after for loop
  104.  
  105. if(y.equals(username)) {
  106. System.out.println("Enter your password: ");
  107. String password=myScan.nextLine();
  108. Boolean k=fbArray.get(i).checkPassword(password);
  109. if(k) {
  110. fbArray.remove(i);
  111. System.out.println("User successfully removed.");
  112. Driver.menu();
  113. }
  114. }
  115. }
  116. System.out.println("That username doesn't exist.");
  117. }
  118.  
  119. public void getPasswordHint() {
  120.  
  121. System.out.println("Enter your username for password help: ");
  122. String username=myScan.nextLine();
  123.  
  124. for(int i=0;i<=fbArray.size();i++) { //checks each user name
  125. String y = fbArray.get(i).getUsername();
  126.  
  127. if (y.equals(username)) { //if the username exists
  128. fbArray.get(i).getPasswordHelp();
  129. Driver.menu();
  130. } if (!y.equals(username)) { } //do nothing until after for loop
  131.  
  132. }
  133. System.out.println("That username doesn't exist.");
  134. }
  135.  
  136. ==============================================================================
  137. DRIVER
  138.  
  139. public class Driver {
  140. static Scanner myScan=new Scanner(System.in);
  141. static Facebook theOne=new Facebook();
  142.  
  143. public static void main(String[] args) {
  144.  
  145. theOne.deSerialize();
  146.  
  147. menu();
  148.  
  149. }
  150.  
  151. public static void menu() {
  152.  
  153. System.out.println("\nMenu");
  154. System.out.println("\t1. List Users");
  155. System.out.println("\t2. Add a user");
  156. System.out.println("\t3. Delete a user");
  157. System.out.println("\t4. Get password hint");
  158. System.out.println("\t5. Friend Someone");
  159. System.out.println("\t6. De-Friend Somone");
  160. System.out.println("\t7. List Friends");
  161. System.out.println("\t8. Recommend New Friends");
  162. System.out.println("\t9. Quit");
  163.  
  164. try {
  165. int choice=0;
  166. System.out.println("\nWhat would you like to do?(1-9)");
  167. choice=myScan.nextInt();
  168. myScan.nextLine();
  169.  
  170. switch (choice){
  171. case 0: System.out.println("Invalid choice, please try again.\n");
  172. System.exit(0);
  173. break;
  174. case 1:
  175. theOne.listUsers();
  176. break;
  177. case 2:
  178. theOne.addUser();
  179. break;
  180. case 3:
  181. theOne.deleteUser();
  182. break;
  183. case 4:
  184. theOne.getPasswordHint();
  185. break;
  186. case 5:
  187. theOne.addFriend();
  188. break;
  189. case 6:
  190. theOne.removeFriend();
  191. break;
  192. case 7:
  193. theOne.listFriends();
  194. break;
  195. case 8:
  196. theOne.recommendFriends();
  197. break;
  198. case 9:
  199. System.out.println("Goodbye.");
  200. myScan.close();
  201. theOne.quit(); //saves users to file and closes
  202. default: System.out.println("Invalid choice, please try again.\n");
  203. break;
  204. }
  205. }
  206. catch(InputMismatchException ex){
  207. System.out.println("Invalid choice, please enter an integer.\n");
  208. }
  209.  
  210. Driver.menu();
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement