Results 1 to 4 of 4

Thread: Help

  1. #1
    littlechaoz is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Question Help

    i need help with this java assigment, im totally confused as to what to do :(

    Java Code:
    /**
     * ContactDatabase.java
     *
     * This class implements a database of contacts that can be
     * added to, searched, displayed, or items removed.  An ArrayList
     * is used to store the database.
     *
     * @author
     * @version
     */
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    /**
    * 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 ContactDatabase
    {
    	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
    	*/
    	ContactDatabase()
    	{
    		 
    	}
    
    	/**
    	* inputContact inputs contact information from the keyboard.
    	* It then stores this new contact in the contacts ArrayList.
    	*/
    	public void inputContact()
    	{
    		
    	}
    
    	/**
    	* displayAll iterates through the ArrayList of contacts and outputs each one
    	* to the screen.
    	*/
    	public void displayAll()
    	{
    		
    	}
    
    	/**
    	* 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()
    	{
    		
    	}
    
    	/**
    	* 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()
    	{
    		
    	}
    
    	// Main class
    	public static void main(String[] args)
    	{
    		ContactDatabase cdb = new ContactDatabase();
    		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;
    			}
    		}
    	}
    
    	/**
    	 * The inner class, Contact, stores the details for a single contact.  
    	 *  There is no error checking on any of the input.  Whatever string is 
    	 *  passed in for a given attribute is accepted.  
    	 */
    	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;
    	   }
    
    	   /*
    	    * Accessor Methods
    	    */
    
    	   public String getFirst()
    	   {
    	      return first;
    	   }
    
    	   public String getLast()
    	   {
    	      return last;
    	   }
    
    	   public String getPhone()
    	   {
    	      return phone;
    	   }
    
    	   public String getEmail()
    	   {
    	      return 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;
    	   }
    
    
    	   public boolean equals(Object otherObject)
    	   {
    	      if (otherObject ==null)
    	      {
    	         return false;
    	      }
    	      else if (getClass() != otherObject.getClass())
    	      {
    	         return false;
    	      }
    	      else
    	      {
    	         Contact otherContact = (Contact)otherObject;
    	         return (first.equals(otherContact.first) && 
    	               last.equals(otherContact.last)&&
    	               phone.equals(otherContact.phone)&&
    	               email.equals(otherContact.email));
    	      }
    	   }
    
    	} // end inner class, Contact
    } // end class, ContactDatabase

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    8

    Default

    So are we. Tell us what you did, what the error/problem is, and, most important... what is your assignment?
    I die a little on the inside...
    Every time I get shot.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    confused as to what to do
    Looks like you need to implement the empty methods of the ContactDatabase class:
    Java Code:
    ContactDatabase()
    {
        // instantiate your ArrayList here
    }
    
    public void inputContact()
    {
        // Receive user input for the required fields in the Contact
        //     class, viz, names, phone, email.
        // Use these to create a new instance of Contact and add this
        //     new instance to the contacts list.
    }
    
    public void displayAll()
    {
        // Run through list and for each entry print out the Contact info.
        // Since Contact overrides the [i]toString[/i] method then all you
        // need to do is
        // System.out.println("contact = " contacts.get(i));
    }
    
    public void displayMatch()
    {
        // get keyword input from user to be used in search
        // run througth list and print out each Contact which
        // has the keyword, viz, name, phone or email.
    }
    
    public void deleteMatch()
    {
        // get keyword input from user to be used in search
        // run through list and look for any Contact that has
        //     the keyword
        // prompt user to make sure they want to delete the
        //     (next/current) Contact you found
        // if yes, delete the Contact
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •