Question

this needs to be done in Java using Using Multiple Generic Data Structures – LinkedList and ArrayList

In this assignment, you will write a system to manage email address lists (ostensibly so that you could send emails to a list which has a name – which would send an email to each of the addresses in the list – although we will not actually implement sending emails.  Displaying the list will simulate being able to send the emails). We will use multiple Java Generic data structures to hold the data.   We could add MUCH to this program – I am providing specifications for the very basics that must be completed for this assignment to manage your time.  

Basic Requirements:

  1. An email address (which will be defined in the EmailAddressclass) is a String.  Much editing could  be done on this field – but the minimum for this assignment is that it must contain at least one ‘@’ and one ‘.’ and is at least 7 chars long.  I have written this class for you – it is published with this assignment.
  2. An email address list (EmailList class) will contain two fields – a String name of the list, and a LinkedListof EmailAddressobjects.    Note – be sure to know why a LinkedList is an effective structure to use for this data.
  3. The Directoryclass will hold handle an ArrayList of EmailList objects.  As we will need to search for EmailLists – we will want to keep these entries sorted by the name of the list.
  4. Main should have a menu which allows the following choices:
  • the creation of a new EmailList that is added to the EmailList’s ArrayList;
  • display of all the EmailLists in the ArrayList
  • addition of an EmailAddress to an existing EmailList in the ArrayList
  • deletion of an EmailAddress in an existing EmailList
  • display a particular EmailList
  • read in EmailLists from a file
  1. You must use these class names and the default package.   Be sure to ensure you are adhering to all Object Oriented Principles as have been covered in the class.  In particular, all fields MUST be declared as private.  (If you don’t specify this – the default is public).   Also, we will continue in this assignment to NOT use get/set methods.
  2. All possible error conditions must be handled.  No program crashing is allowed.  No System.exit() calls are allowed.
  3. File input hint – you should not need to use .nextLine() method….you can use .next() method – so you don’t have issues with the new line character.
  4. Efficiency considerations (minimum):
  • Use only one Scanner object to the keyboard in your program and pass it to the methods that need it.
  • Insert and search of EmailListsin Directoryclass
  • Insert and delete of EmailAddresses in EmailListclass

EmailLists.txt file format:

This file contains the following format:

  • An int – number of email lists contained in the file
  • Then for each email list
    • The name of list (containing no spaces)
    • An int – number of email addresses in the list to follow
    • The email addresses -

Sample file – EmailLists.txt:

5
CST8130Class 4 [email protected] [email protected] [email protected] [email protected]
Friends 2 [email protected] [email protected]
Family 4 [email protected] [email protected] [email protected] [email protected]
Work 1 [email protected]
ExtendedFamily 10 [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] 

Sample:

Sample Output: Enter c to create a new list Enter the name of the Enter valid email addressjoe@hotmail.com Another? y/BAふ Ent

code:

