Biblioteca Java - Blame information for rev 2
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mihai | 1 | package colectii.map; |
2 | |||
3 | import java.util.*; | ||
4 | import java.io.*; | ||
5 | |||
6 | public class Dictionar2 { | ||
7 | |||
8 | HashMap dct = new HashMap(); | ||
9 | |||
10 | public void adaugaCuvant(Cuvant c, String definitie) { | ||
11 | |||
12 | if(dct.containsKey(c)){ | ||
13 | System.out.println("Modific cuvant existent!"); | ||
14 | } | ||
15 | else | ||
16 | { | ||
17 | System.out.println("Adauga cuvant nou."); | ||
18 | } | ||
19 | dct.put(c, definitie); | ||
20 | |||
21 | } | ||
22 | |||
23 | public String cautaCuvant(Cuvant c) { | ||
24 | return (String)dct.get(c); | ||
25 | } | ||
26 | |||
27 | public void afisDictionar() { | ||
28 | System.out.println(dct); | ||
29 | } | ||
30 | |||
31 | |||
32 | public static void main(String args[]) throws Exception { | ||
33 | Dictionar2 dict = new Dictionar2(); | ||
34 | char raspuns; | ||
35 | String linie, explic; | ||
36 | BufferedReader fluxIn = new BufferedReader(new InputStreamReader(System.in)); | ||
37 | |||
38 | do { | ||
39 | System.out.println("Meniu"); | ||
40 | System.out.println("a - Adauga cuvant"); | ||
41 | System.out.println("c - Cauta cuvant"); | ||
42 | System.out.println("l - Listeaza dictionar"); | ||
43 | System.out.println("e - Iesi"); | ||
44 | |||
45 | linie = fluxIn.readLine(); | ||
46 | raspuns = linie.charAt(0); | ||
47 | |||
48 | switch(raspuns) { | ||
49 | case 'a': case 'A': | ||
50 | System.out.println("Introduceti cuvantul:"); | ||
51 | linie = fluxIn.readLine(); | ||
52 | if( linie.length()>1) { | ||
53 | System.out.println("Introduceti definitia:"); | ||
54 | explic = fluxIn.readLine(); | ||
55 | dict.adaugaCuvant(new Cuvant(linie), explic); | ||
56 | } | ||
57 | break; | ||
58 | case 'c': case 'C': | ||
59 | System.out.println("Cuvant cautat:"); | ||
60 | linie = fluxIn.readLine(); | ||
61 | Cuvant x = new Cuvant(linie); | ||
62 | if( linie.length()>1) { | ||
63 | explic = dict.cautaCuvant(x); | ||
64 | if (explic == null) | ||
65 | System.out.println("nu exista"); | ||
66 | else | ||
67 | System.out.println("Explicatie:"+explic); | ||
68 | } | ||
69 | break; | ||
70 | case 'l': case 'L': | ||
71 | System.out.println("Afiseaza:"); | ||
72 | dict.afisDictionar(); | ||
73 | break; | ||
74 | |||
75 | } | ||
76 | } while(raspuns!='e' && raspuns!='E'); | ||
77 | System.out.println("Program terminat."); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | class Cuvant{ | ||
82 | String c; | ||
83 | public Cuvant(String c) { | ||
84 | this.c = c; | ||
85 | } | ||
86 | |||
87 | public boolean equals(Object obj) { | ||
88 | return c.equals((String)obj); | ||
89 | } | ||
90 | |||
91 | public int hashCode() { | ||
92 | return (int)(Math.random()*c.length()*1000); | ||
93 | } | ||
94 | |||
95 | public String toString() { | ||
96 | return c; | ||
97 | } | ||
98 | } |