Advertisement
Guest User

Evaluations.java

a guest
Aug 6th, 2010
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5.  
  6. public class Evaluations {
  7.     Hashtable<String, Hashtable<String,Double>> mEvaluations = new Hashtable<String, Hashtable<String,Double>>();
  8.  
  9.     public Evaluations(String nomdufichier) throws IOException {
  10.         litFichier(new File(nomdufichier));
  11.     }
  12.    
  13.     public void litFichier(File fichier) throws IOException {
  14.         BufferedReader br = new BufferedReader(new FileReader(fichier));
  15.         try {
  16.           String line;
  17.           while((line = br.readLine())!= null) {
  18.             String[] vals = line.split(" ");
  19.             if(vals.length == 0) continue;
  20.             String utilisateurs = vals[0];
  21.             if(mEvaluations.contains(utilisateurs)) {
  22.                 System.err.println("La clef "+utilisateurs+" est utilisée plus d'une fois.");
  23.                 continue;
  24.             }
  25.             Hashtable<String,Double> notes = new Hashtable<String,Double>();
  26.             for(int k = 1; k< vals.length;++k) {
  27.                 String[] n = vals[k].split(":");
  28.                 if(n.length != 2) {
  29.                     System.err.println("fichier mal formé? "+n[k]);
  30.                     continue;
  31.                 }
  32.                 Double d = new Double(n[1]);
  33.                 notes.put(n[0],d);
  34.             }
  35.             mEvaluations.put(utilisateurs, notes);
  36.           }
  37.         } finally {
  38.             br.close();
  39.         }
  40.     }
  41.    
  42.     public Set<String> utilisateurs() {
  43.         return mEvaluations.keySet();
  44.     }
  45.  
  46.     public Set<Map.Entry<String,Double>> evaluations(String nom) {
  47.         return mEvaluations.get(nom).entrySet();
  48.     }
  49.    
  50.     public static void main(String[] args) throws IOException {
  51.         Evaluations e = new Evaluations(args[0]);
  52.         for (String nom: e.utilisateurs()) {
  53.             System.out.println("Utilisateur "+nom);
  54.             for( Map.Entry<String,Double> me : e.evaluations(nom)) {
  55.                 System.out.println("\taccorde une note de "+me.getValue()+" à l'article "+me.getKey());
  56.             }
  57.         }
  58.     }
  59.    
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement