Project: a contact book in Java
A step-by-step guided project that brings together all the basics: classes, ArrayList, a menu loop and methods. With an interactive console to try it.
What you will build
A console program that keeps contacts (name and phone) in memory while it runs. You can add, list, search and delete. The perfect project to cement classes, lists and loops.
Try it right here before looking at the code. Type a command and press Enter:
ayudalistanuevo <nombre> <telefono>buscar <texto>borra <n>
Step 1: the Contacto class
Each contact has a name and a phone number. That is a class with two fields, a constructor and a method to display it.
public class Contacto {
String nombre;
String telefono;
Contacto(String nombre, String telefono) {
this.nombre = nombre;
this.telefono = telefono;
}
@Override
public String toString() {
return nombre + " — " + telefono;
}
}
toString() makes printing the contact show readable text instead of something like Contacto@1b6d3586.Step 2: store the contacts in an ArrayList
We do not know how many contacts there will be, so we use an ArrayList: a list that grows on its own.
import java.util.ArrayList;
import java.util.Scanner;
public class Agenda {
static ArrayList<Contacto> contactos = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
contactos.add(new Contacto("Ana Gómez", "600111222"));
contactos.add(new Contacto("Luis Pérez", "655333444"));
// ... el menú va aquí (paso 3)
}
}
Step 3: the menu with a while loop
The program loops: it shows options, reads what the user types and acts. It exits when you type salir.
while (true) {
System.out.print("\n> ");
String linea = sc.nextLine().trim();
String[] partes = linea.split("\\s+");
String cmd = partes[0].toLowerCase();
if (cmd.equals("salir")) {
System.out.println("Hasta luego.");
break;
} else if (cmd.equals("lista")) {
listar();
} else if (cmd.equals("nuevo")) {
// nuevo Marta Ruiz 611222333
String telefono = partes[partes.length - 1];
String nombre = linea.substring(6, linea.length() - telefono.length()).trim();
contactos.add(new Contacto(nombre, telefono));
System.out.println("Contacto añadido: " + nombre);
} else if (cmd.equals("buscar")) {
buscar(linea.substring(7).toLowerCase());
} else if (cmd.equals("borra")) {
borrar(Integer.parseInt(partes[1]) - 1);
} else {
System.out.println("Comando no reconocido.");
}
}
Step 4: the methods
Each action in its own method: main stays short and readable.
static void listar() {
if (contactos.isEmpty()) {
System.out.println("(agenda vacía)");
return;
}
for (int i = 0; i < contactos.size(); i++) {
System.out.println(" " + (i + 1) + ". " + contactos.get(i));
}
}
static void buscar(String texto) {
for (Contacto c : contactos) {
if (c.nombre.toLowerCase().contains(texto)) {
System.out.println(" " + c);
}
}
}
static void borrar(int indice) {
if (indice >= 0 && indice < contactos.size()) {
Contacto fuera = contactos.remove(indice);
System.out.println("Borrado: " + fuera.nombre);
} else {
System.out.println("Número fuera de rango.");
}
}
Where to go next
- Save the contact book to a file so it is not lost on exit (see Files in Java).
- Validate the phone number with a regular expression (see Regex in Java).
- Use a
Map<String, Contacto>to look up by name instantly (see Collections). - Sort the contacts alphabetically with
Collections.sort.