[Précédent]
[Index] | [Enoncé] | [Version imprimable]
[Prochain]

13.3
  Bon projet - Corrigé
Niveau 1
 
 
Fichiers:
  SafeProject.java    

Voici une solution possible :

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
import java.util.Scanner;
import java.util.ArrayList;

class SafeProject {
    
    private final static int NB_PROJECTS = 3;
    
    public static void main(String[] args) {
        ArrayList<Project> projects = new ArrayList<Project>();
        
        do {
            Project project = new Project();
            project.readProject();
            projects.add(project);
        } while (projects.size() < NB_PROJECTS);
    }
}

class Project {
    private String name = null;
    private String subject = null;
    private int duration = -1;

    public Project() {}

    // methode utilitaire utilis'ee par readProject pour la
    // lecture des entiers
    private int readInt(Scanner scanner) throws WrongDurationException {
        String strNumber = scanner.nextLine();
        int number;
        try {
            number = Integer.parseInt(strNumber);
        } catch (NumberFormatException e) {
            throw new WrongDurationException(strNumber + " is not a number !");
        }
        if (number <= 0) {
            throw new WrongDurationException("Duration should be stricly positive !");
        }
        return number;
    }

    // methode utilitaire utilis'ee par readProject pour la
    // lecture des String
    private String readString(Scanner scanner) throws NameTooLongException {
        String str = scanner.nextLine();
        if (str.length() > 50) {
            throw new NameTooLongException("Value should not exceed 50 characters");
        }
        return str;
    }


    // La methode readProject redemande les donn'ees
    // jsuqu'a ce qu'elles soient correctes
    public void readProject() {
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("Donnez le nom du projet : ");
            try {
                name = readString(scanner);
            } catch (NameTooLongException e) {
                System.err.println("[ " + e.getMessage() + " ]");
            }
        } while (name == null);

        do {
            try {
                System.out.println("Donnez le sujet du projet : ");
                this.subject = readString(scanner);
            } catch (NameTooLongException e) {
                System.err.println("[ " + e.getMessage() + " ]");
            }
        } while (subject == null);

        do {
            try {
                System.out.println("Donnez la durée du projet : ");
                this.duration = readInt(scanner);
            } catch (WrongDurationException e) {
                System.err.println("[ " + e.getMessage() + " ]");
            }
        } while (duration < 0);
    }
}

class WrongDurationException extends Exception {
    public WrongDurationException(String msg) {
        super(msg);
    }

    public WrongDurationException() {
        super("Wrong duration !");
    }
}

class NameTooLongException extends Exception {
    
    public NameTooLongException(String s) {
        super(s);
    }
    
    public NameTooLongException() {
        super("Too long name !");
    }

}

 


[Précédent]
[Index] | [Enoncé] | [Version imprimable]
[Prochain]