Cette première version du programme ne gère pas les erreurs et utilise un tableau pour stocker les produits. Pour les Produits, là où on utiliserait un record ou enregistrement ou struct dans un langage impératif, on utilise une classe (Product) dont les variables d'instance corrrespondent aux champs des enregistrements.
class Product{ String nom; int ref; int qte; void print(){ System.out.print(nom+" "+ref+" "+qte); } Product(String n, int r, int q){ nom = n; ref = r; qte = q; } Product stocker(int delta){ qte = qte + delta; return this; } Product destocker(int delta){ qte = qte - delta; return this; } } class Stock{ Product[] t = new Product[50]; int nb = 0; protected int rechercher(int r){ for (int i=0; i < nb; i++){ if (t[i].ref == r) return i; } return -1; } Stock referencer(String n, int r){ t[nb] = new Product(n,r,0); nb = nb + 1; // nb++; return this; } Stock dereferencer(int r){ int indice = rechercher(r); t[indice] = t[nb-1]; nb = nb - 1; return this; } Stock ajouter(int r, int delta){ int indice = rechercher(r); t[indice].stocker(delta); return this; } Stock retirer(int r, int delta){ int indice = rechercher(r); t[indice].destocker(delta); return this; } void etat(){ for (int i = 0; i<nb; i++){ t[i].print(); System.out.println(); } } } class Produit1{ public static void main(String[] arg){ Stock st = new Stock(); st.referencer("peigne",1001); st.referencer("brosse",999); st.ajouter(1001,100); st.referencer("lime",1003); st.ajouter(1003,50); st.retirer(1001,3); st.etat(); } }