Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. public class Blog {
  2.  
  3. /*
  4. Blog.java created by J.D. Horey for AP CS
  5.  
  6. This class allows the user to make a Blog object which
  7. allows the user to make posts, likes, etc. just like
  8. on a mainstream blog.
  9.  
  10. */
  11.  
  12. private String username;
  13. private String password;
  14. private String currentPost;
  15. private int currentLikes;
  16. private int totalPosts;
  17. private int totalLikes;
  18.  
  19. //Blog constructor (not with post)
  20. public Blog(String username, String password){
  21. this.username = username;
  22. this.password = password;
  23.  
  24. this.currentPost = "";
  25. this.currentLikes = 0;
  26. this.totalPosts = 0;
  27. this.totalLikes = 0;
  28. }
  29.  
  30. //Blog constructor (not with post)
  31. public Blog(String username, String password, String currentPost){
  32. this.username = username;
  33. this.password = password;
  34. this.currentPost = currentPost;
  35.  
  36. this.currentLikes = 0;
  37. this.totalPosts = 1;
  38. this.totalLikes = 0;
  39. }
  40.  
  41. //return username associated with the Blog
  42. public String getUsername(){
  43. return this.username;
  44. }
  45.  
  46. //return current post text associated with the Blog
  47. public String getCurrentPost(){
  48. return this.currentPost;
  49. }
  50.  
  51. //return current likes on the current post of the Blog
  52. public int getCurrentLikes(){
  53. return this.currentLikes;
  54. }
  55.  
  56. //return total number of posts on the Blog
  57. public int getTotalPosts(){
  58. return this.totalPosts;
  59. }
  60.  
  61. //return total number of likes on all of the posts on the Blog
  62. public int getTotalLikes(){
  63. return this.totalLikes;
  64. }
  65.  
  66. //set current post on the Blog to a string
  67. public void setCurrentPost(String newPost){
  68. this.currentPost = newPost;
  69. this.totalPosts++;
  70. this.currentLikes = 0;
  71. }
  72.  
  73. //likes the current post on the Blog
  74. public boolean likeCurrentPost(){
  75. //if there is no currentPost, don't add a like
  76. if(!this.currentPost.equals("")) {
  77. this.currentLikes++;
  78. this.totalLikes++;
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }
  84.  
  85. //reset password by giving oldPassword and setting newPassword
  86. public boolean resetPassword(String oldPassword, String newPassword){
  87. //if the old password is correct, update to newPassword
  88. if(oldPassword.equals(this.password)){
  89. this.password = newPassword;
  90. return true;
  91. }
  92. return false;
  93. }
  94.  
  95. //return average likes throughout all posts on the Blog
  96. public Double averageLikes(){
  97. return (double)this.totalLikes / (double)this.totalPosts;
  98. }
  99.  
  100. //return a string of the latest post on the Blog
  101. //ex: "Bob: This is epic"
  102. public String toString(){
  103. return this.username + ": " + this.currentPost;
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement