"fin seance 16-09-2020"

This commit is contained in:
JunkJumper 2020-09-16 16:24:48 +02:00
parent ae2f8fc4f6
commit 53fbb5340a
8 changed files with 445 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package TD2;
public class Admin {
private String nom;
private Forum forum;
public Admin() {
this(null,null);
}
public Admin(String n, Forum f) {
this.setNom(n);
this.forum = f;
}
public void knowMembers() {
System.out.println(this.forum.getMembers().toString());
}
public int knowMembersCount() {
return this.forum.getMembers().size();
}
public Forum getForum() {
return this.forum;
}
public void addForum(Forum forum) {
this.forum = forum;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nom == null) ? 0 : nom.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Admin other = (Admin) obj;
if (nom == null) {
if (other.nom != null)
return false;
} else if (!nom.equals(other.nom))
return false;
return true;
}
}

View File

@ -0,0 +1,140 @@
package TD2;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Forum {
private String nom;
private Admin admin;
private MessageManager mg;
private ForumManager fm;
private List<Member> member = new ArrayList<Member>();
private List<Message> lmsg = new ArrayList<Message>();
public Forum(String n) {
this(n, null);
}
public Forum(String n, MessageManager msgManager) {
this.setNom(n);
this.setMessageManager(msgManager);
}
public Message createMessage(String content, Member auth) {
return this.getMessageManager().createMessage(content, auth);
}
public void addMessage(Message msg) {
this.lmsg.add(msg);
}
public void createUser(String i) {
boolean isAvaible = true;
for (Member m : this.getMembers()) {
if(m.getNom().equalsIgnoreCase(i)) {
System.err.println("Un utilisateur a déja prit ce nom");
isAvaible = !isAvaible;
}
}
if(isAvaible) {
this.addMember(new Member(i));
}
}
protected void addMember(Member m) {
this.getMembers().add(m);
}
public List<Member> getMembers() {
return this.member;
}
public Admin getAdmin() {
return this.admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public MessageManager getMessageManager() {
return this.mg;
}
private void setMessageManager(MessageManager msgManager) {
this.mg = msgManager;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public List<Message> getLmsg() {
return this.lmsg;
}
public List<Message> getLatestmsg(Member m) {
List<Message> l = new ArrayList<Message>();
for (Message msg : this.getLmsg()) {
if(msg.getCreationDate().compareTo(Date.from(Instant.now() )) <= 10) {
l.add(msg);
}
}
m.lireMessage(l);
return l;
}
public ForumManager getFm() {
return fm;
}
public void setFm(ForumManager fm) {
this.fm = fm;
}
@Override
public String toString() {
return this.getNom();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((admin == null) ? 0 : admin.hashCode());
result = prime * result + ((nom == null) ? 0 : nom.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Forum other = (Forum) obj;
if (admin == null) {
if (other.admin != null)
return false;
} else if (!admin.equals(other.admin))
return false;
if (nom == null) {
if (other.nom != null)
return false;
} else if (!nom.equals(other.nom))
return false;
return true;
}
}

View File

@ -0,0 +1,36 @@
package TD2;
import java.util.ArrayList;
import java.util.List;
public class ForumManager {
private List<Forum> l;
public ForumManager() {
l = new ArrayList<Forum>(1);
}
public Forum createForum(String s) {//On veut créer un forum en précisant le nom du forum.
boolean doesExist = false;
int index = -1;
for (int i = 0; i < l.size(); i++) {
if(l.get(i).getNom().equalsIgnoreCase(s) ) {
doesExist = !doesExist;
index = i;
}
}
if(!doesExist) { //Si le forum n'existe pas, on le créer.
return new Forum(s, new MessageManager());
} else { //Si un forum avec ce nom existe déjà, il ne se passe rien. (On renvoi le Forum déjà existant
return l.get(index);
}
}
public List<Forum> getListeForum() {
return this.l;
}
}

View File

@ -0,0 +1,14 @@
package TD2;
public class Internaute {
private String nom;
public Internaute(String n) {
this.nom = n;
}
public String getNom() {
return this.nom;
}
}

View File

@ -0,0 +1,58 @@
package TD2;
import java.util.ArrayList;
import java.util.List;
public class Member {
private String nom;
private List<Message> msg = new ArrayList<Message>();
public Member() {
this(null, null);
}
public Member(String n) {
this(n, null);
}
public Member(String n, List<Message> m) {
this.nom = n;
this.msg = m;
}
// Operations
public void envoyerMessage(Message m) {
this.msg.add(m);
}
public boolean effacerMessage(Message m) {
return this.nom.equalsIgnoreCase(m.getAuthor().getNom());
}
public void lireMessage(List<Message> l) {
System.out.println(l.toString());
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public List<Message> getMsg() {
return msg;
}
public void setMsg(List<Message> msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Membre " + this.getNom() + "\n";
}
}

View File

@ -0,0 +1,63 @@
package TD2;
import java.time.Instant;
import java.util.Date;
public class Message {
private String content;
private Member author;
private Date creationDate;
public Message() {
this(null);
}
public Message(String c) {
this(c, null);
}
public Message(String c, Member a) {
//Un message a un contenu et un'e aut'eur'rice
this.content = c;
this.setAuthor(a);
this.creationDate = Date.from(Instant.now());
}
@Override
public String toString() {
return "Message de <<" + this.getAuthor() + ">> : " + this.getContent() + "\n";
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Member getAuthor() {
return this.author;
}
public void setAuthor(Member author) {
this.author = author;
}
public boolean isOutOfDate(Date creationDate, int secondsElapsed) {
Date d = new Date();
long ms = d.getTime();
ms = ms - secondsElapsed*1000;
d.setTime(ms);
return creationDate.before(d);
}
}

View File

@ -0,0 +1,27 @@
package TD2;
public class MessageManager {
public MessageManager() {
}
public Message createMessage(String msg, Member auth) {
Message m = new Message(msg, auth);
auth.envoyerMessage(m);
return m;
}
public boolean deleteMessage(Message msg, Member m) {
if(m.effacerMessage(msg)) {
msg = null;
return true;
} else {
return false;
}
}
public void posterMessage() {
//TODO
}
}

View File

@ -0,0 +1,40 @@
package TD2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class ForumTest {
@Test
void scenCreateForum() throws InterruptedException {
//Un administrateur créer un forum en précisant le nom du forum. Il est alors administrateur du forum.
ForumManager fm = new ForumManager();
Admin stade = new Admin("oogle-stade", fm.createForum("OGCN"));
stade.getForum().setAdmin(stade);
assertEquals("oogle-stade", stade.getForum().getAdmin().getNom());
}
@Test
void scenCreateExistantForum() throws InterruptedException {
//Un administrateur créer un forum mais un forum avec ce nom existe déjà donc on renvoi le forum existant.
ForumManager fm = new ForumManager();
Forum abcd = new Forum("abcd");
Admin stade = new Admin("oogle-stade", fm.createForum("abcd"));
assertEquals(abcd, stade.getForum());
}
@Test
void internauteDevientMembre() throws InterruptedException {
ForumManager fm = new ForumManager();
Admin stade = new Admin("oogle-stade", fm.createForum("OGCN"));
stade.getForum().setFm(fm);
stade.getForum().createUser("unPseudo");
}
}