Context : This is a continuation of the previous exercise. Now, users will be ab
ID: 3912258 • Letter: C
Question
Context: This is a continuation of the previous exercise. Now, users will be able to send emails to each other.
Important: You don't know how many emails each user will send, so you will work with a dynamicsized collection. There's no limit to how many emails can be sent.
You need to:
Create an Email class that will have the following attributes: from (this will be a User object), to (this will be another User object), message (a string with the content of the email). These values will be passed in the constructor. You must implement accessors and mutators for the attributes.
Modify your User class to add the following:
An attribute that is a collection of emails sent
A method named sendEmail, that receives an Email object. This method will add the email to the list of emails sent.
Note: Use the main method of the EmailSystem class to thoroughly test your program. In this main method, you can hard code the values to test your implementation. That is, you don't have to ask the user for values using the Scanner or JavaFX dialogs. Your attributes should all be private attributes.
Explanation / Answer
Solution:
###########Email.java
/**
*
* @author Your Name here
* File Name: Email.java
* Creation Date: Today's date
* Description: Small description here
*
*/
public class Email {
// Email attributes
private String from;
private String to;
private String message;
// Parameterized constructor to set the values to the fields while creation of an object
public Email(String from, String to, String message) {
this.from = from;
this.to= to;
this.message = message;
}
// Getters and Setters to access the data or to mutate them
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
########User.java
import java.util.ArrayList;
/**
*
* @author Your Name here
* File Name: User.java
* Creation Date: Today's date
* Description: Small description here
*
*/
public class User {
// User Attributes
static ArrayList<Email> emailSent = new ArrayList<Email>();
// sendEmail method to send the email/ store the email intoemailsent list
private static void sendEmail(Email email) {
emailSent.add(email);
}
// Driver program to test theFunctionalities
public static void main(String[] args) {
// First creating 2 emails by calling the constructor.
Email email1 = new Email("subratbehera94@g.com", "abc@xyz.com", "Hi! There");
Email email2 = new Email("subratkumar95@g.com", "abc@xyz.com", "Hi! There. I am here.");
// Now sending the emails
sendEmail(email1);
sendEmail(email2);
}
}
Note: For any doubt just comment me below, before giving any negative feedback. If you liked it then please do give a thumbs up.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.