Problems implementing a method

0

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();
    }
    
asked by Roman345 19.12.2016 в 20:24
source

1 answer

2

I'm assuming (based on the comments of your class Platform ) that this is responsible for maintaining the user registry. By the same comments I see that there is a method in this class called findUser that receives as parameter an object of type String corresponding to the username of a user and that returns an object of type User when traversing all users in the ArrayList of that class. Based on this one possible solution is:

 public Message sendMessage(String to, String subject, String message) throws CompetitionException {  

    User receiver = platform.findUser(to);
    if (to == null) {
        throw new CompetitionException(CompetitionException.RECIPIENT_NOT_FOUND);
    }
    else if(receiver == null ) {
        throw new Exception(); //Aquí debes lanza el tipo de excepción apropiado de tu lógica de negocio
    }
    Message m = new Message(this, this, subject, message);
    return m;
}

Each object of type User is instantiated with at least one object of type Platform , according to the constructors you defined. Class Platform offers a method called findUser that according to the comments 'looks for a User from your username' and returns that object. According to the implementation, if the object exists it returns successfully, otherwise it returns null, so in the sendMessage method we can invoke it and verify if it has returned null, in which case we know that the user does not exist and we throw the exception appropriate.

You mention in the questions that you finally delegate the sending of the message in the method sendMessage of the class Platform , but I do not find evidence that it is this way, even, by the signature of this method I do not find sense to that instancies an object of type Message . Even in the signature of this method you can see that you do not even use the object of type User created. I can suggest that you change the signature of the method sendMessage of class Platform to accept two objects of type User (who sends and who receives) and an object of type Message (the message you build):

public Message sendMessage(User from, User to, String subject, Message message) throws CompetitionException {               
    //Implementación
} 

And that in the method sendMessage of the class User invoke this method (to make the delegation effective).

    
answered by 19.12.2016 / 20:47
source