Answer to Question #285924 in Java | JSP | JSF for Nayab Gul

Question #285924

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-09T02:20:46-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

Pablo Foxall
11.03.23, 02:45

Hi assignmentexpert.com administrator, Thanks for the well-written and informative post!

Leave a comment

LATEST TUTORIALS
New on Blog