实现方法getMessages java

时间:2016-12-19 21:14:11

标签: java

我有一个问题,我正在实现一个应用程序,我想要我的方法getMessages类user,返回给我 用户列出的所有未读消息。

当用户阅读该消息时,它将使用Message类的read方法将其标记为已读。

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
{ 

User receiver = platform.findUser(to);
if (to == null) {
    throw new CompetitionException(CompetitionException.RECIPIENT_NOT_FOUND);
}
else if(receiver == null ) {
    throw new CompetitionException(CompetitionException.RECIPIENT_NOT_FOUND); //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;
}

public List<Competition> myCompetitions() {
    return null;
}

MESSAGE类

  import java.text.DateFormat;
 import java.util.Date;
 import java.text.SimpleDateFormat;

 public class Message {

private String subject;
private String message;
private MessageStatus status;
private Date createdAt;
private User to;
private User from;

public Message (User from, User to, String subject, String message) {
    this.to = to;
    this.from = from;
    this.subject = subject;
    this.message = message; 
    this.createdAt = new Date();
}

public String getSubject() {
    return subject;
}


public void setSubject(String subject) {
    this.subject = subject;
}

    public String getMessage () {
    return message;
}


public void setMessage(String message) {
    this.message = message;
}

public void read() {

}

public MessageStatus getStatus() {    
    status = MessageStatus.PENDING;
    return this.status;
}
public void setStatus(MessageStatus r) {
    this.status = r;
}

public Date getCreatedAt() {
    return createdAt;
}

public String toString() {                    
    StringBuilder sb = new StringBuilder(); 
    SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
    sb.append("{").append("date:").append("<").append(sdf.format(createdAt)).append(">,") 
        .append("from:").append("<").append(from).append(",").append("to:").append("<").append(to).append(",Subject:<").append(subject).append(">,status:<").append(getStatus()).append(">}");//append(price).append("}");

    return sb.toString();
}

public boolean equals(Object obj) { 
   if(obj==null) {
        return false;
    }        
    if (obj instanceof Message) {
        Message message = (Message) obj;
        if (!this.from.equals(message.from) || !this.to.equals(message.to) || !this.status.equals(message.status)|| !this.message.equals(message.message)|| !this.subject.equals(message.subject)) 

        {
            return false;
        }        
        // Additional checks can be added
    } else {
        return false;
    }

    return true;
}  


 }




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() {

}

}

我不知道如何实现USER类的getMessages方法和Message类的读取。

3 个答案:

答案 0 :(得分:0)

在你的消息类中,你应该有一个标志,如果消息被读取,它将保留信息。 在该检查的基础上,您应该遍历列表以找出未读/已读消息。

答案 1 :(得分:0)

public List<Message> getMessages() { 
        List<Message> temp = null;
        for(Message m:inbox)
        {
            if(m.getStatus.PENDING)
            {
                temp.add(m);
            }
        }
    return temp;
   }

可能有任何语法错误,因为我没有遵守这个,但逻辑上这可以按照您的问题工作。

答案 2 :(得分:0)

如果我清楚地了解你的要求, 您希望方法getMessages返回用户未读取的所有消息的列表吗?

如果是这种情况,您可以在Message类中创建一个标记,该标记将记住消息的状态。 像这样:

private boolean mReadFlag = false; //false - no, true - yes

...

//Will set the flag to read
public void read() {
    this.mReadFlag = true;
}

//Will return the read status
public boolean isRead(){
    return this.mReadFlag;
}

然后,在User

public List<Message> getMessages() {     
    List<Message> messageList = new ArrayList<Message>();

    for(Message message : YOUR_MESSAGE_LIST){
        if(!message.isRead()){
            messageList.add(message);
        }
    }

    return messageList;
}