I’m just doing a small program. It’s an address book which has four options:
- insert new contact
- search contact by last name
- delete contact by name
- show all contacts
- exit program
Just wondering how to get the insert contact part and how to store it. I’ve hardcoded one contact to test it.
Here is my code I have started
package addressbook;
import java.util.Scanner;
public class addressbooks
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//create a table to hold information
String[][] addressbooks = new String[100][8];
addressbooks[0][0]="Mobile Number";
addressbooks[0][1]="First Name";
addressbooks[0][2]="Last Name";
addressbooks[0][3]="Address";
addressbooks[0][4]="City";
addressbooks[0][5]="County";
addressbooks[0][7]="Telephone Number";
//pre-populate address book for testing purposes and records
addressbooks[1][0]="1";
addressbooks[1][1]="David";
addressbooks[1][2]="Needham";
addressbooks[1][3]="Sraheens, Achill";
addressbooks[1][4]="Galway";
addressbooks[1][5]="Mayo";
addressbooks[1][6]="086-1581077";
addressbooks[1][7]="098-45368";
addressbooks[2][0]="2";
addressbooks[2][1]="Mc";
addressbooks[2][2]="lovin";
addressbooks[2][3]="Hawaii";
addressbooks[2][4]="Hawaii";
addressbooks[2][5]="Hawaii";
addressbooks[2][6]="12345";
addressbooks[2][7]="412-555-1234";
//menu options
System.out.print("Welcome to my Address book!");
System.out.print("\n");
System.out.print("\n1 - Insert a New Contact \n2 - Search Contact by Last Name \n3 - Delete Contact \n4 - Show All Contacts \n5 - Exit " );
System.out.print("\n");
System.out.print("\nChoose your option: ");
int option = input.nextInt();
if (option ==1)
{
System.out.print("\nPlease enter your First Name : ");
}
if (option ==2)
{
}
if (option ==3)
{
}
if (option ==4)
{
System.out.println(addressbooks[1][0]+
"\t"+addressbooks[1][2]+ ", "+addressbooks[1][1]+
"\n\t"+addressbooks[1][3]+
"\n\t"+addressbooks[1][4]+ ", "+addressbooks[1][5]+ " "+addressbooks[1][6]+
"\n\t"+addressbooks[1][7]);
}
if (option ==5)
{
}
}
}
I’d start like this:
I would recommend separating all that stuff about text-based IO out away from the fundamentals of your problem. If you get it right, the next step is to write a web UI. Most of the code will be reusable if you do it correctly.
Think about layering your app:
Model classes might be used in all three layers.
The answer recommending JDBC isn’t wrong. If you write your service and persistence classes as interfaces, you’ll find it easy to swap out an in-memory Map of Contacts for a database version that uses JDBC:
There are lots of ways to do things. I’ve already changed my mind: I’ve introduced a Contact class: