The project requires me to create an ArrayList of objects and then print the complete list. Additionally the maximum amount of characters cannot exceed 280. The objects created are defined in my "Message" class and the lists are defined and handled in my MessagingService class. I cant wrap my head around why the MessagingService class cannot return the complete ArrayList that is defined. It simply returns nothing. I have added two other printing methods to test whether it is successfully added to the list.
The Message class:
import java.util.Objects;
public class Message {
private String sender;
private String content;
public Message(String sender, String content) {
this.sender = sender;
this.content = content;
}
public String getSender() {
return sender;
}
public String getContent() {
return content;
}
public String toString() {
return this.sender + ": " + this.content;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Message other = (Message) obj;
if (!Objects.equals(this.sender, other.sender)) {
return false;
}
if (!Objects.equals(this.content, other.content)) {
return false;
}
return true;
}
}
The MessagingService class:
import java.util.ArrayList;
public class MessagingService {
private ArrayList<Message> messages;
public MessagingService() {
this.messages = new ArrayList<>();
}
public void add(Message message) {
if (message.toString().length() <= 280) {
this.messages.add(message);
}
}
public ArrayList<Message> getMessages() {
System.out.println("For each print method:");
for (Message message : messages) {
System.out.println(message);
}
System.out.println("");
System.out.println("Println complete list method:");
System.out.println(this.messages);
System.out.println("");
System.out.println("Return print method:");
return this.messages;
}
}
The Main class:
public class Main {
public static void main(String[] args) {
Message test1 = new Message("pieterer", "aaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbbaaaaaaaaaa1111111111bbbbbbbbbb");
Message test2 = new Message("koos", "kaaskril");
Message test3 = new Message("jan", "wikkelspies");
MessagingService list = new MessagingService();
System.out.println("");
list.add(test1);
list.add(test2);
list.add(test3);
list.getMessages();
}
}
The link to the exercise can be found here: https://java-programming.mooc.fi/part-6/1-objects-within-objects