public class EmailAddress {

private String address = new String();

//***************** default constructor ******************************************

public EmailAddress() {

}

//***************** initial constructor ******************************************

public EmailAddress (String address) {

if (address.contains ("@") && address.contains(".") && address.length() >= 7)

this.address = new String (address);

}

//***************** toString **************************************************

public String toString() {

return address;

}

//***************** addAddress **************************************************

public void addAddress(Scanner in, String prompt) {

if (prompt.charAt(0) == 'y')

System.out.print ("Enter valid email address");

address = in.next();

while (!address.contains ("@") || !address.contains(".") || address.length() < 7) {

System.out.print ("Enter valid email address...must contain @ and .:");

address = in.next();

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assign3.java

import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;

public class Assign3 {
   private Scanner sc = new Scanner(System.in);
   private Directory directory = new Directory();

   public static void main(String[] args) {
       Assign3 assign3 = new Assign3();
       assign3.menu();
   }

   /**
   * Shows the menu, interacts with the user for menu selection, loops till
   * they exit.
   */
   public void menu() {
       boolean done = false;

       while (!done) {
           System.out.println("Enter c to create a new list");
           System.out.println("      p to display all the email lists");
           System.out.println("      a to add an entry to a list");
           System.out.println("      d to delete an entry from a list");
           System.out.println("      l to display a list");
           System.out.println("      f to load lists from file");
           System.out.print("      q to quit: ");

           String option = null;

           while (!sc.hasNextLine()) {
               sc.next();
           }

           option = sc.nextLine().toLowerCase();

           if (option.equals("c")) {
               createList();
           } else if (option.equals("p")) {
               displayAllLists();
           } else if (option.equals("a")) {
               addEntryToList();
           } else if (option.equals("d")) {
               deleteEntryFromList();
           } else if (option.equals("l")) {
               displaySingleList();
           } else if (option.equals("f")) {
               readFile();
           } else if (option.equals("q")) {
               System.out.println("Quit");
               done = true;
           } else {
               System.out.println("Invalid option, please try again.");
           }

           System.out.println();
       }
   }

   /**
   * Prompts user to enter data for a new list.
   */
   private void createList() {
       String name = enterName("Enter the name of the list: ");
       EmailList emailList = findEmailList(name);

       if (emailList == null) {
           emailList = new EmailList(name);
           enterEmailAddress(emailList);
           directory.addEmailList(emailList);
       } else {
           System.out.println("The EmailList with the name already exist.");
       }
   }

   /**
   * Displays all EmailLists in the directory.
   */
   private void displayAllLists() {
       directory.display();
   }

   /**
   * Prompts user to enter the name of the EmailList and displays the list.
   */
   private void displaySingleList() {
       String name = enterName("Enter name of list to display: ");
       EmailList emailList = findEmailList(name);

       if (emailList != null) {
           emailList.display();
       } else {
           System.out.println("The EmailList does not exist.");
       }
   }

   /**
   * Prompts user to enter the name of the EmailList and adds an EmailAddress to the list.
   */
   private void addEntryToList() {
       String name = enterName("Enter name of list to add to: ");
       EmailList emailList = findEmailList(name);

       if (emailList != null) {
           EmailAddress emailAddress = new EmailAddress();
           emailAddress.addAddress(sc, "y");
           emailList.addEntry(emailAddress);
           sc.nextLine();
       } else {
           System.out.println("The EmailList does not exist.");
       }
   }

   /**
   * Prompts user to enter the name of the EmailList and deletes the list.
   */
   private void deleteEntryFromList() {
       String name = enterName("Enter name of list to delete from: ");
       EmailList emailList = findEmailList(name);

       if (emailList != null) {
           int count = emailList.size();

           if (count > 0) {
               emailList.show();

               int index = -1;

               while (true) {
                   System.out.print("Enter entry number to delete: ");
                   try {
                       index = sc.nextInt();
                       sc.nextLine();
                       break;
                   } catch (InputMismatchException ex) {
                       System.err.println("Please enter an integer");
                       sc.nextLine();
                   }
               }

               if (index >= 0 && index < count) {
                   emailList.deleteEntry(index);
               } else {
                   System.out.println("The entry number is out of range.");
               }
           } else {
               System.out.println("The EmailList is empty.");
           }
       } else {
           System.out.println("The EmailList does not exist.");
       }
   }

   /**
   * Reads in EmailLists from a file.
   */
   private void readFile() {
       String filename = null;

       System.out.print("Enter name of file to process: ");

       while (true) {
           while (!sc.hasNextLine()) {
               sc.next();
           }

           filename = sc.nextLine().trim();

           if (!filename.equals("")) {
               break;
           }
       }

       Scanner scanner = openFile(filename);

       if (scanner != null) {
           // Read the line count.
           int totalLines = 0;

           while (scanner.hasNextLine()) {
               String line = scanner.nextLine().trim();

               if (!line.equals("")) {
                   try {
                       totalLines = Integer.parseInt(line);

                       if (totalLines <= 0) {
                           System.out.println("The total lines must be a positive integer: " + line);
                           return;
                       }

                       break;
                   } catch (NumberFormatException ex) {
                       System.out.println("Cannot find the total lines: " + line);
                       return;
                   }
               }
           }

           int lineCount = 0;

           while (scanner.hasNextLine()) {
               String line = scanner.nextLine().trim();

               if (!line.equals("")) {
                   lineCount++;

                   Scanner s = new Scanner(line).useDelimiter(" ");
                   String name = s.next();
                   int numberOfEmailAddresses = s.nextInt();
                   EmailList emailList = new EmailList(name);

                   for (int i = 0; i < numberOfEmailAddresses; i++) {
                       emailList.addEntry(new EmailAddress(s.next()));
                   }
                   directory.addEmailList(emailList);

                   if (lineCount >= totalLines) {
                       break;
                   }
               }
           }

           scanner.close();
       } else {
           System.out.println(filename + " not found");
       }
   }

   /**
   * Prompts user to enter a name that represents the EmailList
   * and returns the name.
   *
   * @return String
   */
   private String enterName(String prompt) {
       String name = null;
       boolean done = false;

       while (!done) {
           System.out.print(prompt);

           while (!sc.hasNextLine()) {
               sc.next();
           }

           name = sc.nextLine().trim();

           if (!name.equals("")) {
               done = true;
           }
       }

       return name;
   }

   private void enterEmailAddress(EmailList emailList) {
       boolean done = false;
       EmailAddress emailAddress = null;

       while (!done) {
           emailAddress = new EmailAddress();
           emailAddress.addAddress(sc, "y");
           emailList.addEntry(emailAddress);

           sc.nextLine();

           System.out.print("Another? y/n: ");

           while (!sc.hasNextLine()) {
               sc.next();
           }

           String option = sc.nextLine().toLowerCase();

           if (option.equals("y")) {
               continue;
           } else if (option.equals("n")) {
               done = true;
           } else {
               System.out.println("Invalid option, please try again.");
           }
       }
   }

   /**
   * Finds the specified EmailList for the given name.
   *
   * @return EmailList
   */
   private EmailList findEmailList(String name) {
       return directory.findEmailList(name);
   }

   private Scanner openFile(String filename) {
       File file = new File(filename);

       if (file.exists()) {
           try {
               Scanner scanner = new Scanner(file);

               return scanner;
           } catch (FileNotFoundException e) {
               return null;
           }
       } else {
           return null;
       }
   }
}


Directory.java

import java.util.List;
import java.util.ArrayList;

public class Directory {

   private List<EmailList> list = null;

   /**
   * Default constructor.
   */
   public Directory() {
       this.list = new ArrayList<EmailList>();
   }

   /**
   * Adds an EmailList to the directory.
   *
   * @param EmailList
   */
   public void addEmailList(EmailList emailList) {
       list.add(emailList);
   }

   /**
   * Displays all EmailLists in the directory.
   */
   public void display() {
       System.out.println("The email lists are: ");
       for (EmailList emailList: list) {
           emailList.display();
       }
   }

   /**
   * Finds the specified EmailList for the given name.
   *
   * @return EmailList
   */
   public EmailList findEmailList(String name) {
       for (EmailList emailList: list) {
           if (emailList.isEqual(name)) {
               return emailList;
           }
       }
       return null;
   }
}


EmailAddress.java

import java.util.Scanner;

public class EmailAddress {
   private String address = new String();

   //***************** default constructor ******************************************
   public EmailAddress() {
   }

   //***************** initial constructor ******************************************
   public EmailAddress (String address) {
       if (address.contains ("@") && address.contains(".") && address.length() >= 7)
           this.address = new String (address);
   }

   //*****************   toString    **************************************************
   public String toString() {
       return address;
   }

   //***************** addAddress    **************************************************
   public void addAddress(Scanner in, String prompt) {
       if (prompt.charAt(0) == 'y')
           System.out.print ("Enter valid email address: ");
       address = in.next();
       while (!address.contains ("@") || !address.contains(".") || address.length() < 7) {
           System.out.print ("Enter valid email address...must contain @ and .: ");
           address = in.next();
       }
   }

}

EmailList.java

import java.util.List;
import java.util.LinkedList;

public class EmailList {

   private String name = null;
   private List<EmailAddress> addresses = null;

   /**
   * Parameterized constructor.
   */
   public EmailList(String name) {
       this.name = name;
       this.addresses = new LinkedList<EmailAddress>();
   }

   /**
   * Adds an entry to the list.
   *
   * @param EmailAddress
   */
   public void addEntry(EmailAddress emailAddress) {
       addresses.add(emailAddress);
   }

   /**
   * Removes the entry at the specified position in the list.
   */
   public void deleteEntry(int index) {
       addresses.remove(index);
   }

   /**
   * Displays all entries in the list.
   */
   public void display() {
       System.out.print(name + ":[");

       for (int i = 0; i < addresses.size(); i++) {
           if (i > 0)
               System.out.println(",");
           System.out.print(addresses.get(i));
       }

       System.out.println("]");
   }

   /**
   * Shows all entries in the directory for selection.
   */
   public void show() {
       System.out.println(name);
       for (int i = 0; i < addresses.size(); i++) {
           System.out.println(i + " " + addresses.get(i));
       }
   }

   /**
   * Compares the EmailList with name.
   *
   * @param name
   * @return boolean
   */
   public boolean isEqual(String name) {
       if (this.name.equals(name)) {
           return true;
       } else {
           return false;
       }
   }

   /**
   * Returns the number of entries in the list.
   *
   * @return the number of entries in the list
   */
   public int size() {
       return addresses.size();
   }
}

Add a comment
Know the answer?
Add Answer to:
This needs to be done in Java using Using Multiple Generic Data Structures – LinkedList and Array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java program application that creates an ArrayList that holds String objects. It needs to...

    Create a Java program application that creates an ArrayList that holds String objects. It needs to prompt the user to enter the names of friends, which are then put into the ArrayList; allow the user to keep adding names until they enter "q" to quit. After input is complete, display a numbered list of all friends in the list and prompt the user to enter the number of the friend they wish to delete. After deleting that entry, output the...

  • Write a Java program to work with a generic list ADT using a fixed size array,...

    Write a Java program to work with a generic list ADT using a fixed size array, not ArrayList. Create the interface ListInterface with the following methods a) add(newEntry): Adds a new entry to the end of the list. b) add(newPosition, newEntry): Adds a new entry to the list at a given position. c) remove(givenPosition): Removes the entry at a given position from the list. d) clear( ): Removes all entries from the list . e) replace(givenPosition, newEntry): Replaces the entry...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • Java Data Structures

    Programming Instructions:Using      Java Object Oriented Principles, write a program which produces the      code as indicated in the following specifications: Your       program must be a console application that provides a user this exact       menu:Please select one of the following:1: Add Client to Bank2: Display Clients in the Bank3: Set Bank Name4: Search for a Client5: Exit Enter your Selection: <keyboard input here> The       menu must be displayed repeatedly until 5...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Lab 1.4: Arrays, File I/O and Method Review (40 pts) Assume you work for a company...

    Lab 1.4: Arrays, File I/O and Method Review (40 pts) Assume you work for a company that received a list of email addresses of potential new customers from a data broker. Your company receives a file named customers.txt with the below information: Jiming Wu [email protected] James Brown [email protected] Leanna Perez [email protected] Xing Li [email protected] Stacey Cahill [email protected] Mohammed Abbas [email protected] Kumari Chakrabarti [email protected] Shakil Smith Shakattaq2G.com Jung Ahrin [email protected] Pedro Martinez [email protected] Ally Gu [email protected] Tamara White [email protected] Alvin Ngo...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT