Application.java 2.66 KB
package app;

import kernel.Cell;
import kernel.Grid;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import kernel.function.Average;
import kernel.function.Sum;
import kernel.operation.Addition;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Application {

    public static void main(String[] args) throws IOException {
        File fichier = new File("essai.ser");

        // ouverture d'un flux sur un fichier
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier));

        Grid grid = new Grid();

        // Création
        System.out.println("Création de quelques cases...");

        try {
            grid.createCell("A", 1, 60.);
            grid.createCell("B", 1, 0.);
            grid.createCell("A", 2, 5.);
            grid.createCell("A", 6, new Addition(grid.getCell("A", 1),grid.getCell("A", 2)));
            
            List<Cell> sumList = new ArrayList<>();
            sumList.add(grid.getCell("A", 1));
            sumList.add(grid.getCell("A", 2));

            grid.createCell("A", 3, new Sum(sumList));

            List<Cell> averageList = new ArrayList<>();
            averageList.add(grid.getCell("A", 3));
            averageList.add(grid.getCell("B", 1));

            grid.createCell("B", 2, new Average(averageList));
        } catch (CellNotFoundException | CreateCycleException | InvalidIntervalException exception) {
            System.out.println(exception.getMessage());
        }

        // Affichage
        List<Cell> cells = grid.getCells();

        System.out.println("Affichage des valeurs :");
        for (Cell cell : cells)
            System.out.println(cell.getId() + ": " + cell.getValue());

        System.out.println("Affichage des formules :");
        for (Cell cell : cells)
            System.out.println(cell.getId() + ": " + cell.toString());

        System.out.println("Affichage des formules développées :");
        for (Cell cell : cells)
            System.out.println(cell.getId() + ": " + cell.getDevelopedFormula());

        // Propagation
        try {
            grid.setValue("A", 1, 20.);
        } catch (CellNotFoundException exception) {
            System.out.println("exception");
        }

        System.out.println("Affichage des valeurs après modification :");
        for (Cell cell : cells)
            System.out.println(cell.getId() + ": " + cell.getValue());

        // sérialization de l'objet
        oos.writeObject(grid);
        oos.close();
    }
}