题目:
编写程序,实现具有如下功能的交互式词典:
(1)可以查询每个单词的解释。例如:输入“Hello”将显示意思“a greeting”;
(2)能够加入新的单词和解释;;
(3)能够删除单词和解释;
(4)能够显示所有单词和解释
(5)将所有单词和解释保存在一个文件中,程序运行时可以读取。
(提示:编写一个词典WordDictionary类,包含查询单词的解释、增加单词和解释、删除单词和解释、显示所有单词和解释和从文件读取词典以及将词典保存到文件等方法。在该类中可以使集合类对象保存单词和解释,使用对象序列化和反序列化保存和读取。)
分析
编写一个词典WordDictionary类,包含查询单词的解释、增加单词和解释、删除单词和解释、显示所有单词和解释和从文件读取词典以及将词典保存到文件等方法。在该类中可以使集合类对象保存单词和解释,使用对象序列化和反序列化保存和读取。)
代码实现
测试类
import java.util.Collection;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class DictionariesTest {
static WordDictionary Dictionary = new WordDictionary();
static Scanner reader = new Scanner(System.in);
private static void print() {
Collection<Word> collection = Dictionary.wordsCollection();
if (collection.isEmpty()) {
System.out.println("没有单词!");
return;
}
Iterator<Word> iter = collection.iterator();
while (iter.hasNext()) {// 使用迭代器对象遍历 collection 中的元素
Word word = iter.next();
System.out.print("单词:" + word.getName()+" ");
System.out.println("解释:" + word.getExplain());
}
}
private static void find() {
System.out.print(">请输入要查找的单词:");
String name = reader.nextLine().trim();
Word word = Dictionary.find(name);
if (word == null) {
System.out.println("查无此单词!");
} else {
System.out.println("单词解释是:" + word.getExplain());
}
}
private static void add() {
System.out.print(">请输入要添加单词名字:");
String name = reader.nextLine().trim();
System.out.print(">请输入单词解释:");
String explain = reader.nextLine();
Dictionary.add(new Word(name, explain));
}
private static void del() {
System.out.print(">请输入要删除的单词:");
String name = reader.nextLine().trim();
if (Dictionary.del(name)) {
System.out.println("删除成功!");
} else {
System.out.println("没有此单词!");
}
}
public static void main(String args[]) {
int number = 0;
while (true) {
System.out.println("0:退出 1:添加 2:查找 3:删除 4:浏览");
System.out.print(">请选择一个数字:");
try {
number = reader.nextInt(); // 从键盘接收一个整数
reader.nextLine();// 吸收掉输入整数后的回车符
} catch (InputMismatchException e) {
System.out.println("输入的不是数字!");
reader.nextLine();// 吸收掉输入的整数和回车符
continue;// 越过后面的语句,重新循环
}
switch (number) {
case 0:
Dictionary.save();
return;
case 1:
add();//添加
break;
case 2:
find();//查找
break;
case 3:
del();//删除
break;
case 4:
print();//浏览
break;
}
}
}
}
字典类
import java.io.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class WordDictionary {
private Collection<Word> collection = null;
public WordDictionary() {
load();//加载文件中的数据
}
public Word find(String name) {
Iterator<Word> iter = collection.iterator();
while (iter.hasNext()) { // 使用迭代器对象遍历collection中的元素
Word word = iter.next();
if (name.equals(word.getName())) // 如果找到单词
return word; // 返回单词对象
}
return null; // 没找到单词返回空
}
public void add(Word word) {
collection.add(word);
}
public boolean del(String name) {
Iterator<Word> iter = collection.iterator();
while (iter.hasNext()) { // 使用迭代器对象遍历collection中的元素
Word word = iter.next();
if (name.equals(word.getName())){ // 如果找到单词
collection.remove(word);
return true; // 返回单词true
}
}
return false; // 没找到单词返回false
}
public Collection<Word> wordsCollection() {
return collection;
}
private void load() {
File myFile = new File("d:" + File.separator + "data.dat");
if (myFile.exists()) {
try {
ObjectInputStream si = new ObjectInputStream(
new FileInputStream(myFile));
this.collection = (Collection<Word>) si.readObject();
si.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
collection = new HashSet<Word>();
}
}
public void save() { // 保存通讯录
try {
ObjectOutputStream so = new ObjectOutputStream(
new FileOutputStream("d:" + File.separator + "data.dat"));
so.writeObject(this.collection);
so.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
单词类
import java.io.Serializable;
public class Word implements Serializable {
private static final long serialVersionUID = 1L;
private String name;//单词名字
private String explain;//单词解释
public Word(String name, String explain) {
this.name = name;
this.explain = explain;
}
public Word() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
}