编写一个程序,实现具有如下功能的交互式字典:(1)可以查询每个单词的解释(2)能够加入新的单词和解释(3)能够删除单词和解释(4)能够显示所有的单词和解释(5)将所有的单词和解释保存在一个文件中

import java.io.Serializable;


@SuppressWarnings("serial")
public class Word implements Serializable {
private String word;
    private String explain;


    public Word() {
        }


public String getWord() {
return word;
}


public String getExplain() {
return explain;
}


public Word(String word, String explain) {
this.word= word;
this.explain= explain;
}
}
import java.io.*;
import java.util.*;


public class WordDictionary {
private Collection<Word>collection=null;
    public WordDictionary(){
        load();  //加载列表
}
public void add(Word word) {// 添加单词
collection.add(word);
System.out.println("添加成功!");
}
public Word find(String words){//查找单词
        Iterator<Word>iterator=collection.iterator();
        while (iterator.hasNext()){
            Word w=iterator.next();
            if (words.equals(w.getWord()))
            
return w;
}
return null;
}
public boolean del(String words) {//删除单词
        Iterator<Word> iterator = collection.iterator();
        while (iterator.hasNext()) {
            Word x = iterator.next();
            if (words.equals(x.getWord())) {
collection.remove(x);
                return true;
}
        }
return false;
}

public Collection<Word>wordCollection() {//返回全部单词
	System.out.println("全部单词:");
return collection;
}

@SuppressWarnings("unchecked")
public void load(){
        File file=new File("D:"+File.separator+"英汉词典.txt");
        if (file.exists()){
try{
ObjectInputStream si=new ObjectInputStream(new FileInputStream(file));
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:/dictionary.txt"));
so.writeObject(this.collection);
so.close();
}catch(IOException e){
e.printStackTrace();
}
    }
}
import java.util.Collection;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;

public class Dictionary {

static WordDictionary wordDictionary= new WordDictionary();
    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {
System.out.println("英汉词典:");
        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:
wordDictionary.save();
                    return;
                case 1:
     add();
                    break;
                case 2:
     find();
                    break;
                case 3:
     del();
                    break;
                case 4:
     print();
                    break;
}
        }
    }
public static void add(){
System.out.println("输入要增加的单词:");
String word=reader.nextLine();
System.out.println("输入要增加的解释:");
String explain=reader.nextLine();
wordDictionary.add(new Word(word,explain));
}
public static void find(){
System.out.println("输入要查询的单词:");
String word=reader.nextLine();
Word words=wordDictionary.find(word);
        if(words==null){
System.out.println("没有这个单词!");
} else {
System.out.println("单词解释:"+words.getExplain());
}
    }
public static void del(){
System.out.println("输入要删除的单词:");
String word=reader.nextLine();
        if (wordDictionary.del(word)){
System.out.println("删除成功!");
}else {
System.out.println("没有这个单词!");
}
    }
public static void print(){
        Collection<Word>collection=wordDictionary.wordCollection();
        if (collection.isEmpty()){
System.out.println("单词库为空!!");
            return;
}
        Iterator<Word>iterator=collection.iterator();
        while (iterator.hasNext()){
            Word w=iterator.next();
System.out.print("单词:"+w.getWord());
System.out.println(" ; 解释:"+w.getExplain());
}
    }

}
需要建立三个类,不要将代码都放到一个类里面!
下面是程序运行后的结果!



发布了22 篇原创文章 · 获赞 17 · 访问量 5865

猜你喜欢

转载自blog.csdn.net/weixin_42193813/article/details/80470663