주제 :
다음과 같은 기능으로 대화 형 사전을 구현하는 프로그램을 작성하십시오.
(1) 각 단어의 설명을 쿼리 할 수 있습니다. 예 : "Hello"를 입력하면 "인사"라는 의미가 표시됩니다.
(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;
}
}