Answered Essay: You may use any Java API class.

You may use any Java API class.

You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.

The goal of this assignment is to write a program that manages an email system (like a stripped down version of Outlook) except that you can’t send and receive emails (naturally). However, this program will have all of the usual email features like deleting emails, creating new emails, moving emails to different folders, adding and removing emails, and the like.

1. Write a fully documented class named Email. This class will contain the following information about each email.

private String to
The String literal which stores the “to” field.

private String cc
The String literal which stores the “cc” field.

private String bcc
The String literal which stores the “bcc” field.

private String subject
The String literal which stores the “subject” field.

private String body
The String literal which stores all of the text in the email’s body.

private GregorianCalendar timestamp
Represents the time that this email was created.

The Email class should have a Constructor, mutator and accessor methods for each instance variable. In order for your program to be able to save Email objects to a file, all objects in it must be serializeable, sothis class must implement Serializable.

2. Write a fully documented class named Folder. This class represents an email folder and will handle all of the logic for adding and removing emails. It contains the following data values:

private ArrayList<Email> emails

Stores all of the emails contained in this folder.

private String name

Stores the name of the folder.

private String currentSortingMethod

Stores the current sorting method (however that you see fit) so that emails added can be properly sorted without having to first resort the folder. Notes: Default is date descending.

It also has the following methods (including the usual constructor and / methods):

public void addEmail(Email email)

Adds an email to the folder according to the current sorting method.

public Email removeEmail(int index)

Removes an email from the folder by index.

public void sortBySubjectAscending()

Sorts the emails alphabetically by subject in ascending order.

public void sortBySubjectDescending()

Sorts the emails alphabetically by subject in descending order.

public void sortByDateAscending()

Sorts the emails by date in ascending order.

public void sortByDateDescending()

Sorts the emails by date in descending order.

3. Write a fully documented class named Mailbox. This class represents an email box and it will contain all of the folders and the remaining logic. It contains the following data values:

private Folder inbox

Stores the inbox, which is a special folder which should never be allowed to be deleted or renamed. All new emails go here.

private Folder trash

Stores the trash, which is a special folder which should never be allowed to be deleted or renamed. When an email is deleted, it is moved here.

private ArrayList<Folder> folders

Stores all of the custom folders contained in the mailbox. (It can be any data collection,  is only an example)

public static Mailbox mailbox

Stores the main mailbox that will  by the application. This mailbox should be instantiated in the main method.

It also contains the following methods (and a main method):

public void addFolder(Folder folder)

Adds the given folder to the list of custom folders. Note: check to make sure a folder with that given name does not already exist. If it does, return an error in some manner.

public void deleteFolder(String name)

Removes the given folder from the list of custom folders

public void composeEmail()

Gets user input on the contents of the email and adds it to the inbox.

public void deleteEmail(Email email)

Moves the email to the trash. (This method shouldn’t remove any emails from folders, the method removeEmail should be called and then deleteEmail is called on the result)

public void clearTrash()

Removes all emails from the trash folder.

public void moveEmail(Email email, Folder target)

Takes the given email and puts in in the given folder. If the folder cannot be found, instead move it to the Inbox. (Again, this method shouldn’t remove any emails from folders, the method  should be called and then  is called on the result)

public Folder getFolder(String name)

Returns a folder by folder name.

 

4. When the program begins, it should see if the file “mailbox.obj” exists in the current directory. If so, it should initialize the mailbox with the data in this file using serialization. Otherwise, your program will start with an empty mailbox. Then, the program should provide the user with a menu with the following options.

A – Add folder

Prompts the user for folder name, and creates and adds a new folder with the specified name to the list of folders.

R – Remove folder

Prompts the user for folder name and removes the folder if a folder with the given name exists.

C – Compose email

Prompts the user for TO, CC, BCC, Subject, and then Body and creates a new email and adds it to the inbox.

F – View Folder

Prompts the user for folder name, if the folder is found, the folder submenu is opened for that specific folder which displays all of the emails in the folder.

I – View Inbox

Opens the folder submenu for the inbox.

T – View Trash

Opens the folder submenu for the trash.

E – Empty Trash

Empties the trash folder of all emails.

Q – Quit the program

Remember to save the contents of the mailbox to the file, “mailbox.obj”.

There is no option to load or save a file in the menu. This is because you should automatically search for a file named “mailbox.obj” upon startup, and save to the file when the user wishes to quit the program.

Folder submenu:

M – Move email

Prompts the user for email index and then displays a list of folders and asks the user to input the name of the folder to move to. If the specified folder was not found, cancel the entire operation. User should be able to move to Inbox, Trash, and the current folder.

