I am implementing a sendMessage
method but I can not do what I want.
I have two classes one call User
:
import java.util.ArrayList;
import java.util.List;
public class User implements CompetitionListener {
private Platform platform;
private String username;
private String password;
private String fullName;
private List<Message> inbox;
private List<Message> outbox;
private List<Submission> submissions;
public User (Platform platform, String username, String password, String fullName) {
/**
* PR1 Ex 2.1: User constructor needed for user registration
*/
this.platform = platform;
this.username = username;
this.password = password;
this.fullName = fullName;
this.inbox = new ArrayList<Message>();
this.outbox = new ArrayList<Message>();
this.submissions = new ArrayList<Submission>();
}
public User (User obj) {
/**
* PR1 Ex 2.3: Implementation of the copy constructor
*/
this.platform = obj.platform;
this.username = obj.username;
this.password = obj.password;
this.fullName = obj.fullName;
this.inbox = obj.inbox;
this.outbox = obj.outbox;
this.submissions = obj.submissions;
}
public Boolean checkPassword(String password) {
/**
* PR1 Ex 2.2: Implementation of checkPassword, required by login
*/
return this.password.equals(password);
}
public Organizer asOrganizer() {
/**
* PR1 Ex 2.3: Create a new object for the Organizer Role
*/
return new Organizer(this);
}
public Participant asParticipant() {
/**
* PR1 Ex 2.3: Create a new object for the Participant Role
*/
return new Participant(this);
}
public String getUserName() {
/**
* PR1 Ex 2.1: Required by method findUser
*/
return this.username;
}
public String getFullName() {
/**
* PR1 Ex 2.1: Required by test
*/
return this.fullName;
}
public String toString() {
StringBuilder sb = new StringBuilder ();
sb.append(getFullName()).append("<").append(getUserName()).append(">");
return sb.toString();
}
public boolean equals(Object obj) {
/**
* PR1 Ex 2.2: Required by test
*/
if(obj==null) {
return false;
}
if (obj instanceof User) {
User user = (User) obj;
if (!this.username.equals(user.username) || !this.password.equals(user.password) || !this.fullName.equals(user.fullName)) {
return false;
}
// Additional checks can be added
} else {
return false;
}
return true;
}
public List<Message> getMessages() {
return null;
}
public Message sendMessage(String to, String subject, String message) throws CompetitionException {
if (to == null) {
throw new CompetitionException(CompetitionException.RECIPIENT_NOT_FOUND);
}
Message m = new Message(this, this, subject, message);
return m;
}
public List<Competition> myCompetitions() {
return null;
}
public List<Message> getInbox() {
return this.inbox;
}
public List<Message> getOutbox() {
return this.outbox;
}
public Platform getPlatform() {
return this.platform;
}
public void onNewEvaluation() {
}
public void onCompetitionClosed() {
}
}
And another call Platform
:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Platform {
private List<User> users;
private List<Competition> competitions;
public Platform() {
/**
* PR1 Ex 2.1: We need to initialize the list of users
*/
users = new ArrayList<User>();
}
private User findUser(String username) {
/**
* PR1 Ex 2.1: Implementation of method findUser to find a user in the list users by username
*/
User user = null;
Iterator<User> itr = this.users.iterator();
while(itr.hasNext() && user == null) {
User u = itr.next();
if(u.getUserName().equals(username)) {
user = u;
}
}
return user;
}
public User registerUser(String username, String password, String fullname) {
/**
* PR1 Ex 2.1: Register a new user, checking that it does not exist
*/
User newUser = null;
// Check if the user is new or already exists
User queryUser = findUser(username);
if(queryUser==null) {
newUser = new User(this, username, password, fullname);
this.users.add(newUser);
}
return newUser;
}
public User login(String username, String password) {
/**
* PR1 Ex 2.2: Login an already existing user
*/
User user = null;
// Find the user in the list of registered users
User queryUser = findUser(username);
// If the user exists, check the password
if(queryUser!=null && queryUser.checkPassword(password)) {
user = queryUser;
}
return user;
}
public Integer getNumUsers() {
/**
* PR1 Ex 2.1: Required for test, to check if a new user is registered
*/
return this.users.size();
}
public Integer getNumCompetitions() {
return null;
}
public Message sendMessage(User from, String to, String subject, String message) throws CompetitionException {
return null;
}
public void registerCompetition(Competition competition) {
}
public List<Competition> getOpenCompetitions() {
return null;
}
private void evaluateAll() {
}
public void run() {
// Simulates system call for evaluation
evaluateAll();
}
}
The case is that the User#sendMessage
method receives 3 parameters to
, subject
, message
and this message must be sent to its recipient. The method delegates sending to Platform#sendMessage
.
I want the system to check that the to
parameter that the email sends is correct (not null
and that exists in the platform), if an exception does not occur.
This part I have almost since because if null
I throw the exception but if it is not I do not know how to raise the search and see if it exists or not.
In the same way, it will be verified in the recipient that exists in the platform and that the past information is correct, if it does not throw an exception to you.
The first part I have it like this:
User#sendMessage
:
public Message sendMessage(String to, String subject, String message) throws CompetitionException {
if (to == null) {
throw new CompetitionException(CompetitionException.RECIPIENT_NOT_FOUND);
}
Message m = new Message(this, this, subject, message);
return m;
}
How do I find out if the to
already exists or not to throw the exception or continue with the message?
I add to the question that the sendMessage method of the PLATFORM class must pass these tests and that's why I declare it that way:
// Send a message from a not registered user
User u_test=new User(platform, "not_registered", "a password", "Unregistered User");
try {
u_test.sendMessage(username2, msg_subject1, msg_body1);
fail();
} catch (CompetitionException ce) {
assertEquals(ce.getMessage(), CompetitionException.SENDER_NOT_FOUND);
} catch (Exception e) {
fail();
}