一、操作步骤
1.在百度上把所有的单词全部分别复制到26个文本文件中,对应首字母编号
参考链接: http://www.517ming.com/a-kaitou-danci.html
2. 创建一个父类Words
3. 创建26个首字母子类继承Words,并通过BufferedReader将文本文件中的信息读入
4. 将读入的信息进行分割, 因为文件中的格式为 1 nadir n. 最低点,无底 (主要是空格的个数是可变的!)
5. 创建其他需要用到的类,详细信息看以下代码
二、Words类
介绍: Words类是其它26个首字母(A-Z)的父类
这个类代码比较简单,只是为了方便利用多态查询对应的信息
package cn.com.dictionary;
import java.util.LinkedHashMap;
class Words {
public final LinkedHashMap<MyString, String> map = new LinkedHashMap<MyString, String>();
public LinkedHashMap<MyString, String> map() {
return map;
}
}
三、首字母类(A-Z)
由于类比较多,所有就将26个首字母类选取其中一个作为模板展示,其它类都是这个模板
package cn.com.dictionary;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
final class A extends Words{
private static final A A = new A();
private A() {
//a.txt文本文件相当于一个数据库吧,里面存储的是所有首字母a开头的单词及对应的中文翻译
try (FileReader fr = new FileReader("E:/Workspace/javacode/src/cn/com/words/a.txt");
BufferedReader br = new BufferedReader(fr);) {
while(true) {
// 按行读取数据
String str = br.readLine();
if(str == null) break;
ArrayList<String> list = new ArrayList<>();
/* 将读取到的数据按照空格分割,由于空格个数不一致,所以这里写成str.split("\\s+");
\s表示空格,+号表示空格的个数,是可变的
*/
String []ss = str.split("\\s+");
//利用trim将分割后的数组除空判断长度是否为0;如果不为0,则不是空字符串
for(String s:ss){
if(s.trim().length()!=0) {
list.add(s);
}
}
// 第一个元素表示的是编号,周树人曾说过:没用的东西就得把它删掉
list.remove(0);
/*
经过测试我发现,这格式有时候会变成这样一种样子,例如: 1 dog n. 狗
1 或者是 1
dog dog
n. n.狗
狗
所以这里得分情况存储
*/
if (list.size() == 3) {
String s1 = list.get(2);
String s2 = list.get(1);
String s = s2+s1;
list.set(2, s);
list.remove(1);
}
// 由于后面需要用到containsKey,所以写了一个String的包装类,用来无视 key 的大小写
map.put(new MyString(list.get(0)), list.get(1));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static A getA() {
return A;
}
}
四、Indexes类
介绍:这个类是用来通过首字母查询单词的索引,也就相当于目录吧
package cn.com.dictionary;
import java.util.LinkedHashMap;
public class Indexes {
public static final LinkedHashMap<Character, Words> map = new LinkedHashMap<Character, Words>();
static {
map.put('A', A.getA());
map.put('B', B.getB());
map.put('C', C.getC());
map.put('D', D.getD());
map.put('E', E.getE());
map.put('F', F.getF());
map.put('G', G.getG());
map.put('H', H.getH());
map.put('I', I.getI());
map.put('J', J.getJ());
map.put('K', K.getK());
map.put('L', L.getL());
map.put('M', M.getM());
map.put('N', N.getN());
map.put('O', O.getO());
map.put('P', P.getP());
map.put('Q', Q.getQ());
map.put('R', R.getR());
map.put('S', S.getS());
map.put('T', T.getT());
map.put('U', U.getU());
map.put('V', V.getV());
map.put('W', W.getW());
map.put('X', X.getX());
map.put('Y', Y.getY());
map.put('Z', Z.getZ());
}
}
五、MyString类
介绍: 这是一个String的封装类,为了到时候无视key的大小写问题
package cn.com.dictionary;
public class MyString {
private final String s;
public MyString(String s) {
super();
this.s = s;
}
public MyString() {
super();
s = null;
}
public String getS() {
return s;
}
@Override
public int hashCode() {
return s.toLowerCase().hashCode();
}
@Override
public boolean equals(Object obj) {
MyString other = (MyString) obj;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equalsIgnoreCase(other.s))
return false;
return true;
}
@Override
public String toString() {
return s;
}
}
六、测试类
package cn.com.dictionary;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
public class TestDictionary {
private static Scanner s = new Scanner(System.in);
private static LinkedHashMap<Character, Words> map = Indexes.map;
public static void main(String[] args) {
menu();
}
public static void menu() {
boolean bn;
do {
bn = false;
System.out.println("欢迎使用 XW 英汉词典");
System.out.println("1-首字母查询 2-单词翻译");
int num = s.nextInt();
if (num == 1)
byFirstWords();
else if (num == 2)
byWords();
else {
System.out.println("输入有误!");
}
System.out.println("继续查询-y 退出-n");
char c = s.next().charAt(0);
if (c == 'y' || c == 'Y') {
bn = true;
}
} while (bn);
}
// 实现英译汉的功能类
public static void byWords() {
boolean bn;
do {
bn = false;
System.out.println("请输入你要查询的单词");
MyString words = new MyString(s.next());
for (char c : words.getS().toCharArray()) {
if (!ifLetter(c)) {
bn = true;
break;
}
}
if (!bn) {
char c = words.getS().charAt(0);
if (c >= 'a') {
c = (char) (c - 32);
}
if (map.get(c).map().containsKey(words)) {
System.out.println(map.get(c).map().get(words).trim());
} else {
System.out.println(words);
}
} else {
System.out.println("输入有误!");
System.out.println("重新查询-y 退出-n");
char c = s.next().charAt(0);
bn = (c == 'y' || c == 'Y') ? true : false;
}
} while (bn);
}
// 实现首字母查询的功能类
public static void byFirstWords() {
boolean bn;
do {
bn = false;
System.out.println("请输入你要查询单词的首字母:");
char c = s.next().charAt(0);
if (ifLetter(c)) {
if (c >= 'a') {
c = (char) (c - 32);
}
LinkedList<MyString> list = new LinkedList<MyString>();
int count;
Words w = map.get(c);
Set<MyString> set = w.map().keySet();
do {
count = 0;
bn = false;
System.out.println("------" + c + "------");
for (MyString c1 : set) {
list.add(c1);
count++;
System.out.println(count + ":" + c1);
}
System.out.println("请输入对应的单词编号:");
int num = s.nextInt();
if (num <= list.size() && num >= 1) {
MyString key = list.get(num - 1);
System.out.println(w.map().get(key).trim());
} else {
System.out.println("输入有误!");
System.out.println("重新输入编号-y 退出-n");
c = s.next().charAt(0);
bn = (c == 'y' || c == 'Y') ? true : false;
}
} while (bn);
} else {
System.out.println("输入有误!");
System.out.println("重新查询-y 退出-n");
c = s.next().charAt(0);
bn = (c == 'y' || c == 'Y') ? true : false;
}
} while (bn);
}
public static boolean ifLetter(char c) {
return (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') ? true : false;
}
}
代码效果
由于还没学到数据库,所有只能以这种方式来实现英汉互译的功能,有些不足的地方,欢迎大家指出