2016-12-19 8 views
-2

質問があります。アプリケーションを実装しています。クラスのユーザーのgetMessagesメソッドを返すようにしてください。 ユーザーが未読メッセージをすべて表示します。メソッドgetMessagesを実装する

ユーザーがメッセージを読むと、Messageクラスのreadメソッドを使用してメッセージを読み取ったものとしてマークします。

USERクラスは、私はUSERクラスのそして、getMessagesメソッドとの読み込みを実装する方法がわからないこの

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

} 

}

のようなものですMessageクラス

+0

に - あなたが正確に何を苦労していますか? (あなたの 'getStatus'メソッドはおそらくメッセージのステータスを保留に設定していないはずです) – UnholySheep

+0

この質問は不完全です。どのようにメッセージを保管していますか?データベースでは?ユーザーはどのようにメッセージを読んでいますか?これは何らかの種類のWebアプリですか?何らかのネイティブアプリですか?それを外から見ると...読者がメッセージをクリックして開くときにイベントハンドラを設定する必要があると思います。次に、メッセージが読み込まれた何らかの種類のストレージ(データベース、テキストファイルなど)にログインし、プログラムの状態を更新します。 –

答えて

0

メッセージクラスには、メッセージが読み込まれているかどうか情報を保持するフラグが必要です。 そのチェックに基づいて、未読/読み込みメッセージを見つけるためにリストをトラバースする必要があります。

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

私はこれを遵守していないため、論理的にはあなたの質問ごとに動作するので、構文エラーがある可能性があります。

0

あなたが尋ねていることを明確に理解していれば、 メソッドgetMessagesは、ユーザーが読んでいないすべてのメッセージのリストを返すようにしますか?

この場合、クラス内にメッセージのステータスを記憶するフラグを作成できます。このよう :

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; 
} 
関連する問題