Answer to Question #284858 in Java | JSP | JSF for User12345

Question #284858

You are required to develop a small chatting application where two or more friends can communicate each other through messages.

Create a class Message which has Date d, and message (string). Provide getters/setters, constructors, toString.

Create a class Friend having String name, String contact, email and ArrayList of Messages provide getters/setters, constructors, toString addMessage(Message m) method which will add new message to the list.

Provide following options to the user using JFrame.

1. Login which will help user login to the application.

2. View Friends (Display List of All Friends)

3. View Messages ( This should display all message of a Friend)

4. Send message (This should ask for friend name and message match the friend name and write that message to the array list).


1
Expert's answer
2022-01-05T01:40:19-0500
import java.util.ArrayList;

public class Friend {
    private String name;
    private String contact;
    private String email;
    private ArrayList<Message> messages;

    public Friend(String name, String contact, String email) {
        this.name = name;
        this.contact = contact;
        this.email = email;
        messages = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public ArrayList<Message> getMessages() {
        return messages;
    }

    public void setMessages(ArrayList<Message> messages) {
        this.messages = messages;
    }

    public void addMessage(Message message) {
        messages.add(message);
    }

    @Override
    public String toString() {
        return name  + " " + contact +" " + email + " " + messages;
    }
}



import java.util.Date;

public class Message {
    private Date date;
    private String message;

    public Message(Date date, String message) {
        this.date = date;
        this.message = message;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return date + " " + message;
    }

}

import javax.swing.*;

public class Main extends JFrame {

    public Main() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JButton login = new JButton("Login");
        JButton viewFriends = new JButton("View Friends");
        JButton viewMessages = new JButton("View Messages");
        JButton sendMessage = new JButton("Send Message");
        panel.add(login);
        panel.add(viewFriends);
        panel.add(viewMessages);
        panel.add(sendMessage);
        add(panel);
        setSize(400, 200);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    public static void main(String[] args) {
        new Main();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Bradley Lenehan
09.03.23, 14:38

Dear assignmentexpert.com admin, You always provide great insights.

Charla Marrone
15.02.23, 17:05

Dear assignmentexpert.com administrator, Your posts are always well received by the community.

Leave a comment

LATEST TUTORIALS
New on Blog