D – Delete email

Prompts the user for email index and moves the email to the trash folder.

V – View email contents

Prompts the user for email index and prints the information about the corresponding email.

SA – Sort by subject ascending

Sorts the emails by subject in ascending order.

SD – Sort by subject descending

Sorts the emails by subject in descending order.

DA – Sort by date ascending

Sorts the emails by date in ascending order.

DD – Sort by date descending

Sorts the emails by date in descending order.

R – Return to main menu

4. Include exception classes or additional classes as needed.

OUTPUT FORMAT

Be sure to have a menu after each operation.

Display to the user any errors that seem appropriate at any point during execution of your program.

EXTRA CREDIT (12 points)

Use Graphical User Interface (GUI) for program input and display the results. (12 points)

NOTE: The GUI must include headers (like the “Subject”, “Date” headers in your average mailbox system) and these headers must be clickable to sort. Clicking a header switches from ascending to descending (and vice versa) and changes the sort parameter to whatever the header specifies. And rather than accessing emails by index, all emails should have two buttons (Delete and Move) next to it that the user uses to delete and move emails from folders.

i.e. If the current sorting method is Date Ascending and the user clicks on the Subject header, the new sorting method is Subject Descending.

SAMPLE COMPARABLE CODE

import java.util.*;
 
/*
 * An example of type abstraction that implements Comparable
 * and Comparator interfaces.
 *
 * The Comparable/Comparator interfaces provide a standard means
 * for communication with yet unknown types of objects.
*/
      
public class CollectionsTester {
         public static void main(String[] args) {
           ArrayList<Employee> staff = new ArrayList<Employee>();
                   
           staff.add(new Employee("Joe",100000, 177700010));
           staff.add(new Employee("Jane",200000, 111100010));
           staff.add(new Employee("Bob",66666, 1999000010));
           staff.add(new Employee("Andy",77777, 188800010));
      
           Collections.sort(staff);                                      // Sort by salary
           System.out.println("Lowest paid employee: "+staff.get(0));    // Prints Bob
           
           Collections.sort(staff, new NameComparator());                // Sort by aplahabetical order
           System.out.println("First employee in list: "+staff.get(0));  // Prints Andy
          
           Collections.sort(staff, new IdComparator());                  // Sort by ID number
           System.out.println("Employee with lowest ID: "+staff.get(0)); // Prints Jane
         }
}
 
public class Employee implements Comparable {
        private String name;
        private int salary;
        private int id;
        public Employee(String initName, int initSal, int initId) {
                id     = initId;
                name = initName;
                salary = initSal;
        }
        public String getName(){ return name; }
        public int getSalary() { return salary; }
        public int getId(){ return id; }
        public void setSalary(int newSalary) {
                salary = newSalary;
        }
        public int compareTo(Object o) {
                Employee otherEmp = (Employee)o;
                if (this.salary == otherEmp.salary)
                        return 0;
                else if (this.salary > otherEmp.salary)
                        return 1;
                else
                        return -1;
        }
        public String toString() {
                return name + ", $" + salary + ", "+ id;
        }
}
 
public class NameComparator implements Comparator {
        public int compare(Object o1, Object o2) {
                Employee e1 = (Employee) o1;
                Employee e2 = (Employee) o2;
                return (e1.getName().compareTo(e2.getName()));
        }
}
 
public class IdComparator implements Comparator {
       public int compare(Object o1, Object o2) {
           Employee e1 = (Employee) o1;
           Employee e2 = (Employee) o2;
           if (e1.getId() == e2.getId())
               return 0;
           else if (e1.getId() > e2.getId())
               return 1;
           else
               return -1;
       }
}

EXAMPLES

ObjectInputStream.readObject():
MyClass myObject; //MyClass implements Serializable
try {
//If file is found, open it
FileInputStream   file = new FileInputStream(“mySaveFile.obj”);
ObjectInputStream fin = new ObjectInputStream(file);
myObject = (MyClass) fin.readObject();
file.close();
} catch(IOException a){}
catch(ClassNotFoundException c){} //Bottoms up!
//Note that myObject may still be null

ObjectOutpurStream.writeObject():
try {
FileOutputStream   file = new FileOutputStream(“mySaveFile.obj”);
ObjectOutputStream fout = new ObjectOutputStream(file);
fout.writeObject(myObject);
fout.close();
} catch(IOException a) { /*Error*/ }

System.out.printf():
System.out.printf(“%4d %4d %4d %-40s”, 1, 2, 3, “A string”);
System.out.printf(“%4d %4d %4d %-40s”, 10, 20, 30, “A longer string”);
System.out.printf(“%4d %4d %4d %-40s”, 10, 100, 1000, “Very longer than previous strings”);
//output:
//   1    2    3 A string
// 10   20   30 A longer string
// 10 100 1000 Very longer than previous strings

SAMEPL INPUT/OUTPUT

Comments in green. User input in black. Program output in blue.

Previous save not found, starting with an empty mailbox.

Mailbox:

——–

Inbox

Trash

A – Add folder

R – Remove folder

C – Compose email

F – Open folder

I – Open Inbox

T – Open Trash

Q – Quit

Enter a user option: A

Enter folder name: Other Folder

Mailbox:

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: C

Enter recipient (To): phillip.smith@stonybrook.edu, psmith@ic.sunysb.edu

Enter carbon copy recipients (CC): psmith@cs.sunysb.edu

Enter blind carbon copy recipients (BCC): phroph@yahoo.com

Enter subject line: Testing

Enter body: This is a test of the mailbox program!

Email successfully added to Inbox.

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: I

Inbox

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

M – Move email

D – Delete email

V – View email contents

SA – Sort by subject line in ascending order

SD – Sort by subject line in descending order

DA – Sort by date in ascending order

DD – Sort by date in descending order

R – Return to mailbox

Enter a user option: M

Enter the index of the email to move: 1

Folders:

Inbox

Trash

Other Folder

Select a folder to move “Testing” to: Other Folder

“Testing” successfully moved to Other Folder.

Inbox

Inbox is empty.

//Menu not included to save space.

Enter a user option: R

Mailbox:

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: F

Enter folder name: Other Folder

Other Folder

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

//Menu not included to save space.

Enter a user option: V

Enter email index: 1

To: phillip.smith@stonybrook.edu, psmith@ic.sunysb.edu

CC: psmith@cs.sunysb.edu

BCC: phroph@yahoo.com

Subject: Testing

This is a test of the mailbox program!

Other Folder

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

//Menu not included to save space.

Enter a user option: D

Enter email index: 1

“Testing” has successfully been moved to the trash.

Other Folder

Other Folder is empty.

//Menu not included to save space.

Enter a user option: R

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: E

1 item(s) successfully deleted.

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: Q

Program successfully exited and mailbox saved.

You may use any Java API class.

You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.

The goal of this assignment is to write a program that manages an email system (like a stripped down version of Outlook) except that you can’t send and receive emails (naturally). However, this program will have all of the usual email features like deleting emails, creating new emails, moving emails to different folders, adding and removing emails, and the like.

1. Write a fully documented class named Email. This class will contain the following information about each email.

private String to
The String literal which stores the “to” field.

private String cc
The String literal which stores the “cc” field.

private String bcc
The String literal which stores the “bcc” field.

private String subject
The String literal which stores the “subject” field.

private String body
The String literal which stores all of the text in the email’s body.

private GregorianCalendar timestamp
Represents the time that this email was created.

The Email class should have a Constructor, mutator and accessor methods for each instance variable. In order for your program to be able to save Email objects to a file, all objects in it must be serializeable, sothis class must implement Serializable.

2. Write a fully documented class named Folder. This class represents an email folder and will handle all of the logic for adding and removing emails. It contains the following data values:

private ArrayList<Email> emails

Stores all of the emails contained in this folder.

private String name

Stores the name of the folder.

private String currentSortingMethod

Stores the current sorting method (however that you see fit) so that emails added can be properly sorted without having to first resort the folder. Notes: Default is date descending.

It also has the following methods (including the usual constructor and / methods):

public void addEmail(Email email)

Adds an email to the folder according to the current sorting method.

public Email removeEmail(int index)

Removes an email from the folder by index.

public void sortBySubjectAscending()

Sorts the emails alphabetically by subject in ascending order.

public void sortBySubjectDescending()

Sorts the emails alphabetically by subject in descending order.

public void sortByDateAscending()

Sorts the emails by date in ascending order.

public void sortByDateDescending()

Sorts the emails by date in descending order.

3. Write a fully documented class named Mailbox. This class represents an email box and it will contain all of the folders and the remaining logic. It contains the following data values:

private Folder inbox

Stores the inbox, which is a special folder which should never be allowed to be deleted or renamed. All new emails go here.

private Folder trash

Stores the trash, which is a special folder which should never be allowed to be deleted or renamed. When an email is deleted, it is moved here.

private ArrayList<Folder> folders

Stores all of the custom folders contained in the mailbox. (It can be any data collection,  is only an example)

public static Mailbox mailbox

