Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Blog {
- /*
- Blog.java created by J.D. Horey for AP CS
- This class allows the user to make a Blog object which
- allows the user to make posts, likes, etc. just like
- on a mainstream blog.
- */
- private String username;
- private String password;
- private String currentPost;
- private int currentLikes;
- private int totalPosts;
- private int totalLikes;
- //Blog constructor (not with post)
- public Blog(String username, String password){
- this.username = username;
- this.password = password;
- this.currentPost = "";
- this.currentLikes = 0;
- this.totalPosts = 0;
- this.totalLikes = 0;
- }
- //Blog constructor (not with post)
- public Blog(String username, String password, String currentPost){
- this.username = username;
- this.password = password;
- this.currentPost = currentPost;
- this.currentLikes = 0;
- this.totalPosts = 1;
- this.totalLikes = 0;
- }
- //return username associated with the Blog
- public String getUsername(){
- return this.username;
- }
- //return current post text associated with the Blog
- public String getCurrentPost(){
- return this.currentPost;
- }
- //return current likes on the current post of the Blog
- public int getCurrentLikes(){
- return this.currentLikes;
- }
- //return total number of posts on the Blog
- public int getTotalPosts(){
- return this.totalPosts;
- }
- //return total number of likes on all of the posts on the Blog
- public int getTotalLikes(){
- return this.totalLikes;
- }
- //set current post on the Blog to a string
- public void setCurrentPost(String newPost){
- this.currentPost = newPost;
- this.totalPosts++;
- this.currentLikes = 0;
- }
- //likes the current post on the Blog
- public boolean likeCurrentPost(){
- //if there is no currentPost, don't add a like
- if(!this.currentPost.equals("")) {
- this.currentLikes++;
- this.totalLikes++;
- return true;
- }else{
- return false;
- }
- }
- //reset password by giving oldPassword and setting newPassword
- public boolean resetPassword(String oldPassword, String newPassword){
- //if the old password is correct, update to newPassword
- if(oldPassword.equals(this.password)){
- this.password = newPassword;
- return true;
- }
- return false;
- }
- //return average likes throughout all posts on the Blog
- public Double averageLikes(){
- return (double)this.totalLikes / (double)this.totalPosts;
- }
- //return a string of the latest post on the Blog
- //ex: "Bob: This is epic"
- public String toString(){
- return this.username + ": " + this.currentPost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement