diff --git a/2020-2021/src/TD2/Admin.java b/2020-2021/src/TD2/Admin.java new file mode 100644 index 0000000..6b873b1 --- /dev/null +++ b/2020-2021/src/TD2/Admin.java @@ -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; + } + + +} \ No newline at end of file diff --git a/2020-2021/src/TD2/Forum.java b/2020-2021/src/TD2/Forum.java new file mode 100644 index 0000000..ba383f4 --- /dev/null +++ b/2020-2021/src/TD2/Forum.java @@ -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 = new ArrayList(); + private List lmsg = new ArrayList(); + + + 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 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 getLmsg() { + return this.lmsg; + } + + public List getLatestmsg(Member m) { + List l = new ArrayList(); + 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; + } + +} diff --git a/2020-2021/src/TD2/ForumManager.java b/2020-2021/src/TD2/ForumManager.java new file mode 100644 index 0000000..1308104 --- /dev/null +++ b/2020-2021/src/TD2/ForumManager.java @@ -0,0 +1,36 @@ +package TD2; + +import java.util.ArrayList; +import java.util.List; + +public class ForumManager { + + private List l; + + public ForumManager() { + l = new ArrayList(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 getListeForum() { + return this.l; + } + +} diff --git a/2020-2021/src/TD2/Internaute.java b/2020-2021/src/TD2/Internaute.java new file mode 100644 index 0000000..7b15374 --- /dev/null +++ b/2020-2021/src/TD2/Internaute.java @@ -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; + } +} diff --git a/2020-2021/src/TD2/Member.java b/2020-2021/src/TD2/Member.java new file mode 100644 index 0000000..cdf4b86 --- /dev/null +++ b/2020-2021/src/TD2/Member.java @@ -0,0 +1,58 @@ +package TD2; + +import java.util.ArrayList; +import java.util.List; + +public class Member { + + private String nom; + private List msg = new ArrayList(); + + public Member() { + this(null, null); + } + + public Member(String n) { + this(n, null); + } + + public Member(String n, List 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 l) { + System.out.println(l.toString()); + } + + public String getNom() { + return this.nom; + } + + public void setNom(String nom) { + this.nom = nom; + } + + public List getMsg() { + return msg; + } + + public void setMsg(List msg) { + this.msg = msg; + } + + @Override + public String toString() { + return "Membre " + this.getNom() + "\n"; + } +} diff --git a/2020-2021/src/TD2/Message.java b/2020-2021/src/TD2/Message.java new file mode 100644 index 0000000..3e93e00 --- /dev/null +++ b/2020-2021/src/TD2/Message.java @@ -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); + } +} diff --git a/2020-2021/src/TD2/MessageManager.java b/2020-2021/src/TD2/MessageManager.java new file mode 100644 index 0000000..ed43a8c --- /dev/null +++ b/2020-2021/src/TD2/MessageManager.java @@ -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 + } + +} diff --git a/2020-2021/tests/TD2/ForumTest.java b/2020-2021/tests/TD2/ForumTest.java new file mode 100644 index 0000000..b3acebb --- /dev/null +++ b/2020-2021/tests/TD2/ForumTest.java @@ -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"); + + + } + + + +}