Stores the main mailbox that will  by the application. This mailbox should be instantiated in the main method.

It also contains the following methods (and a main method):

public void addFolder(Folder folder)

Adds the given folder to the list of custom folders. Note: check to make sure a folder with that given name does not already exist. If it does, return an error in some manner.

public void deleteFolder(String name)

Removes the given folder from the list of custom folders

public void composeEmail()

Gets user input on the contents of the email and adds it to the inbox.

public void deleteEmail(Email email)

Moves the email to the trash. (This method shouldn’t remove any emails from folders, the method removeEmail should be called and then deleteEmail is called on the result)

public void clearTrash()

Removes all emails from the trash folder.

public void moveEmail(Email email, Folder target)

Takes the given email and puts in in the given folder. If the folder cannot be found, instead move it to the Inbox. (Again, this method shouldn’t remove any emails from folders, the method  should be called and then  is called on the result)

public Folder getFolder(String name)

Returns a folder by folder name.

 

4. When the program begins, it should see if the file “mailbox.obj” exists in the current directory. If so, it should initialize the mailbox with the data in this file using serialization. Otherwise, your program will start with an empty mailbox. Then, the program should provide the user with a menu with the following options.

A – Add folder

Prompts the user for folder name, and creates and adds a new folder with the specified name to the list of folders.

R – Remove folder

Prompts the user for folder name and removes the folder if a folder with the given name exists.

C – Compose email

Prompts the user for TO, CC, BCC, Subject, and then Body and creates a new email and adds it to the inbox.

F – View Folder

Prompts the user for folder name, if the folder is found, the folder submenu is opened for that specific folder which displays all of the emails in the folder.

I – View Inbox

Opens the folder submenu for the inbox.

T – View Trash

Opens the folder submenu for the trash.

E – Empty Trash

Empties the trash folder of all emails.

Q – Quit the program

Remember to save the contents of the mailbox to the file, “mailbox.obj”.

There is no option to load or save a file in the menu. This is because you should automatically search for a file named “mailbox.obj” upon startup, and save to the file when the user wishes to quit the program.

Folder submenu:

M – Move email

Prompts the user for email index and then displays a list of folders and asks the user to input the name of the folder to move to. If the specified folder was not found, cancel the entire operation. User should be able to move to Inbox, Trash, and the current folder.

D – Delete email

Prompts the user for email index and moves the email to the trash folder.

V – View email contents

Prompts the user for email index and prints the information about the corresponding email.

SA – Sort by subject ascending

Sorts the emails by subject in ascending order.

SD – Sort by subject descending

Sorts the emails by subject in descending order.

DA – Sort by date ascending

Sorts the emails by date in ascending order.

DD – Sort by date descending

Sorts the emails by date in descending order.

R – Return to main menu

4. Include exception classes or additional classes as needed.

OUTPUT FORMAT

Be sure to have a menu after each operation.

Display to the user any errors that seem appropriate at any point during execution of your program.

EXTRA CREDIT (12 points)

Use Graphical User Interface (GUI) for program input and display the results. (12 points)

NOTE: The GUI must include headers (like the “Subject”, “Date” headers in your average mailbox system) and these headers must be clickable to sort. Clicking a header switches from ascending to descending (and vice versa) and changes the sort parameter to whatever the header specifies. And rather than accessing emails by index, all emails should have two buttons (Delete and Move) next to it that the user uses to delete and move emails from folders.

i.e. If the current sorting method is Date Ascending and the user clicks on the Subject header, the new sorting method is Subject Descending.

SAMPLE COMPARABLE CODE

import java.util.*;
 
/*
 * An example of type abstraction that implements Comparable
 * and Comparator interfaces.
 *
 * The Comparable/Comparator interfaces provide a standard means
 * for communication with yet unknown types of objects.
*/
      
public class CollectionsTester {
         public static void main(String[] args) {
           ArrayList<Employee> staff = new ArrayList<Employee>();
                   
           staff.add(new Employee("Joe",100000, 177700010));
           staff.add(new Employee("Jane",200000, 111100010));
           staff.add(new Employee("Bob",66666, 1999000010));
           staff.add(new Employee("Andy",77777, 188800010));
      
           Collections.sort(staff);                                      // Sort by salary
           System.out.println("Lowest paid employee: "+staff.get(0));    // Prints Bob
           
           Collections.sort(staff, new NameComparator());                // Sort by aplahabetical order
           System.out.println("First employee in list: "+staff.get(0));  // Prints Andy
          
           Collections.sort(staff, new IdComparator());                  // Sort by ID number
           System.out.println("Employee with lowest ID: "+staff.get(0)); // Prints Jane
         }
}
 
public class Employee implements Comparable {
        private String name;
        private int salary;
        private int id;
        public Employee(String initName, int initSal, int initId) {
                id     = initId;
                name = initName;
                salary = initSal;
        }
        public String getName(){ return name; }
        public int getSalary() { return salary; }
        public int getId(){ return id; }
        public void setSalary(int newSalary) {
                salary = newSalary;
        }
        public int compareTo(Object o) {
                Employee otherEmp = (Employee)o;
                if (this.salary == otherEmp.salary)
                        return 0;
                else if (this.salary > otherEmp.salary)
                        return 1;
                else
                        return -1;
        }
        public String toString() {
                return name + ", $" + salary + ", "+ id;
        }
}
 
public class NameComparator implements Comparator {
        public int compare(Object o1, Object o2) {
                Employee e1 = (Employee) o1;
                Employee e2 = (Employee) o2;
                return (e1.getName().compareTo(e2.getName()));
        }
}
 
public class IdComparator implements Comparator {
       public int compare(Object o1, Object o2) {
           Employee e1 = (Employee) o1;
           Employee e2 = (Employee) o2;
           if (e1.getId() == e2.getId())
               return 0;
           else if (e1.getId() > e2.getId())
               return 1;
           else
               return -1;
       }
}

EXAMPLES

ObjectInputStream.readObject():
MyClass myObject; //MyClass implements Serializable
try {
//If file is found, open it
FileInputStream   file = new FileInputStream(“mySaveFile.obj”);
ObjectInputStream fin = new ObjectInputStream(file);
myObject = (MyClass) fin.readObject();
file.close();
} catch(IOException a){}
catch(ClassNotFoundException c){} //Bottoms up!
//Note that myObject may still be null

ObjectOutpurStream.writeObject():
try {
FileOutputStream   file = new FileOutputStream(“mySaveFile.obj”);
ObjectOutputStream fout = new ObjectOutputStream(file);
fout.writeObject(myObject);
fout.close();
} catch(IOException a) { /*Error*/ }

System.out.printf():
System.out.printf(“%4d %4d %4d %-40s”, 1, 2, 3, “A string”);
System.out.printf(“%4d %4d %4d %-40s”, 10, 20, 30, “A longer string”);
System.out.printf(“%4d %4d %4d %-40s”, 10, 100, 1000, “Very longer than previous strings”);
//output:
//   1    2    3 A string
// 10   20   30 A longer string
// 10 100 1000 Very longer than previous strings

SAMEPL INPUT/OUTPUT

Comments in green. User input in black. Program output in blue.

Previous save not found, starting with an empty mailbox.

Mailbox:

——–

Inbox

Trash

A – Add folder

R – Remove folder

C – Compose email

F – Open folder

I – Open Inbox

T – Open Trash

Q – Quit

Enter a user option: A

Enter folder name: Other Folder

Mailbox:

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: C

Enter recipient (To): phillip.smith@stonybrook.edu, psmith@ic.sunysb.edu

Enter carbon copy recipients (CC): psmith@cs.sunysb.edu

Enter blind carbon copy recipients (BCC): phroph@yahoo.com

Enter subject line: Testing

Enter body: This is a test of the mailbox program!

Email successfully added to Inbox.

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: I

Inbox

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

M – Move email

D – Delete email

V – View email contents

SA – Sort by subject line in ascending order

SD – Sort by subject line in descending order

DA – Sort by date in ascending order

DD – Sort by date in descending order

R – Return to mailbox

Enter a user option: M

Enter the index of the email to move: 1

Folders:

Inbox

Trash

Other Folder

Select a folder to move “Testing” to: Other Folder

“Testing” successfully moved to Other Folder.

Inbox

Inbox is empty.

//Menu not included to save space.

Enter a user option: R

Mailbox:

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: F

Enter folder name: Other Folder

Other Folder

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

//Menu not included to save space.

Enter a user option: V

Enter email index: 1

To: phillip.smith@stonybrook.edu, psmith@ic.sunysb.edu

CC: psmith@cs.sunysb.edu

BCC: phroph@yahoo.com

Subject: Testing

This is a test of the mailbox program!

Other Folder

Index |        Time       | Subject

———————————–

  1   |  12:38PM 4/8/2012 | Testing

//Menu not included to save space.

Enter a user option: D

Enter email index: 1

“Testing” has successfully been moved to the trash.

Other Folder

Other Folder is empty.

//Menu not included to save space.

Enter a user option: R

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: E

1 item(s) successfully deleted.

Mailbox

——–

Inbox

