Guest User

Untitled

a guest
Aug 31st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.85 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Collections;
  6. import java.util.Date;
  7. import java.util.Set;
  8. /**
  9. * Write a description of class Server here.
  10. *
  11. * A simple server, containing the most important information
  12. *
  13. *
  14. * @author (Peter De Wolf)
  15. * @version (22.04.2012)
  16. */
  17. public class Server
  18. {
  19. // A list with all the registered users.
  20. private ArrayList<User> users;
  21. // This field keeps track of the user who is logged in at the moment.
  22. private User currentUser;
  23. // This is a built in counter that keeps track of the unique id's for tweets, retweets and directmessages.
  24. private long uniqueID;
  25. // This is a list that stores all the tweets and retweets.
  26. private ArrayList<Message> tweetsAndRetweets;
  27. // This field has the maximum length of a tweet, by adjusting this number in the constructor, the changes will apply generally.
  28. private int maxTweetLength;
  29.  
  30. /**
  31. * Construct 2 new arraylists, 1 userobject, a unique ID and the maxtweetlength.
  32. */
  33. public Server()
  34. {
  35. users = new ArrayList<User>();
  36. currentUser = null;
  37. tweetsAndRetweets = new ArrayList<Message>();
  38. uniqueID = 1;
  39. maxTweetLength = 140;
  40.  
  41. }
  42.  
  43. /**
  44. * This method returns an integer. The integer returned is the maximum tweetlength that is allowed.
  45. */
  46. public int getMaxTweetLength()
  47. {
  48. return maxTweetLength;
  49. }
  50.  
  51. /**
  52. * This method adds a user-object to the user-list
  53. * @param userName: this is the username
  54. * @param fullName: this is the full name of the user
  55. * @param password: this is the password that the user will use to log in
  56. * If the username is not already taken, a new user will be registered.
  57. * @ return this method returns a boolean that will tell you if the user was registered succesfully.
  58. */
  59.  
  60. public boolean addUser(String userName, String fullName,String password)
  61. {
  62. User newUser = new User(userName, fullName, password);
  63. boolean uniqueNickName = true;
  64. for( User user : users)
  65. if( newUser.getNickName().equals( user.getNickName()))
  66. {
  67. uniqueNickName = false;
  68. }
  69.  
  70. if( uniqueNickName)
  71. {
  72. users.add(newUser);
  73. newUser.addFollowing(newUser);
  74. newUser.addFollower(newUser);
  75. }
  76.  
  77. return uniqueNickName;
  78. }
  79.  
  80. /**
  81. * This method is used to log in.
  82. * @param userName: This is the username of the user that wants to log in.
  83. * @param password: This is the password of the user that wants to log in.
  84. * If password and username match, the user will be logged in.
  85. * @ return this method returns a boolean that will tell you if the user succesfully logged in.
  86. */
  87.  
  88. public boolean logIn(String userName, String password)
  89. {
  90. boolean logInSuccesFull = false;
  91. boolean userExists = false;
  92. if ( currentUser == null )
  93. {
  94. for(User user: users)
  95. if( user.getNickName().equals(userName) && password.equals(user.getPassWord()))
  96. {
  97. currentUser = user;
  98. logInSuccesFull = true;
  99. userExists = true;
  100.  
  101. }
  102. }
  103.  
  104. return logInSuccesFull;
  105. }
  106.  
  107. /**
  108. * This method is used to set the currentUser to null, then there is nobody logged in.
  109. */
  110.  
  111. public void logOut()
  112. {
  113. currentUser = null;
  114. }
  115.  
  116. /**
  117. * This method is used to find all the users in the userdatabase which fullname or username contains a cetrain searchstring.
  118. * @param searchstring: This is the string that must be present in the username or fullname
  119. * @param numberOfResults: This integer is used to indicate the number of userNames that this method will return
  120. */
  121.  
  122. public List<String> searchUsers( String searchString, int numberOfResults)
  123. {
  124. List<String> searchResult = new ArrayList<String>();
  125. int index = 0;
  126. for( User user: users)
  127. {
  128. if( user.getNickName().contains(searchString) || user.getFullName().contains(searchString) && index < numberOfResults )
  129. {
  130. index++;
  131. searchResult.add(user.getNickName());
  132. }
  133. }
  134. return searchResult;
  135. }
  136.  
  137. /**
  138. * This method will return the usernames of all the people the currentuser is following.
  139. * @return A list with strings of people the current user is following.
  140. */
  141.  
  142. public List<String> getFriends()
  143. {
  144. List<String> friends = currentUser.getFriends();
  145. friends.remove(0);
  146. return friends;
  147. }
  148.  
  149. /**
  150. * This method will return the usernames of all the people that are following the current user.
  151. * @return A list with strings of the people that are following the current user.
  152. */
  153. public List<String> getFollowers()
  154. {
  155. List<String> followers = currentUser.getFollowers();
  156. followers.remove(0);
  157. return followers;
  158. }
  159.  
  160. /**
  161. * This method will remove a user from the list of users the currentuser is following.
  162. * @ param the username of the user that must be removed from the list;
  163. * @ return this method returns a boolean that will tell u if the action was succesfull.
  164. */
  165.  
  166. public boolean unfollow(String userName)
  167. {
  168. User personIWantToStopFollowing = null;
  169. boolean actionSuccesFull = false;
  170. for(User user: users)
  171. {
  172. if(user.getNickName().equals(userName) && userName != currentUser.getNickName())
  173. {
  174. personIWantToStopFollowing = user;
  175. }
  176. }
  177.  
  178. if( personIWantToStopFollowing != null)
  179. {
  180. actionSuccesFull = currentUser.unfollow(personIWantToStopFollowing);
  181. if(actionSuccesFull)
  182. {
  183. personIWantToStopFollowing.removeFollower(currentUser);
  184. }
  185. }
  186.  
  187. return actionSuccesFull;
  188. }
  189.  
  190. /**
  191. * This method will add the user with a certain username to the list of the people the current user is following.
  192. * @ param the username of the user that must be added to the list of friends.
  193. * @ return this method will return a boolean that will tell u if u started following the user
  194. */
  195.  
  196. public boolean follow(String userName)
  197. {
  198.  
  199. boolean following= false;
  200. ArrayList<String> friends = currentUser.getFriends();
  201.  
  202. for(String nickName: friends)
  203. {
  204. if( nickName.equals(userName))
  205. {
  206. following = true;
  207. }
  208. }
  209.  
  210. if(following == false)
  211. {
  212. for(User user: users)
  213. {
  214. if( user.getNickName().equals(userName))
  215. {
  216.  
  217. currentUser.addFollowing(user);
  218. user.addFollower(currentUser);
  219. following = true;
  220. }
  221. }
  222. }
  223.  
  224. return following;
  225. }
  226.  
  227. /**
  228. * This method is used to check the current user
  229. * @ return if there is no current user this method will return a string saying it, else, it will return a string containing the username of the current user.
  230. */
  231. public String getCurrentUser()
  232. {
  233. if( currentUser != null)
  234. {
  235. return currentUser.getNickName();
  236. }
  237. else
  238. {
  239. return null;
  240. }
  241. }
  242.  
  243. /**
  244. * This method will send a directmessage to one certain user.
  245. * @ param receiverusername is the name of the user that will get this private message.
  246. * @ param message is the content of the message that will be sent to the user
  247. * @ return this will return the messageID of the directmessage if the sending was succesfull, or 0 when the sending was not succesfull.
  248. */
  249. public long sendDirectMessage(String recieverUserName, String message)
  250. {
  251. User reciever = null;
  252. boolean recieverFollowsSender = false;
  253. boolean senderFollowsReciever = false;
  254.  
  255. for (User user: users)
  256. {
  257. if(user.getNickName().equals(recieverUserName))
  258. {
  259. reciever = user;
  260. }
  261. }
  262.  
  263. if( reciever != null)
  264. {
  265. for(String userName: reciever.getFollowers())
  266. {
  267. if (userName.equals(currentUser.getNickName()))
  268. {
  269. recieverFollowsSender = true;
  270. }
  271. }
  272. }
  273.  
  274. for(String userName: currentUser.getFollowers())
  275. {
  276. if(userName.equals(recieverUserName))
  277. {
  278. senderFollowsReciever = true;
  279. }
  280. }
  281.  
  282. if( recieverFollowsSender && senderFollowsReciever )
  283. {
  284. DirectMessage DM = new DirectMessage(uniqueID,message, currentUser.getNickName());
  285. reciever.addDirectMessage(DM);
  286. uniqueID++;
  287. return uniqueID-1;
  288. }
  289. else
  290. {
  291. return 0;
  292. }
  293.  
  294. }
  295.  
  296. /**
  297. * This method will return a userdescription of a user, including the username, fullname, amount of tweets, followers and friends.
  298. * @ param username is the name of the user we want the userdescription from.
  299. * @ return a string containing the userdescription of a user, including the username, fullname, amount of tweets, followers and friends.
  300. */
  301.  
  302. public String getUserDescription(String userName)
  303. {
  304. boolean userFound = false;
  305. String description = null;
  306. for(User user: users)
  307. {
  308. if(user.getNickName().equals(userName))
  309. {
  310. userFound = true;
  311. description = user.getDescription();
  312.  
  313. }
  314. }
  315.  
  316. return description;
  317. }
  318.  
  319. /**
  320. * This method gets the directmessagedescription
  321. * @ param messageID is the unique ID of the directmessage that is requested.
  322. * @ return this is a string containing all the important information of 1 directmessage.
  323. */
  324.  
  325. public String getDirectMessageDescription(long messageID)
  326. {
  327. return currentUser.getDirectMessageDescription(messageID);
  328. }
  329.  
  330. /**
  331. * This method will return a tweetdescription.
  332. * @ param tweetID is the unique ID of the tweet
  333. * @ return this is a string containing all the important information of 1 tweet.
  334. */
  335. public String getTweetDescription(long tweetID)
  336. {
  337. String description= "The tweet could not be found.";
  338. for( Message message : tweetsAndRetweets)
  339. {
  340. if(message.getMessageID() == tweetID)
  341. {
  342. description = message.getMessageDescription();
  343. }
  344. }
  345. return description;
  346. }
  347.  
  348. /**
  349. * This method will create a retweet, getting the original tweedID if the tweet you want to retweet is already a tweet, or returning the normal ID if you try to retweet is a tweet.
  350. * @param tweetID is the unique ID of the tweet you want to retweet
  351. * @return it returns a long, the unique tweetID of the
  352. */
  353.  
  354. public long createRetweet(long tweetID)
  355. {
  356. String content = null;
  357. long referenceID = 0;
  358. String originalAuthor = null;
  359. boolean retweetSuccesfull = false;
  360. Date originalTimeOfCreation = null;
  361. for(Message message: tweetsAndRetweets)
  362. {
  363. if( message.getMessageID() == tweetID)
  364. {
  365. content = message.getMessageContent();
  366. uniqueID++;;
  367. originalAuthor = message.getOriginalAuthor();
  368. referenceID = message.getReferenceID();
  369. originalTimeOfCreation = message.getOriginalMomentOfCreation();
  370. retweetSuccesfull = true;
  371.  
  372. }
  373. }
  374.  
  375. if(retweetSuccesfull == true)
  376. {
  377. Retweet newRetweet = new Retweet((uniqueID-1), content, currentUser.getNickName(), referenceID, originalAuthor, originalTimeOfCreation);
  378. currentUser.incrementAmountOfTweets();
  379. tweetsAndRetweets.add(newRetweet);
  380. return uniqueID-1;
  381. }
  382. else
  383. {
  384. return 0;
  385. }
  386.  
  387. }
  388.  
  389. /**
  390. * This method will create a new tweet.
  391. * @param message is the string that will be displayed in the tweet, the method automatically gets the username from the currentuser.
  392. * @return this method returns the uniqueID of the created tweet
  393. */
  394.  
  395. public long createNewTweet(String message)
  396. {
  397. if( message.length() <= maxTweetLength )
  398. {
  399. Tweet newTweet = new Tweet(uniqueID, message, currentUser.getNickName());
  400. tweetsAndRetweets.add(newTweet);
  401. uniqueID++;
  402. currentUser.incrementAmountOfTweets();
  403. return uniqueID-1;
  404. }
  405. else
  406. {
  407. return 0;
  408. }
  409. }
  410.  
  411. // vanaf hier: alle methodes die aanroep doen op sorteeralgorithme en het sorteeralgorithme
  412.  
  413. /**
  414. * This method give all the new directmessages in inversed chornological order, it does not need any parameters, but assumes that there is a user logged on.
  415. * @return This method will return a list with longs, the messageIDs of the directmessages.
  416. */
  417. public List<Long> getDirectMessages()
  418. {
  419. ArrayList<Message> sortingList = currentUser.getDirectMessages();
  420. int amountOfNewDirectMessages = sortingList.size();
  421. return sortTheList(sortingList, amountOfNewDirectMessages);
  422.  
  423. }
  424.  
  425. /**
  426. * This method will collect all the tweets 1 user created and will return them in an inverse choronological order. The amount of tweets is returned to a certain
  427. * amount of results.
  428. * @ param userName: This is the username of the user from whom we want to get the tweets.
  429. * @ param numberOfResults: This is the number of tweets that will finally be displayed.
  430. * @ return This returns a list with all the IDs of the tweets that will be printed.
  431. */
  432. public List<Long> getTweetsOfUser(String userName, int numberOfResults)
  433. {
  434. ArrayList<Message> listToBeSorted = new ArrayList<Message>();
  435.  
  436. for(Message message : tweetsAndRetweets)
  437. {
  438. if(message.getCreator().equals(userName))
  439. {
  440. listToBeSorted.add(message);
  441.  
  442. }
  443.  
  444. }
  445. return sortTheList(listToBeSorted, numberOfResults);
  446. }
  447.  
  448. /**
  449. * This method will search tweets that contain a certain searchstring
  450. * @param searchString: is the string that will be looked for in the tweets.
  451. * @param numberOfResults: is an integer that indicates how many searchresults maximum will be returned.
  452. * @return This method returns a list of uniqueIDs of the tweets that contain the searchstring, the length of the list is limited to the numberOfResults
  453. */
  454. public List<Long> searchTweets(String searchString, int numberOfResults)
  455. {
  456. ArrayList<Message> listToBeSorted = new ArrayList<Message>();
  457.  
  458. for(Message message: tweetsAndRetweets)
  459. {
  460. if( message.getMessageContent().contains(searchString))
  461. {
  462. listToBeSorted.add(message);
  463. }
  464. }
  465. return sortTheList(listToBeSorted, numberOfResults);
  466.  
  467. }
  468.  
  469. /**
  470. * This method gets all tweets of the users you are following, sorts them invers chronological and then return a list of longs.
  471. * @ param numberOfResults: This parameter indicates how many results will be returned
  472. * @ return a list of unique IDs.
  473. */
  474. public List<Long> getTimeLine(int numberOfResults)
  475. {
  476. ArrayList<Message> tweetsToBeSorted = new ArrayList<>();
  477. List<String> friends = currentUser.getFriends();
  478. for(Message message: tweetsAndRetweets)
  479. {
  480.  
  481. for(String name: friends)
  482. {
  483. if(name.equals(message.getCreator()))
  484. {
  485. tweetsToBeSorted.add(message);
  486.  
  487. }
  488. }
  489.  
  490. }
  491. return sortTheList(tweetsToBeSorted, numberOfResults);
  492.  
  493. }
  494.  
  495. /**
  496. * This method will sort any list it gets invers chronoligcally. It takes the list of tweets, calculates the timelapse, gives the 2 arraylists with timelapse and messageID's
  497. * to quicksort.
  498. * @ param listToBeSorted: this is a list of messages that will be sorted.
  499. * @ param numberOfResults: This integer determines how many results will be returned maximum.
  500. */
  501.  
  502. private ArrayList<Long> sortTheList( ArrayList<Message> listToBeSorted, int numberOfResults)
  503. {
  504. Date momentOfRequest = new Date();
  505. ArrayList<Long> idList = new ArrayList<>();
  506. ArrayList<Long> timelapseList = new ArrayList<>();
  507. ArrayList<Long> sortedList = new ArrayList<>();
  508. int index = 0;
  509. for( Message message : listToBeSorted)
  510. {
  511. long timelapse = momentOfRequest.getTime() - message.getMomentOfCreation().getTime();
  512. idList.add(message.getMessageID());
  513. timelapseList.add(timelapse);
  514. }
  515. sortedList = quickSort(timelapseList, idList, 0, (timelapseList.size()-1));
  516.  
  517. int listSize = sortedList.size();
  518. while(index < listSize)
  519. {
  520. if( index >= numberOfResults)
  521. {
  522. sortedList.remove(numberOfResults);
  523. }
  524. index++;
  525. }
  526. return sortedList;
  527. }
  528.  
  529. // quicksort + 3way partitition
  530.  
  531. /**
  532. * This method will sort the list it gets invers chronological. The list with timelapses gives the difference in time in milliseconds, so the smaller the timelapse, the
  533. * newer the tweet, and so it will be put first in the arraylist.
  534. * @param tijdslijst is a list containing the timelapses
  535. * @param idlijst is a list containing the id's of the tweets that must be sorted invers chronological
  536. * @param low is the integer that indicates from which point the list must be sorted
  537. * @param high is the integer that indicates untill which point the list must be sorted
  538. * @return This method returns the sorted list of id's.
  539. */
  540. private static ArrayList<Long> quickSort(ArrayList<Long> tijdlijst, ArrayList<Long> idlijst, int low, int high)
  541. {
  542.  
  543. if (low < high)
  544. {
  545. int j = partition( tijdlijst, idlijst, low, high);
  546. quickSort(tijdlijst, idlijst, low, j-1);
  547. quickSort(tijdlijst, idlijst, j+1, high);
  548.  
  549. }
  550.  
  551. return idlijst;
  552. }
  553.  
  554. /**
  555. * The partition method of quicksort, this method will in the arraylist compare elements and swap them accordingly, until the arraylist is divided into a pivot, a left part of the
  556. * arralist, containing smaller elements than the pivot, and a right part of the arraylist, containing elements bigger than the pivot.
  557. * @param tijdlijst is the arraylist with timestamps that will be sorted from small to big
  558. * @param idlijst is a list with ID's that will be swapped together with the timestamps, so the timestamps and id's from the 2 lists still belong together
  559. * @param low is the integer that indicates from which point the arraylist will be divided into a left, right part and a pivot.
  560. * @param high is the integer that indicates untill which point in the arraylist the elements will be compared and swapped.
  561. * @return this method returns an integer, i, that indicates at which point in the arraylist the pivot is at the moment, so the quicksort can recursif recall itself and sort
  562. * the arraylist from the beginning till 1 position under the pivot, and from 1 position above the pivot till the end of the list.
  563. */
  564. private static int partition(ArrayList<Long> tijdlijst, ArrayList<Long> idlijst, int low, int high)
  565. {
  566. Long x = tijdlijst.get(high);
  567. int i = low;
  568.  
  569. for( int j = low; j <= high-1; j++)
  570. {
  571. if(tijdlijst.get(j) <= x)
  572. {
  573. Collections.swap(tijdlijst, i, j);
  574. Collections.swap(idlijst, i, j);
  575. i++;
  576.  
  577. }
  578.  
  579. }
  580.  
  581. Collections.swap(tijdlijst, i, high);
  582. Collections.swap(idlijst, i, high);
  583. return i;
  584. }
  585.  
  586. // extras
  587. /**
  588. * This method will add a favourite tweet to the favourites list of the current user
  589. * @param id is the tweetID of the tweet that must be added to the favourites
  590. * @return succesFull is the boolean that will be returned, it indicates whether the action was succesfull or not.
  591. */
  592. public boolean addFavourite(Long id)
  593. {
  594. boolean succesFull = false;
  595. for(Message message: tweetsAndRetweets)
  596. {
  597. if(message.getMessageID() == id)
  598. {
  599. succesFull = true;
  600. currentUser.addFavourite(message);
  601. }
  602. }
  603.  
  604. return succesFull;
  605. }
  606.  
  607. /**
  608. * This method will return a list of ID, containing the id's of the favourites of a user.
  609. * @param userName is the username of the user whose favourites we try to get.
  610. * @param numberOfResults is the amount of favourites we want to see from the user.
  611. * @return A list containing the tweetID's of the favourites of the user.
  612. */
  613. public List<Long> getFavouritesOfUser(String userName, int numberOfResults)
  614. {
  615. User wantedUser = null;
  616. for(User user: users)
  617. {
  618. if(user.getNickName().equals(userName))
  619. {
  620. wantedUser = user;
  621. }
  622. }
  623.  
  624. return sortTheList(wantedUser.getFavourites(), numberOfResults);
  625. }
  626.  
  627. }
Add Comment
Please, Sign In to add comment