java0004——迷你小项目--英汉小字典

  1. 流程图
  • 代码思路流程图
    在这里插入图片描述
  • 代码设计流程图
    在这里插入图片描述
  1. 注意的问题
  • Scanner next()与nextline()的区别 :
    一句话 nextline()可以输入空格、tab等特殊的字符,而且还可以避免在连续输入多个字符串的时候出现输入问题
  • 父类与子类在这里尽量不要重名
  1. 代码
  • Word.java
package MyDictionary;

public class Word {
	String eWord;
	String cWord;

	public String geteWord() {
		return eWord;
	}

	public void seteWord(String eWord) {
		this.eWord = eWord;
	}

	public String getcWord() {
		return cWord;
	}

	public void setcWord(String cWord) {
		this.cWord = cWord;
	}

	public Word(String eWord, String cWord) {
		this.eWord = eWord;
		this.cWord = cWord;
	}

	public Word() {
		this.eWord = null;
		this.cWord = null;
	}

	public String toString() {
		return (this.eWord + "===" + this.cWord);
	}
}
  • dictionaryFuction .java
package MyDictionary;
public class dictionaryFuction {
	Word[] words;
	int count;

	public dictionaryFuction() {
		// TODO Auto-generated constructor stub
		words = new Word[100];
		count = 0;
	}

	// 添加单词
	public boolean add(Word word) {
		if (count < 100) {
			this.words[this.count++] = word;
			return true;
		} else
			return false;
	}

	// 查询单词
	public Word searchWord(String word) {
		for (int i = 0; i < count; i++) {
			if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word))
				return words[i];
		}
		return null;
	}

	// 显示所有单词
	public void listAllWords() {
		System.out.println("以下是所有的单词");
		for (int i=0;i<count;i++)
			System.out.println(this.words[i].toString());
	}

	// 删除单词
	public boolean deleteWords(String word) {
		int location = -1;
		// 寻找要删除的元素,返回true
		for (int i = 0; i < count; i++) {
			if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word)) {
				location = i;
				break;
			}
		}
		// 找到删除,如果是最后一个元素,count--直接覆盖
		if (location >= 0) {
			for (int i = location; i < count - 1; i++) {
				words[i] = words[i + 1];
			}
			count--;
			return true;
		}
		// 找不到返回false
		else
			return false;
	}

	// 根据中文单词的含义,更新词典中的单词
	public boolean updateWord(Word word) {
		if (word != null) {
			int i = indexOf(word.getcWord());
			if (i >= 0) {
				this.words[i] = word;
				return true;
			} else
				return false;
		} else
			return false;
	}

	// 判断单词的中文,显示在列表中的位置
	private int indexOf(String str) {
		// TODO Auto-generated method stub
		if (str == null)
			return -1;
		else {
			for (int i = 0; i < this.count; i++) {
				if (this.words[i].getcWord().equals(str))
					return i;
			}
			return -1;
		}
	}
}
  • LookUpDictionary .java
package MyDictionary;
import java.util.*;
public class LookUpDictionary {
	dictionaryFuction my;
	Scanner input;
	public LookUpDictionary() {
		// TODO Auto-generated constructor stub
		my = new dictionaryFuction();
		input = new Scanner(System.in);
	}
	// 开始
	public void welcome() {
		System.out.println("****************");
		System.out.println("欢迎使用我的字典");
		System.out.println("****************");
	}
	// 查询功能
	public void lookUp() {
		char choice;
		do {
			System.out.println("1、查单词");
			System.out.println("2、加单词");
			System.out.println("3、删除单词");
			System.out.println("4、产看所有单词");
			System.out.println("5、修改单词");
			System.out.println("0、退出");
			choice = input.nextLine().charAt(0);
			switch (choice) {
			case '1':
				showWord();
				break;
			case '2':
				showAdd();
				break;
			case '3':
				showDel();
				break;
			case '4':
				my.listAllWords();
				break;
			case '5':
				showUpdata();
				break;
			default:
				break;
			}	
		} while (choice != '0');
	}
	public void showWord() {
		System.out.println("请输入你要产找单词的中英文");
		String str = input.nextLine();
		Word word = my.searchWord(str);
		if (word != null) {
			System.out.println("产找单词的中英文含义为:");
			System.out.println(word.toString());
		} else
			System.out.println("这个单词没找到");
	}
	public void showAdd() {
		if (my.count < 100) {
			System.out.println("请输入你想要添加单词的中文含义");
			String Cword = input.nextLine();
			System.out.println("请输入你想要添加单词的英文含义");
			String Eword = input.nextLine();
			Word word = new Word(Eword, Cword);
			my.add(word);
			System.out.println("添加单词成功");
		} else
			System.out.println("字典已满");
	}
	public void showDel() {
		System.out.println("请输入你要删除的单词的含义");
		String string = input.nextLine();
		boolean ifDel = my.deleteWords(string);
		if (ifDel)
			System.out.println("删除成功");
		else
			System.out.println("删除失败!!!");
	}
	public void showUpdata() {
		System.out.println("仅能修改单词的英文含义");
		System.out.println("请输入你想要修改单词的中文含义");
		String cStr = input.nextLine();
		System.out.println("请输入单词修改后的英文含义");
		String eStr = input.nextLine();
		Word word = new Word(eStr, cStr);
		if (my.updateWord(word))
			System.out.println("单词修改成功");
		else
			System.out.println("词典中没有这个单词");
	}
	// 结束
	public void bye() {
		System.out.println("****************");
		System.out.println("Bye —Bye");
		System.out.println("****************");
	}
	public void run() {
		welcome();
		lookUp();
		bye();
	}
}
  • dictionaryDemo .java
package MyDictionary;
public class dictionaryDemo {
	public static void main(String[] args) {
		LookUpDictionary Demo =new LookUpDictionary();
		Demo.run();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41448891/article/details/82804504
今日推荐