Trash

Other Folder

//Menu not included to save space.

Enter a user option: Q

Program successfully exited and mailbox saved.

Expert Answer

 

Given below is the code needed for the question. Output also shown at the end. Please don’t forget to rate the answer if it helped. Thank you.

Email.java

import java.io.Serializable;
import java.util.GregorianCalendar;

public class Email implements Serializable{
private String to;
private String cc;
private String bcc;
private String subject;
private String body;
private GregorianCalendar timestamp;

public Email()
{

}

public Email(String to, String subject, String body)
{
this.to = to;
this.subject = subject;
this.body = body;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getCc() {
return cc;
}

public void setCc(String cc) {
this.cc = cc;
}

public String getBcc() {
return bcc;
}

public void setBcc(String bcc) {
this.bcc = bcc;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public GregorianCalendar getTimestamp() {
return timestamp;
}

public void setTimestamp(GregorianCalendar timestamp) {
this.timestamp = timestamp;
}

}

Folder.java

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Folder implements Serializable {
private ArrayList<Email> emails;
private String name;
private String currentSortingMethod;

private static Comparator<Email> dateAscending = new DateComparator();
private static Comparator<Email> subjectAscending = new SubjectComparator();
private static Comparator<Email> dateDescending = dateAscending.reversed();
private static Comparator<Email> subjectDescending = subjectAscending.reversed();
public Folder(String name)
{
this.name = name;
emails = new ArrayList<Email>();
currentSortingMethod = “DD”; //default sorting order date descending
}

public String getName() {
return name;
}

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

public String getCurrentSortingMethod() {
return currentSortingMethod;
}

private Comparator<Email> getCurrentComparator()
{
switch(currentSortingMethod)
{
case “SA” : return subjectAscending;
case “SD” : return subjectDescending;
case “DA” : return dateAscending;
case “DD” : return dateDescending;
default: return null;
}
}

public ArrayList<Email> getEmails()
{
return emails;
}
public void addEmail(Email email)
{
Comparator<Email> comp = getCurrentComparator();
for(int i = 0; i < emails.size(); i++)
{
if(comp.compare(email, emails.get(i)) < 0) //found the correct place to add
{
emails.add(i, email);
return;
}
}
emails.add(email);
}

public Email removeEmail(int index)
{
if(index >=1 && index <= emails.size())
return emails.remove(index – 1);
else
return null;
}

public void sortBySubjectAscending()
{
currentSortingMethod = “SA”;
Collections.sort(emails, subjectAscending);
}

public void sortBySubjectDescending()
{
currentSortingMethod = “SD”;
Collections.sort(emails, subjectDescending);
}
public void sortByDateAscending()
{
currentSortingMethod = “DA”;
Collections.sort(emails, dateAscending);
}
public void sortByDateDescending()
{
currentSortingMethod = “DD”;
Collections.sort(emails, dateDescending);
}
}

Mailbox.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Mailbox implements Serializable{
private Folder inbox;
private Folder trash;
private ArrayList<Folder> folders;

public static Mailbox mailbox ;
private static Scanner keybd = new Scanner(System.in);

public Mailbox()
{
inbox = new Folder(“Inbox”);
trash = new Folder(“Trash”);
folders = new ArrayList<Folder>();
}
public Folder getFolder(String name)
{
if(name.equalsIgnoreCase(“Inbox”))
return inbox;
if(name.equalsIgnoreCase(“Trash”))
return trash;
for(int i = 0; i < folders.size(); i++)
if(folders.get(i).getName().equalsIgnoreCase(name))
return folders.get(i);

return null; //when no matching name was found
}
public void addFolder(Folder folder)
{
if(getFolder(folder.getName()) != null)
{
throw new IllegalArgumentException(“Already a folder with name ” + folder.getName() + ” exists”);
}

folders.add(folder);
}

public void deleteFolder(String name)
{
for(int i = 0; i < folders.size(); i++)
if(folders.get(i).getName().equalsIgnoreCase(name))
{
folders.remove(i);
return;
}

throw new IllegalArgumentException(“Folder ” + name + ” could not be deleted.”);
}

public void composeEmail()
{
Email email = new Email();
System.out.print(“Enter recipient (To): “);
email.setTo(keybd.nextLine().trim());
System.out.print(“Enter carbon copy recipients (CC): “);
email.setCc(keybd.nextLine().trim());
System.out.print(“Enter blind carbon copy recipients (BCC): “);
email.setBcc(keybd.nextLine().trim());
System.out.print(“Enter subject line: “);
email.setSubject(keybd.nextLine().trim());
System.out.print(“Enter body: “);
email.setBody(keybd.nextLine().trim());
email.setTimestamp((GregorianCalendar)GregorianCalendar.getInstance());
mailbox.inbox.addEmail(email);
System.out.println(“Email successfully added to Inbox.”);
}
public void deleteEmail(Email email)
{
if(email != null)
trash.addEmail(email);
}

public void clearTrash()
{
int n = trash.getEmails().size();
trash.getEmails().clear();
System.out.println(n + ” item(s) successfully deleted.”);
}
public void moveEmail(Email email, Folder target)
{
if(email != null && target != null)
target.addEmail(email);
}

private static void listFolders()
{
System.out.println(“Inbox”);
System.out.println(“Trash”);

//list other folders in the mailbox
for(int i = 0; i < mailbox.folders.size(); i++)
System.out.println(mailbox.folders.get(i).getName());
}
public static void main(String[] args) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“mailbox.obj”));
mailbox = (Mailbox)ois.readObject();
ois.close();
} catch (Exception e) {
System.out.println(“Previous save not found, starting with an empty mailbox.n”);
mailbox = new Mailbox();
}

