Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Java program that uses an ArrayList of parameter type Dictionary to store a database of words and allows the user to add a term delete a term display all terms

Write a java program that uses an ArrayList of parameter type Dictionary to store a
database of words. The Dictionary class should store the term and its synonyms.
Add appropriate accessor and mutator methods.
Your database program should present a menu that allows the user to add a term,
delete a term, display all terms, search for a specific term, or search for a specific
term and give the user the option to delete it. The search should find any term
where any instance variable contains a target search string. For example, if “Legacy”
is the search target, then the term should be displayed or deleted. Use the “foreach”
loop to iterate through the ArrayList. The program should also check if
the user inputs only text and should prompt the user for “Invalid Input” if the
input is other than text.

Answer:


/**
 * QuestionContacts.java
 *
 * This program implements a database of contacts that can be
 * added to, searched, displayed, or items removed.  An ArrayList
 * is used to store the database.
 */

import java.util.Scanner;
import java.util.ArrayList;

/**
* The Contact class stores the details for a single contact.
*/
class Contact
{
	private String first, last, phone, email;

	/**
	* Constructors.
	*/
	public Contact()
	{
	}

	public Contact(String first, String last, String phone, String email)
	{
		this.first = first;
		this.last = last;
		this.phone = phone;
		this.email = email;
	}

	/*
	* Mutator Methods
	*/
	public void setFirst(String first)
	{
		this.first = first;
	}

	public void setLast(String last)
	{
		this.last = last;
	}

	public void setPhone(String phone)
	{
		this.phone = phone;
	}

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

	/*
	* Return all fields concatenated into a string
	*/
	public String toString()
	{
		return last + ", " + first + ". " + phone + ", " + email;
	}
}

/**
* The ContactDatabase class stores each contact in an arraylist.
* Methods exist to add new contacts, search contacts, delete, and print contacts
* to the console.
*/
public class QuestionContacts
{
	private ArrayList<Contact> contacts;		// ArrayList of contact
	private static final int QUIT = 0;			// Menu choices
	private static final int ADD = 1;
	private static final int LISTALL = 2;
	private static final int SEARCH = 3;
	private static final int DELETE = 4;

	/**
	* Default constructor - make a new ArrayList object with parameter type Contact
	*/
	Question2Contacts()
	{
		contacts = new ArrayList<Contact>();
	}

	/**
	* inputContact inputs contact information from the keyboard.
	* It then stores this new contact in the contacts ArrayList.
	*/
	public void inputContact()
	{
		Scanner scan = new Scanner(System.in);
		Contact newContact = new Contact();

		System.out.println();
		System.out.println("Enter last name.");
		newContact.setLast(scan.next());
		System.out.println("Enter first name.");
		newContact.setFirst(scan.next());
		System.out.println("Enter phone number.");
		newContact.setPhone(scan.next());
		System.out.println("Enter Email.");
		newContact.setEmail(scan.next());

		// Add to the arraylist
		contacts.add(newContact);
	}

	/**
	* displayAll iterates through the ArrayList of contacts and outputs each one
	* to the screen.
	*/
	public void displayAll()
	{
		for (Contact c : contacts)
		{
			System.out.println(c.toString());
		}
	}

	/**
	* displayMatch inputs a keyword from the user.
	* It then iterates through the ArrayList of contacts and outputs each one
	* to the screen if the contact information contains the keyword.
	*/
	public void displayMatch()
	{
		boolean foundMatch;
		Scanner scan = new Scanner(System.in);

		System.out.println();
		System.out.println("Enter search keyword.");
		String keyword = scan.nextLine();

		System.out.println("Matches:");
		foundMatch = false;
		for (Contact c : contacts)
		{
			if (c.toString().toLowerCase().contains(keyword.toLowerCase()))
			{
				System.out.println(c.toString());
				foundMatch = true;
			}
		}
		if (!foundMatch)
		{
			System.out.println("There were no matches.");
		}
	}

	/**
	* deleteMatch inputs a keyword from the user.
	* It then iterates through the ArrayList of contacts and asks the user
	* if the contact should be deleted, if the contact information contains the keyword.
	*/
	public void deleteMatch()
	{
		boolean foundMatch;
		Scanner scan = new Scanner(System.in);

		System.out.println();
		System.out.println("Enter search keyword.");
		String keyword = scan.nextLine();

		foundMatch = false;
		for (Contact c : contacts)
		{
			if (c.toString().toLowerCase().contains(keyword.toLowerCase()))
			{
				System.out.println("Delete " + c.toString() + " (Y/N)?");
				String s = scan.nextLine();
				s = s.toLowerCase();
				if (s.charAt(0)=='y') {
					contacts.remove(c);		
// Remove object c if user answers "Y"
				}
				foundMatch = true;
			}
		}
		if (!foundMatch)
		{
			System.out.println("There were no matches.");
		}
	}

	// Main class
	public static void main(String[] args)
	{
		Question2Contacts cdb = new Question2Contacts();
		Scanner scan = new Scanner(System.in);
		int choice = ADD;

		// Main menu
		while (choice != QUIT)
		{
			System.out.println();
			System.out.println("Choose from the following:");
			System.out.println("0) Quit");
			System.out.println("1) Add new contact");
			System.out.println("2) List all contacts");
			System.out.println("3) Search contacts by keyword and display");
			System.out.println("4) Search contacts by keyword and remove");
			choice = scan.nextInt();
			switch (choice)
			{
				case ADD: 	cdb.inputContact();
							break;
				case LISTALL: cdb.displayAll();
							break;
				case SEARCH: cdb.displayMatch();
							break;
				case DELETE: cdb.deleteMatch();
							break;
			}
		}
	}
} 


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!