Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Mini address book

Core exercise

A book of first names stored in an ArrayList<String>, with a menu: 1) Ajouter (add) 2) Lister (list) 3) Rechercher (search) 4) Supprimer (delete) 0) Quitter (quit).

Goals: Collections (ArrayList), loops, submenus, searching.

Features

  • rechercherPrenom(List<String> noms, String recherche) returns the index found, or -1. The search ignores case.
  • supprimerPrenom(List<String> noms, String aSupprimer) returns true if the deletion took place.
  • listerPrenoms(List<String> noms) prints the list, and a clear message if it is empty (the check looks for the word “vide”, French for empty).

Steps

  1. afficherMenuAgenda().
  2. ajouterPrenom(List<String> noms, Scanner sc).
  3. listerPrenoms(List<String> noms).
  4. rechercherPrenom(List<String> noms, String recherche).
  5. supprimerPrenom(List<String> noms, String aSupprimer).
  6. boucleAgenda(Scanner sc) → the main loop.

Criteria: No crash if the list is empty, safe deletion, clear messages.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Agenda.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Agenda {

    public static void afficherMenuAgenda() {
        // TODO : 1) Ajouter 2) Lister 3) Rechercher 4) Supprimer 0) Quitter
    }

    public static void ajouterPrenom(List<String> noms, Scanner sc) {
        // TODO : lire un prenom et l'ajouter a la liste
    }

    public static void listerPrenoms(List<String> noms) {
        // TODO : afficher chaque prenom, ou un message si la liste est vide
    }

    public static int rechercherPrenom(List<String> noms, String recherche) {
        // TODO : renvoyer l'indice trouve (sans tenir compte de la casse), sinon -1
        return -1;
    }

    public static boolean supprimerPrenom(List<String> noms, String aSupprimer) {
        // TODO : supprimer si present, renvoyer true en cas de succes
        return false;
    }

    public static void boucleAgenda(Scanner sc) {
        List<String> noms = new ArrayList<String>();
        // TODO : do { menu, choix, action } while (choix != 0);
    }
}

Exercices.java

import java.util.Scanner;

public class Exercices {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Agenda.boucleAgenda(sc);
        sc.close();
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

An ArrayList is traversed like an array, but its size is obtained with size() and an element with get(i). To ignore case, compare with equalsIgnoreCase.

Extensions

  • Alphabetical sorting (with Collections.sort).
  • Simple CSV export / import, if you have covered files.