Hello!
I have a program which resembles an online forum. It has message chains, messaged and messages can be replied. I have 2 classes: Chain and Message classes. Messages can be replies so message class has an LinkedList and if message gets replies, reply goes to list (a message can only reply to one message). Chain class also has LinkedList and it contains all messages, which don't have any replies. If new message gets added, it goes to list in chain class and if that message gets replied, that message goes to list in message class.
I have a small problem when I'm trying to add replies to messages using a method in Chain class. This problem does not occur if I do the same using Message class. Firstly, when I add new message to Chain, it goes to the list without problem and if I try to reply that message, it works as well. However, if I'm trying to reply a "reply" (message in message class LinkedList), it doesn't work. My program thinks that I'm trying to reply to the second message, which is reply to the first one.
I need this output:
message1
--message2 reply to message 1
----message3 reply to message 2
------message4 reply to message 3
----message5 reply to message 2
message6
lines stand for whitespace. Every reply gets indent of "--" and if reply is replies, "----" and so on.
Im currently getting this:
message1
--message2 reply to message 1
----message3 reply to message 2
----message4 reply to message 3
message6
Something goes wrong when I'm adding replies to messages, since my program can print properly 2 replies, so indent of "----", but after that it doesn't work.
My program:
I didin't add the printing method, since it works with just using message class. Problem is in that method in chain class. If I add new messages to the chain and add replies to those messages maually like this, everything works: Chain.addMessage(message1), message1.addReply(new message). I need to use my program in a way that I search message with ID from either the messages reply list or message list in chain. Thx in advance and sorry for poor englishpublic class Chain( private LinkedList messages; public void addReplyToMessage(int id, String comment){ // if there are messages in chain if (getMessages().size() > 0){ //lets iterate messages which are located in chain for (int i = 0; i < getMessages().size(); i++){ //Convert element(i) to Message object Message inChain = (Message)getMessages().element(i); //create new LinkedList to store all replies from messages LinkedList replies = new LinkedList(); //if searched id is matched in chain messagelist, create new message and add it to if (inChain.getId() == id){ Message m1 = new Message(comment); inChain.addReply(m1); // if message was not found from chains linkedlist //it is reply to a reply, and located in linked list linked to message }else{ //add replies from message to linkedlist object replies = inChain.getReply(); //iterate over replies for (int j = 0; j < replies.size(); j++){ Message reply = (Message)replies.element(j); //if reply id matches, add it to messages "reply" list if (reply.getId() == id); Message m2 = new Message(comment); reply.addReply(m2); } } } } } //returns linkedlist that has messages without replies public LinkedList getMessages(){ return messages; } //adds new message to chain public void addNewMessage(Message m){ messages.add(m); } }