Object Oriented Tests
For this challenge, you are going to build a mock commentssection.
Design
We're going to focus on two aspects:
Users
Users come in 3 flavors, normal users, moderators, and admins.Normal users can only create new comments, and edit the their owncomments. Moderators have the added ability to delete comments (toremove trolls), while admins have the ability to edit or delete anycomment.
Users can log in and out, and we track when they last loggedin
Comments
Comments are simply a message, a timestamp, and the author.
Comments can also be a reply, so we'll store what the parentcomment was.
Your Challenge
This is challenge is not about building a fully functional API,but more about focusing on the design from an object-orientedpoint-of-view.
We've provided the basic API (which is incomplete), which wewould like you to complete being aware of the followingObject-Oriented Programming concepts:
Encapsulation of Properties
The method-based API is provided. These must be completedas-is.
Additional methods are allowed, though remember to keep read-onlyproperties read-only.
Instantiation
Classes should be instantiated with properties (as provided), tocreate instances with values already assigned.
User/Moderator/Admin defaults:
Should be marked as not logged in
Should return null for the last logged in atproperty
Comment defaults:
Should set the current timestamp for the created atproperty upon instantiation
Replied To is optional, and should be null if notprovided.
Inheritance & Access Control
Note: for the sake of simplicity, you can simply treatobject equality as "equal", though more complete solutions willalso pass.
User
Users can be logged in and out.
When logging in, set the lastLoggedInAt timestamp. Do not modifythis timestamp when logging out
Users can only edit their own comments
Users cannot delete any comments
Moderator is a User
Moderators can only edit their own comments
Moderators can delete any comments
Admin is both a User and a Moderator
Admins can edit any comments
Admins can delete any comments
Composition
Comments contain a reference to the User who created it(author)
Comments optionally contain a reference to another comment(repliedTo)
When converting to a string (toString), the following format isused:
No replied to:«message» by «author.name»
With replied to:«message» by «author.name» (replied to«repliedTo.author.name»)
Beyond these basics, you are free to add to the API, but onlythese concepts will be scored automatically.
// code
import java.util.Date;
public class Solution {
public static class User {
public User(String name) {}
public boolean isLoggedIn() {}
public Date getLastLoggedInAt() {}
public void logIn() {}
public void logOut() {}
public String getName() {}
public void setName(String name) {}
public boolean canEdit(Comment comment) {}
public boolean canDelete(Comment comment) {}
}
public static class Moderator {
public Moderator(String name) {}
}
public static class Admin {
public Admin(String name) {}
}
public static class Comment {
public Comment(User author, String message) {}
public Comment(User author, String message, Comment repliedTo){}
public String getMessage() {}
public void setMessage(String message) {}
public Date getCreatedAt() {}
public User getAuthor() {}
public Comment getRepliedTo() {}
public String toString() {}
}
}
Object Oriented Tests For this challenge, you are going to build a mock comments section. Design We're going to focus on
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am