编写程序,实现具有如下功能的交互式词典:(1)可以查询每个单词的解释。例如:输入“Hello”将显示意思“a greeting”(2)能够加入新的单词和解释(3)能够删除单词和解释(4)能够显示所有

题目:
编写程序,实现具有如下功能的交互式词典:
(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;
    }
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java课程设计实训大作业:记事本+简易计算器+聊天系统+日历+中英查询 基础任务一:设计日历软件 根据如下图,综合运用GUI编程、事件处理、Calendar类应用等知识设计一款月历,要求能通过输入(或选择)年月的方式正确显示当前月份的所有日期。 基础任务二:设计中英查询软件 根据Java面向对象程序设计相关理论,及GUI编程、事件处理、数据库编程等技术,设计一个如下图所示的“中英文释义查询”程序。输入英文单词查询数据库将对应的中文显示在下框中;输入中文,查询数据库将对应的英文单词显示在下框中。 提升任务三:设计简易记事本软件 1.使用Java图形界面组件设计记事本软件的界面,参考如图所示。 2.程序代码规范,逻辑正确,能够正常运行。 3.“文件”菜,包括“建”、“打开”、“保存”、“另存为”和“退出”等功能。 提升任务四:设计简易计算器软件 1.使用Java图形界面组件设计软件,界面如图所示。 2.软件能够满足基本的“加、减、乘、除”等运算要求。 3.程序代码清晰,语法规范,结构合理,逻辑正确。 进阶任务五:自选主题开发一个应用软件(如在线聊天系统,学籍管理系统等)下面给的软件界面只是参考,同学们可以根据自己的想法进行设计。 1.软件界面美观、功能完善软件,导航清晰,操作方便,使用菜栏、工具栏、布局管理器、按钮、表格等多种Java图形界面组件。 2.程序代码清晰,语法规范,结构合理,逻辑正确。 3.功能完善,程序代码优化,执行效率高,具有较好可维护性和可扩展性。 4.软件功能设计具有一定的难度和创意。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值