暑期第三周总结


  本周是数据结构的第二阶段。

  我选择的是背单词系统,用到了我从来没有使用过的新的jar包json包,里面的函数用来实现对文件的操作十分好用

package EngWord;

import com.alibaba.fastjson.JSON;
import little.EnglishWord.test_.Person;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

public class Tools {
    public static String file_path = "C://Users//xiaoguoguo//Desktop//小学期//数据结构小学期//Word.txt";
    private static String read_file() throws IOException {
        File my_file = create_file_obj();
        String content = null;
        StringBuilder result = new StringBuilder();
        BufferedReader br = new BufferedReader(new FileReader(my_file));
        String s = null;
        while ((s = br.readLine()) != null) {
            result.append(s);
        }
        br.close();
        content = result.toString();
        return content;
    }

    // 写文件
    private static void write_file(String content) throws IOException {
        File file = create_file_obj();
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(content);
        writer.flush();
        writer.close();
    }

    // 创建文件对象
    private static File create_file_obj() {
        File my_file = new File(file_path);
        return my_file;
    }

    // 将 Word 类 数组对象 转变成 字符串的方法
    private static String arr_to_string(ArrayList<Word> wrd_arr) {
        String res = JSON.toJSONString(wrd_arr);
        return res;
    }

    // 将 Word 数组字符串 装变成 对象的方法
    private static ArrayList<Word> str_to_arr(String content) {
        ArrayList<Word> arr = (ArrayList<Word>) JSON.parseArray(content.toString(),Word.class);
        return arr;
    }
    // 从 文件中 获得 数组对象
    private static ArrayList<Word> file_to_arr() throws IOException {
        String file_con = read_file();
        ArrayList<Word> wrd_arr = str_to_arr(file_con);
        return wrd_arr;
    }

    // 将 对象数组 存入文件
    private static void arr_to_file(ArrayList<Word> wrd_arr) throws IOException {
        String arr_str = arr_to_string(wrd_arr);
        write_file(arr_str);
    }
    // 添加 新人
    public static void add_new_wrd(Word wrd) throws IOException {
        ArrayList<Word> wrd_arr = file_to_arr();
        wrd_arr.add(wrd);
        arr_to_file(wrd_arr);
        System.out.println("添加单词成功!");
    }

    // 查询 中文 单词
    public static String show_wrd_bychinese(String chinese) throws IOException {
        String res = null;
        ArrayList<Word> wrd_arr = file_to_arr();
        for (int i = 0; i < wrd_arr.size(); i++) {
            if(chinese.equals(wrd_arr.get(i).getChinese())){
                res = "单词:" + wrd_arr.get(i).getEnglish() + "词性:" + wrd_arr.get(i).getCixing() +
                        " 释义:" + wrd_arr.get(i).getChinese() + "例句:" + wrd_arr.get(i).getExample();
                System.out.println(res);
                break;
            }
        }
        return res;
    }

    // 查询 英文 单词
    public static String show_wrd_byenglish(String english) throws IOException {
        String res = null;
        ArrayList<Word> wrd_arr = file_to_arr();
        int index = -1;
        for (int i = 0; i < wrd_arr.size(); i++) {
            if(english.equals(wrd_arr.get(i).getEnglish())){
                res = "单词:" + wrd_arr.get(i).getEnglish() + "词性:" + wrd_arr.get(i).getCixing() +
                        " 释义:" + wrd_arr.get(i).getChinese() + "例句:" + wrd_arr.get(i).getExample();
                System.out.println(res);
                index = 1;
                break;
            }
        }
        if (res == null) {
            System.out.println("词库无单词...");
        }
        return res;
    }
    // 删除 单词
    public static void delete_wrd(String english) throws IOException {
        ArrayList<Word> wrd_arr = file_to_arr();
        int english_index = -1;
        for (int i = 0; i < wrd_arr.size(); i++) {
            if(english.equals(wrd_arr.get(i).getEnglish())){
                english_index = i;
                break;
            }
        }
        if (english_index == -1 ){
            System.out.println("没有此单词...");
        } else {
            wrd_arr.remove((english_index));
            System.out.println("删除成功!");
            arr_to_file(wrd_arr);
        }
    }

    // 修改 单词
    public static void update_wrd(String english,String chinese) throws IOException {
        ArrayList<Word> wrd_arr = file_to_arr();
        for (int i = 0; i < wrd_arr.size(); i++) {
            if(english.equals(wrd_arr.get(i).getEnglish())){
                wrd_arr.get(i).setChinese(chinese);
                System.out.println(wrd_arr.get(i).getChinese());
                System.out.println("修改成功....");
                arr_to_file(wrd_arr);
                break;
            }
        }
    }

    // 查询 全部
    public static String show_all_wrd() throws IOException {
        String res = null;
        ArrayList<Word> wrd_arr = file_to_arr();
        for (int i = 0;i < wrd_arr.size();i++){
            System.out.println("单词 :" + wrd_arr.get(i).getEnglish() + " 词性:" + wrd_arr.get(i).getCixing()+" 释义:" +
                    wrd_arr.get(i).getChinese() + " 例句:" + wrd_arr.get(i).getExample());
        }
        return res;
    }
    // 背单词
    public static void recite_wrd(String english,String chinese) throws IOException {
        ArrayList<Word> wrd_arr = file_to_arr();
        int flag = -1;
        for (int i = 0; i < wrd_arr.size(); i++) {
            if(english.equals(wrd_arr.get(i).getEnglish()) && chinese.equals(wrd_arr.get(i).getChinese())){
               wrd_arr.get(i).setVisited(1);
                //System.out.println(wrd_arr.get(i).getChinese());
                System.out.println("记忆成功!");
                arr_to_file(wrd_arr);
                flag = 1;
                break;
            }
        }
        if(flag == -1) {
            System.out.println("出错了哦,继续努力!");
        }
    }

    // 生词本
    public static String show_new_wrd() throws IOException {
        String res = null;
        ArrayList<Word> wrd_arr = file_to_arr();
        for (int i = 0;i < wrd_arr.size();i++){
            if (wrd_arr.get(i).getVisited() == 0){
                System.out.println("单词 :" + wrd_arr.get(i).getEnglish() + " 词性:" + wrd_arr.get(i).getCixing()+" 释义:" +
                        wrd_arr.get(i).getChinese() + " 例句:" + wrd_arr.get(i).getExample());
            }
        }
        return res;
    }
    public static void rate_wrd() throws IOException {
        ArrayList<Word> wrd_arr = file_to_arr();
        int num = 0;
        for (int i = 0;i < wrd_arr.size();i++){
            if (wrd_arr.get(i).getVisited() == 1){
                num++;
            }
        }

        System.out.println("记忆进度:" + num + " / " + wrd_arr.size());
    }
}

  这里实现了单词的录入,删除,查看,根据英文看释义,根据释义差单词,也粗略的完成了背单词功能,单词记忆进度功能。。。

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/274939.html

(0)
上一篇 2022年7月17日
下一篇 2022年7月17日

相关推荐

发表回复

登录后才能评论