-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDictionaryManagement.java
More file actions
155 lines (146 loc) · 6.3 KB
/
Copy pathDictionaryManagement.java
File metadata and controls
155 lines (146 loc) · 6.3 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class DictionaryManagement {
public void insertFromCommandline() {
Scanner scan = new Scanner(System.in);
System.out.println("-----------Thêm từ mới----------");
System.out.println("Nhập số lượng từ vựng muốn thêm:");
int numAdd = scan.nextInt();
scan.nextLine();
for (int i = 0; i < numAdd; i++) {
Word input = new Word();
System.out.println("Nhập từ tiếng anh:");
input.setWord_target(scan.nextLine());
System.out.println("Nhập từ giải nghĩa:");
input.setWord_explain(scan.nextLine());
Dictionary.arrWord.add(input);
}
}
public void insertFromFile() {
File file = new File("dictionaries.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String str = "";
while ((str = reader.readLine()) != null) {
Word word = new Word();
word.setWord_target(str.substring(0, str.indexOf("\t")));
word.setWord_explain(str.substring(str.indexOf("\t") + 1));
Dictionary.arrWord.add(word);
}
} catch (Exception e) {
System.out.println("Lỗi đọc file!!!" + e);
}
}
public char ASC(char a) {
char b = a;
if ((a > 64) && (a < 91)) {
return (char) (b + 32);
}
return b;
}
public boolean same(String a, Word b) {
int min;
if (a.length() >= b.getWord_target().length()) {
min = b.getWord_target().length();
} else {
min = a.length();
}
for (int i = 0; i < min; i++) {
if ((int) ASC(a.charAt(i)) != (int) ASC(b.getWord_target().charAt(i))) {
return false;
}
}
return true;
}
public void dictionaryLookup() {
System.out.println("-----------Look Up-----------");
System.out.println("Nhập từ cần tra cứu: ");
Scanner scan = new Scanner(System.in);
String wordLookUp = scan.nextLine();
int temp = 0;
for (int i = 0; i < Dictionary.arrWord.size(); i++) {
if ((same(wordLookUp, Dictionary.arrWord.get(i))) && (wordLookUp.length() == Dictionary.arrWord.get(i).getWord_target().length())) {
temp++;
System.out.println("Nghĩa của từ vừa nhập là: " + Dictionary.arrWord.get(i).getWord_explain());
}
}
if (temp == 0) {
System.out.println("Không tìm thấy từ này!!!");
}
}
public void addWord() {
Scanner scan = new Scanner(System.in);
System.out.println("-----------Thêm từ mới----------");
System.out.println("Nhập số lượng từ vựng muốn thêm:");
int numAdd = scan.nextInt();
scan.nextLine();
for (int i = 0; i < numAdd; i++) {
Word input = new Word();
System.out.println("Nhập từ tiếng anh muốn thêm:");
input.setWord_target(scan.nextLine());
System.out.println("Nhập từ giải nghĩa:");
input.setWord_explain(scan.nextLine());
Dictionary.arrWord.add(input);
System.out.println("Đã thêm từ: " + input.getWord_target() + " vào từ điển." );
}
}
public void deleteWord() {
Scanner scan = new Scanner(System.in);
System.out.println("-----------Xoá----------");
System.out.println("Nhập số lượng từ vựng muốn xoá:");
int numDelete = scan.nextInt();
scan.nextLine();
for (int i = 0; i < numDelete; i++) {
System.out.println("Nhập từ cần xóa: ");
String delete = scan.next();
int temp = 0;
for (int j = 0; j < Dictionary.arrWord.size(); j++) {
if ((same(delete, Dictionary.arrWord.get(j))) && (delete.length() == Dictionary.arrWord.get(j).getWord_target().length())) {
temp++;
//System.out.println(Dictionary.arrWord.get(j).getWord_target() + " " + delete.length());
System.out.println("Đã xóa từ: " + Dictionary.arrWord.get(j).getWord_target());
Dictionary.arrWord.remove(Dictionary.arrWord.get(j));
}
}
if (temp == 0) {
System.out.println("Không tìm thấy từ này!!!");
}
}
}
public void repairWord() {
Scanner scan = new Scanner(System.in);
System.out.println("-----------Sửa từ vựng----------");
System.out.println("Nhập số lượng từ vựng muốn sửa:");
int numRepair = scan.nextInt();
scan.nextLine();
for (int i = 0; i < numRepair; i++) {
System.out.println("Nhập từ cần sửa: ");
String repair = scan.next();
int temp = 0;
for (int j = 0; j < Dictionary.arrWord.size(); j++) {
if (repair.equalsIgnoreCase(Dictionary.arrWord.get(j).getWord_target())) {
temp++;
System.out.println("Nhập nghĩa mới của từ: ");
scan.nextLine();
Dictionary.arrWord.get(j).setWord_explain(scan.nextLine());
System.out.println("Đã sửa từ: " + Dictionary.arrWord.get(j).getWord_target());
}
}
if (temp == 0) {
System.out.println("Không tìm thấy từ này!!!");
}
}
}
public void dictionaryExportToFile() {
File file = new File("outdictionary.txt");
try (PrintWriter print = new PrintWriter(file)) {
for (int i = 0; i < Dictionary.arrWord.size(); i++) {
print.println(Dictionary.arrWord.get(i).getWord_target() + "\t" + Dictionary.arrWord.get(i).getWord_explain());
}
System.out.println("In ra file thành công");
} catch (Exception e) {
System.out.println("Lỗi trong quá trình ghi ra file");
}
}
}