Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. public class Generation {
  2.  
  3.     private final File file = new File("src/safeguard.txt");
  4.     private final Scanner sc = new Scanner(System.in);
  5.     private Map<String, Integer> map = new LinkedHashMap<>();
  6.  
  7.     // Chargement et vérification du fichier de configuration
  8.  
  9.     public void load() {
  10.         boolean answer;
  11.         System.out.println("Loup-Garou version 1.0");
  12.         System.out.print("\nCharger une sauvegarde ? : ");
  13.  
  14.         if ((answer = sc.nextBoolean()) == true) {
  15.             if (!file.exists()) {
  16.                 System.err.println("\nImpossible de lire le fichier !");
  17.                 selection();
  18.             }
  19.         } else selection();
  20.  
  21.         try {
  22.             final List<String> list = FileUtils.loadContent(file);
  23.             for (String line : list) {
  24.                 if (!line.startsWith("//")) {
  25.                     String[] args = line.split(": ");
  26.                     map.put(args[0], Integer.parseInt(args[1]));
  27.                 }
  28.             }
  29.             parse();
  30.         } catch (ConfigurationException e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.  
  35.     private void selection() {
  36.         int sv, lg;
  37.         System.out.println("\nConfiguration ->");
  38.         System.out.print("\nSimple_Villageois : ");
  39.         sv = sc.nextInt();
  40.         System.out.print("Loup-Garou : ");
  41.         lg = sc.nextInt();
  42.  
  43.         FileUtils.deleteFile(file);
  44.         FileUtils.save(file, "SIMPLE_VILLAGEOIS: " + sv);
  45.         FileUtils.save(file, "LOUP-GAROU: " + lg);
  46.     }
  47.  
  48.     // Vérification de la validité de la configuration
  49.  
  50.     private void parse() throws ConfigurationException {
  51.         if (map.isEmpty()) throw new ConfigurationException("Bad configuration !");
  52.  
  53.         if (map.get("SIMPLE_VILLAGEOIS") == 0 || map.get("LOUP-GAROU") == 0) {
  54.             throw new ConfigurationException("Wolf or Villager amount cannot be equals to 0 !");
  55.         }
  56.         if (map.get("LOUP-GAROU") >= map.get("SIMPLE_VILLAGEOIS")) {
  57.             throw new ConfigurationException("Wolf amount must be lower than Villager !");
  58.         }
  59.     }
  60.  
  61.     public Integer get(String key) {
  62.         return map.get(key);
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement