Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package zad2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Map;
  10. import java.util.TreeMap;
  11.  
  12. public class Dictionary {
  13.     Map<String, ArrayList<String>> dictionary = new TreeMap<String, ArrayList<String>>();
  14.    
  15.     public Dictionary(String source){
  16.         try {
  17.             BufferedReader br = new BufferedReader(new FileReader(source));
  18.             String[] tab;
  19.             String line = "";
  20.             while ((line = br.readLine()) != null){
  21.                 tab=line.split("=");
  22.                 if(dictionary.containsKey(tab[0])==false){
  23.                     dictionary.put(tab[0], new ArrayList<String>());
  24.                     dictionary.get(tab[0]).add(tab[1].trim());
  25.                     Collections.sort(dictionary.get(tab[0]));
  26.                    
  27.                 }  
  28.                 else{
  29.                     dictionary.get(tab[0]).add(tab[1].trim());
  30.                     Collections.sort(dictionary.get(tab[0]));
  31.                 }
  32.             //  for()
  33.             }
  34.             br.close();
  35.         } catch (Exception exc) {
  36.             System.out.println("Error");
  37.         }
  38.     }
  39.    
  40.     public void show(){
  41.        
  42.             System.out.println(dictionary.values());
  43.         }
  44.    
  45.     public void lookup(String entry){
  46.         ArrayList<String> tmp = dictionary.get("Java");
  47.         System.out.println(tmp.size());
  48.         for(int i=0;i<tmp.size();i++){
  49.             System.out.println(tmp.size()+". "+tmp.get(i));
  50.         }
  51.        
  52.    
  53. //  dictionary.get(tab[0]).size()+". "+
  54.     }
  55.     public void add(String entry){
  56.         String[] tab;
  57.         tab=entry.split("=");
  58.         if(dictionary.containsKey(tab[0])){
  59.             dictionary.get(tab[0]).add(tab[1].trim());
  60.             Collections.sort(dictionary.get(tab[0]));
  61.         }
  62.         else{
  63.             dictionary.put(tab[0], new ArrayList<String>());
  64.             dictionary.get(tab[0]).add(tab[1].trim());
  65.             Collections.sort(dictionary.get(tab[0]));
  66.         }
  67.     }
  68.     public void delete(String entry, int no){
  69.         if(dictionary.containsKey(entry)){
  70.             if(dictionary.get(entry).size()==1){
  71.                  dictionary.get(entry).clear();
  72.                  dictionary.remove(entry);
  73.                  Collections.sort(dictionary.get(entry));
  74.             }
  75.             else
  76.                 dictionary.get(entry).remove(no-1);
  77.                 Collections.sort(dictionary.get(entry));
  78.         }
  79.         else
  80.             System.out.println("Brak hasla o takiej nazwie");
  81.     }
  82.     public void update(String entry, String oldDef, String newDef){
  83.         if(dictionary.containsKey(entry)){
  84.             if(dictionary.get(entry).contains(oldDef)){
  85.                 dictionary.get(entry).set(dictionary.get(entry).indexOf(oldDef), newDef);
  86.             }
  87.             else
  88.                 System.out.println("Brak takiej definicji dla tego hasla");
  89.         }
  90.         else
  91.             System.out.println("Brak hasla o takiej nazwie");
  92.     }
  93.     public void save(){
  94.         String fname = System.getProperty("user.home") + "/dictionary.txt";
  95.         try(BufferedWriter bw = new BufferedWriter(new FileWriter(fname))){
  96.            
  97.         }
  98.         }
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement