Hi Superteo, in the future could you please wrap your code in [code][/code] or [php][/php] tags in order to preserve the format?
Lets take a look see.
PHP Code:
import java.util.*;
public class menu
{
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
int menu = 0;
System.out.println("*** book ***");
System.out.println();
System.out.println("1. Add a Personal Contact");
System.out.println("2. Print the whole list of Contact");
System.out.println("3. Search by Last Name");
System.out.println("4. Exit");
boolean quit = false;
do
{
System.out.print("Please enter your choice: ");
menu = scan.nextInt();
System.out.println();
switch(menu)
{
case 1:
PersonInfo person = new PersonInfo();
break;
case 2:
Print print = new Print();
break;
case 3:
Search search = new Search();
break;
case 4:
quit = true;
break;
default:
System.out.println("Invalid Entry!");
}
}
while (!quit);
}
}
}
}
}
import java.util.Scanner;
import java.io.*;
public class PersonInfo
{
private String name;
private String lastName;
private String address;
private String email;
private String phone;
private String notes;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
PersonInfo() throws IOException
{
System.out.println("Name: ");
name = br.readLine();
System.out.println("Last Name: ");
lastName = br.readLine();
System.out.println("Address: ");
address = br.readLine();
System.out.println("E-mail: ");
email = br.readLine();
System.out.println("Phone Number: ");
phone = br.readLine();
System.out.println("Add Note(s): ");
notes = br.readLine();
Contact contact = new Contact();
}
}
public class Contact
{
private String name;
private String lastName;
private String address;
private String email;
private String phone;
private String notes;
public void setContact(String n, String ls, String ad, String e, String ph, String nt)
{
name = n;
lastName = ls;
address = ad;
email = e;
phone = ph;
notes = nt;
}
public void printContact()
{
System.out.println("Name = " + name);
System.out.println("Last Name = " + lastName);
System.out.println("Address = " + address);
System.out.println("E-mail = " + email);
System.out.println("Phone = " + phone);
System.out.println("Note(s) = " + notes);
}
}
There is a couple of classes unavailable here, so I'm going to assume they don't have any relevance (the Print class and Search class).
This seems to be more work than you really need, but I can see what you are doing here. A big part of the problem is simply that you are not collecting the Contact from PersonInfo anywhere. What I would do, since the PersonInfo doesn't appear to do anything other than collecting information through an input interface, is create a static method instead. Something like this:
PHP Code:
public class PersonInfo
{
public static Contact createContact() throws NoSuchElementException
{
Scanner s = new Scanner(System.in); // I'm just going to use a scanner
String name, lastname, address, email, phone, notes;
name = s.nextLine();
lastName = s.nextLine();
address = s.nextLine();
email = s.nextLine();
phone = s.nextLine();
notes = s.nextLine();
s.close();
return new Contact(name, lastName, address, email, phone, notes);
}
}
Something like that. Note that there is no checking for datatypes or exceptions here.
Next, I'd either change the setContact in Contact to be the constructor, or better to create a new constructor that chains directly to setContact so you can keep both. Usage is a matter of:
PHP Code:
try
{
Contact con = PersonInfo.createContact();
System.out.println("Contact information: " + con);
}
catch (NoSuchElementException ex)
{
System.err.println("Unable to create a contact (scanner error: " + ex.getMessage() . ")");
}
Note the try/catch. As soon as you add to a definition of a method a throws, it then becomes what is called a 'checked' exception. This is required to be wrapped in a try/catch block (you may have seen this in compiler errors), or it will fail to compile. Leaving the 'throws' clause off of a signature will create an 'unchecked' exception which may throw a NoSuchElementException (or technically an IllegalStateException, but Scanner is having problems if it tosses that with the above code), but will not force you to actually try/catch it.
Now to create this into an actual address book, you want to either use an array or a collection. I'd go for the collection as they are much easier to deal with than fighting with array scalings. Contact information sounds like something that is potentially sortable, can insert into the middle of a list, and remove from any location. For this, I'd use a collection called an LinkedList, or potentially a PriorityQueue the presents an algorithmic calculation to the names in order to auto-sort them. Alternatives are the Vector and ArrayList classes which are both scalable, but are not easy to remove items from the inner items in the lists (from an actual processing step). This can be as simple as:
PHP Code:
import java.util.LinkedList;
public class AddressBook extends LinkedList<Contact>
{
}
LinkedList is both a collection and a list, so it has methods such as add, remove, size, clear, toArray, and many others. These are then iterable for each 'Contact' in the list, and can be printed individually (looks like you're Print class is likely what is doing this). This would be like so:
PHP Code:
for (Contact c : myAddressBookVariable)
{
System.out.println(c);
}
For searching, you can either flatten this and use the Arrays class to search it, or you can iterate it. In order to do so, you'll want to override the .equals method on the Contact class to compare two contact for equality:
PHP Code:
public boolean equals(Object other)
{
boolean result = false
if (other instanceof Contact) // whew, its been awhile, I think its instanceof that java likes.
{
result = other.lastName.equals(this.lastName);
}
return result;
}
Hmm, but with multiple contacts under the same name this may not quite work that well.
Oh, but the TreeSet collection actually has a subSet which returns a SortedSet<E> so you could technically splice out of a tree if you use a Comparable<E> interfaced object. This is starting to get a little more advanced; how much java experience did you have?