トピック:
次の機能を備えたインタラクティブ辞書を実現するプログラムを作成します。
(1)各単語の説明を照会できます。例:「こんにちは」と入力すると、「挨拶」の意味が表示されます。
(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;
}
}