File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Project Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Project Help" Watch "Project Help" New topic
Author

Project Help

Donald Dalton
Greenhorn

Joined: Apr 01, 2005
Posts: 4
Hi all.

I am in urgent need of some help with an assignment.

Here are the requirements:
I am to write a console program that maintains a contact database. The
program should consist of at least two classes.

One class named Contact that will be used as an abstract data type for the
contact information. This class will contain fields for a person's first name
last name, email address, and phone number.

A second class named Project2 which will create an array of contacts. The program should present the user with the ability to:
Generate a numbered list of contacts
Add a new contact
Delete a contact by number
Exit

Each operation should be implemented as a seperate method in the Project2 class. No more than 20 contacts.

Any help will be greatly appreciated. I have been working on this assignment for 2 weeks and it is due in a couple days. I have created class Contact (not sure if this is right):


import java.io.*;

public class Contact
{
String firstName;
String lastName;
String eMail;
String phoneNum;
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));

public Contact() throws IOException
{

System.out.print("Enter First Name: ");
firstName = dataIn.readLine();
System.out.print("Enter Last Name: ");
lastName = dataIn.readLine();
System.out.print("Enter eMail Address: ");
eMail = dataIn.readLine();
System.out.print("Enter Phone Number: ");
phoneNum = dataIn.readLine();
}

}

And here is what I have for the Project2 class:
import java.io.*;


public class Project2 {


public static void main(String args []) throws IOException
{

String a = "Add";

BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));

System.out.println("\tContact Database");
System.out.println();
System.out.print("Enter Add to add new contact" + "\nDelete to delete contact" +
"\nList to list contacts" + "\nor Exit to exit program:");
String cmdLine = dataIn.readLine();

} // end main method



} // end class Project

I'm probably way off but this is my first time taking a Java class. I haven't even written 50 lines of code, so I know I've got a long ways to go but don't even know if I've begun correctly or where to go next. Again, this is urgent. So a little nudge in the right direction would be great
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your assignment stipulates at least two classes. You might find it easier if you use three. What you could do is:
  • Define your Contact class. This does nothing more than hold the properties of a Contact. So no IO in this class.
  • Define a class which maintains your list of Contact objects (currently called Project2 - a more descriptive name might be ContactDatabase). Again thats all it does so no IO.
  • Define a "ContactsApp" class which handles all the IO. This is where your main method would be, and where you would handle user input.

  • If you think of the steps involved and (more importantly) the responsibilities of each class, it might become clearer, and make development easier. So the steps of this program could be something like:
  • ContactsApp prompts for user input
  • ContactsApp interprets the input to decide what the user wants to do (something like "Enter 1 to add a contact, 2 to delete" etc.). In this instance lets assume the uses selects 1.
  • ContactsApp prompts for user input again.
  • ContactsApp gathers input and uses it to create a Contact object
  • If no ContactDatabase exists, ContactsApp creates one and calls its add method, passing the newly populated Contact object as a parameter.
  • ContactsApp prompts for user input for the next option
  • etc.


  • For the ContactDatabase class, you probably want to look at a Collections class, specifically something like java.util.HashMap.

    This making sense?


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Donald Dalton
    Greenhorn

    Joined: Apr 01, 2005
    Posts: 4
    This absolutely helps alot. Thanks. But before I dive in, I want to make sure I understand what you're telling me.

    OK, here we go. I'll start with the Contact class
    The purpose of Contact class is to hold variables?

    //Define Contact class
    public class Contact {
    String firstName;
    String lastName;
    String eMail;
    String phoneNum;

    // constructor creates instance of contact class so it can be referenced
    Contact contact = new Contact();
    }

    ContactsApp class allows for user input.
    It's responsibilities:
    -Accept user input
    -If user wants to add contact, store contact info in array which is
    declared and referenced with ContactDatabase class?

    //Define ContactsApp
    import java.io.*;

    public class ContactsApp {
    public static void main(String args[]) throws IOException
    {
    BufferedReader dataIn = new BufferedReader (new
    InputStreamReader(System.in));

    System.out.println("\tContact Database");
    System.out.println();
    System.out.print("Enter 1 to add new contact" + "\nEnter 2 to delete
    contact" + "\nEnter 3 to list contacts" + "\nor Enter 0 to exit
    program:");
    String cmdLine = dataIn.readLine();

    //Use control structure to allow user to enter each contact info
    and save it in the ContactDatabase array? Not sure how. Nested if
    statements?
    if (cmdLine = 1)
    System.out.println("First Name: ");
    String cmdLine = dataIn.readLine();

    }
    }

    I understand that when the user inputs contact info, the info should be saved in an array because there will be a possible 20 contacts, but how? I have problems understanding how arrays are "populated"? through user input.

    //Define ContactDatabase- which creates and manages an array of contacts
    public class ContactDatabase {

    //Declare array
    String[]contacts;

    //construct array
    String[]contact = new String[20];

    ContactDatabase contactdatabase = new ContactDatabase ();
    }

    This is what I understand so far. If I am way lost let me know. I may just have to withdraw from this course and take it again. But, I believe I now better understand the steps involved. Again, I appreciate your help and let me know if I am indeed understanding what you explained. I also certainly appreciate you acting as a teacher because my goal is to learn java not to just get by in this class.
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    The purpose of Contact class is to hold variables?


    Yes. So you provide get/set methods for each possible property. It encapsulates the properties of a Contact. And the ContactDatabase is the same: its only concern is maintaining the properties of a list of Contact objects.

    Think of the array being populated with Contact objects. You prompt for the required input, create a Contact object, set its properties with the values you've gathered and call some sort of add(Contact) method on ContactDatabase.

    The add method will put it in your array, as you would put anything in an array. Currently you've defined your array as an array or String objects. If you change this to be an array of Contact objects, you can put Contact objects in it, i.e. :



    You've got two problems to think about when you write this add method:
  • Arrays can't grow. So what happens when you try to add the 21st Contact?
  • You have to add things to an array at a known position. So given you also have to support deletion, you'll have to think about how you also know which spaces in the array you can add things to.

  • The first problem is easy enough, if you think about it. The second requires you either keep the array sequential at all times, or you search for an avaliable space every time you add something.
    [ April 01, 2005: Message edited by: Paul Sturrock ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Project Help