String choice = “”;

String name;
while(!choice.equalsIgnoreCase(“q”))
{
System.out.println(“Mailbox:”);
System.out.println(“——–“);
listFolders();

System.out.println(“nA – Add folder”);
System.out.println(“R – Remove folder”);
System.out.println(“C – Compose email”);
System.out.println(“F – Open folder”);
System.out.println(“I – Open Inbox”);
System.out.println(“T – Open Trash”);
System.out.println(“E – Empty Trash”);
System.out.println(“Q – Quit”);

System.out.print(“nEnter user option: “);
choice = keybd.next();
keybd.nextLine(); //remove new line
try
{
switch(choice.toUpperCase())
{
case “A”:
System.out.print(“Enter folder name: “);
name = keybd.nextLine();
mailbox.addFolder(new Folder(name.trim()));
break;
case “R”:
System.out.print(“Enter folder name: “);
name = keybd.nextLine();
mailbox.deleteFolder(name.trim());
break;

case “C”:
mailbox.composeEmail();
break;
case “F”:
System.out.print(“Enter folder name: “);
name = keybd.nextLine();
folderSubmenu(name);
break;
case “I”:
folderSubmenu(“Inbox”);
break;
case “T”:
folderSubmenu(“Trash”);
break;
case “E”:
mailbox.clearTrash();
break;
case “Q”:
//save to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(“mailbox.obj”));
oos.writeObject(mailbox);
oos.close();
break;
default:
System.out.println(“Invalid menu choice.”);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}

}

}

private static void folderSubmenu(String name)
{
Folder folder = mailbox.getFolder(name);
if(folder == null)
{
System.out.println(“No such folder – ” + name);
return;
}

String choice = “”;
ArrayList<Email> emails = folder.getEmails();
int index;
Email e;
while(!choice.equalsIgnoreCase(“R”))
{

System.out.println(folder.getName());

if(emails.size() == 0)
System.out.println(folder.getName() + ” is empty”);
else
{
System.out.printf(“%5s|%30s|%sn”, “Index”, “Time”, “Subject”);

for(int i = 0 ; i < emails.size(); i++)
{
e = emails.get(i);
System.out.printf(“%5s|%30s|%sn”, (i+1), e.getTimestamp().getTime(), e.getSubject());
}
}
System.out.println(“nM – Move email”);
System.out.println(“D – Delete email”);
System.out.println(“V – View email contents”);
System.out.println(“SA – Sort by subject line in ascending order”);
System.out.println(“SD – Sort by subject line in descending order”);
System.out.println(“DA – Sort by date in ascending order”);
System.out.println(“DD – Sort by date in descending order”);
System.out.println(“R – Return to mailbox”);
System.out.print(“Enter user option: “);
choice =keybd.next();
keybd.nextLine(); //remove newline
switch(choice.toUpperCase())
{
case “M”:
System.out.print(“Enter the index of email to move: “);
index = keybd.nextInt();
keybd.nextLine();
if(index >= 1 && index <= emails.size())
{
e = emails.get(index – 1);
listFolders();
System.out.print(“Select a folder to move “” + e.getSubject() +”” to:”);
String dest = keybd.nextLine().trim();
Folder destFolder = mailbox.getFolder(dest);
if(destFolder == null)
System.out.println(“No such folder : ” + dest);
else
{
mailbox.moveEmail(folder.removeEmail(index), destFolder);
System.out.println(“”” + e.getSubject() + “”” +
“has successfully been moved to ” + destFolder.getName());
}
}
else
System.out.println(“Invalid mail index.”);
break;
case “V”:
System.out.print(“Enter email index: “);
index = keybd.nextInt();
keybd.nextLine();
if(index >= 1 && index <= emails.size())
{
e = emails.get(index – 1);
System.out.println(“To: ” + e.getTo());
System.out.println(“CC: ” + e.getCc());
System.out.println(“BCC: ” + e.getBcc());
System.out.println(“Subject: ” + e.getSubject());
System.out.println(e.getBody() + “n”);
}
else
System.out.println(“Invalid mail index.”);
break;

case “D”:
System.out.print(“Enter email index: “);
index = keybd.nextInt();
keybd.nextLine();
if(index >= 1 && index <= emails.size())
{
mailbox.deleteEmail(folder.removeEmail(index));
}
else
System.out.println(“Invalid mail index.”);
break;
case “SA”:
folder.sortBySubjectAscending();
break;
case “SD”:
folder.sortBySubjectDescending();
break;
case “DA”:
folder.sortByDateAscending();
break;
case “DD”:
folder.sortByDateDescending();
break;

case “R”:
break;
default:
System.out.println(“Invalid menu choice!”);
}
}
}
}

DateComparator.java

import java.util.Comparator;

public class DateComparator implements Comparator<Email> {

@Override

public int compare(Email o1, Email o2) {

return o1.getTimestamp().compareTo(o2.getTimestamp());

}

}

SubjectComparator.java

import java.util.Comparator;

public class SubjectComparator implements Comparator<Email> {

@Override
public int compare(Email o1, Email o2) {
return o1.getSubject().compareTo(o2.getSubject());
}

}

output

Previous save not found, starting with an empty mailbox.

Mailbox:
——–
Inbox
Trash

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option:
A
Enter folder name: Other Folder
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: i
Inbox
Inbox is empty

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: r
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: c
Enter recipient (To): phillip.smith@stonybrook.edu, psmith@ic.sunysb.edu
Enter carbon copy recipients (CC): psmith@cs.sunysb.edu
Enter blind carbon copy recipients (BCC): phroph@yahoo.com
Enter subject line: Testing
Enter body: This is a test of the mailbox program!
Email successfully added to Inbox.
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: I
Inbox
Index| Time|Subject
1| Sat Aug 12 19:38:12 IST 2017|Testing

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: r
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: c
Enter recipient (To): john@stonybrook.edu
Enter carbon copy recipients (CC):
Enter blind carbon copy recipients (BCC):
Enter subject line: test subject
Enter body: second mail body
Email successfully added to Inbox.
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: i
Inbox
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject
2| Sat Aug 12 19:38:12 IST 2017|Testing

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: SA
Inbox
Index| Time|Subject
1| Sat Aug 12 19:38:12 IST 2017|Testing
2| Sat Aug 12 19:39:30 IST 2017|test subject

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: SD
Inbox
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject
2| Sat Aug 12 19:38:12 IST 2017|Testing

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: DA
Inbox
Index| Time|Subject
1| Sat Aug 12 19:38:12 IST 2017|Testing
2| Sat Aug 12 19:39:30 IST 2017|test subject

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: DD
Inbox
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject
2| Sat Aug 12 19:38:12 IST 2017|Testing

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: M
Enter the index of email to move: 1
Inbox
Trash
Other Folder
Select a folder to move “test subject” to:Other Folder
“test subject”has successfully been moved to Other Folder.
Inbox
Index| Time|Subject
1| Sat Aug 12 19:38:12 IST 2017|Testing

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: r
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: f
Enter folder name: Other Folder
Other Folder
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: V
Enter email index: 1
To: john@stonybrook.edu
CC:
BCC:
Subject: test subject
second mail body

Other Folder
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: d
Enter email index: 1
Other Folder
Other Folder is empty

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: r
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: t
Trash
Index| Time|Subject
1| Sat Aug 12 19:39:30 IST 2017|test subject

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: r
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: e
1 item(s) successfully deleted.
Mailbox:
——–
Inbox
Trash
Other Folder

A – Add folder
R – Remove folder
C – Compose email
F – Open folder
I – Open Inbox
T – Open Trash
E – Empty Trash
Q – Quit

Enter user option: t
Trash
Trash is empty

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option: q
Invalid menu choice!
Trash
Trash is empty

M – Move email
D – Delete email
V – View email contents
SA – Sort by subject line in ascending order
SD – Sort by subject line in descending order
DA – Sort by date in ascending order
DD – Sort by date in descending order
R – Return to mailbox
Enter user option:

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?