📄 dict.java
字号:
import java.io.*;
/**
* Operation transaction hierarchy: realize the dictionary function.<br>
* 1.Add a new word;<br>
* 2.Search a word;<br>
* 3.Recite all the words.<br>
* 4.Find out the words recited most.<br>
* 5.List all the words.<br>
* 6.Save all the words into a file.<br>
* 7.Load words from a file and add them to the database.<br>
*
* Date: May. 24, 2007
*
* @author Bai Feng
* @version 1.8
*/
public class Dict {
private WordDB words; // Word database
private static int seq = -1; // word index
/**
* constructor for the class Dict
*/
public Dict() {
words = new WordDB();// appoint an instance of WordDB
} // end of Dict()
/**
* add a new English word in the dictionary, <br>
* and there must be no repeated item.
*
* @param english English word
* @param chinese Chinese word
*/
public boolean addWord( String english, String chinese ) {
WordItem wi = new WordItem( english, chinese );
WordItem temp;
int i;
boolean hasFlag = false;
//temp = wi;
//System.out.println("words.indexOf(temp)"+words.indexOf(wi));
// finds if there is any same item
for ( i = 0; i < words.size(); i++ ) {
temp = ( WordItem )words.get( i );
if ( temp.equals( wi ) ) {
hasFlag = true;
break;
}
}
// there is no such a word existed.
if ( hasFlag == false ) {
words.add( wi );
//System.out.println("size="+words.size());
//System.out.println("index="+words.indexOf((WordItem)wi));
System.out.println( toString() );
return true;
}
else {
return false;
}
} // end of method addWord()
/**
* search a Chinese word in the dictionary
*
* @param english English word
* @return word found
*/
public String search( String english ) {
//return words.search( english );
boolean foundFlag = false;
int i = 0;
WordItem p;
if ( words.isEmpty() ) return "";
for ( i = 0; i < words.size(); i++ ) {
p = ( WordItem )words.get( i );
foundFlag = p.getEnglish().equals( english );
if ( foundFlag == true ) {
return p.getChinese();
}
}
return ""; // not found, return blank string
} // end of method searchChineseWord()
/**
* convert the class to a string using the format:"\n"
*
* @return string converted
*/
public String toString() {
return ( words.toString() );
}
/**
* recite a word.
*
* @return a word item
*/
public WordItem recite() {
if ( words.isEmpty() ) return null; // no words yet
// next word and point to the first word if finish
if ( seq + 1 < words.size() ) setSeq( seq + 1 );
else setSeq( 0 );
// add each word's counter
WordItem wi = ( WordItem ) words.get( seq );
wi.setCounter( wi.getCounter() + 1 );
return wi;
}
/**
* set seq value.
*
* @param seq new seq value
*/
private void setSeq( int seq ) {
this.seq = seq;
}
/**
* recite from the beginning.
*
*/
public void moveFirst() {
setSeq( -1 );
}
/**
* stat the word that been recited most.
* @return word item
*/
public WordItem stat() {
int max = 0;
int i;
int indexFlag = 0;
if ( words.isEmpty() ) return null;
for ( i = 0; i < words.size(); i++ ) {
// find out the max value of counter
int counterValue = (( WordItem )words.get( i )).getCounter();
if ( max < counterValue ) {
indexFlag = i;
max = counterValue;
}
}
return ( WordItem )words.get( indexFlag );
}
/**
* save the words into a file
*
* @param fileName name of save file
*/
public void save( String fileName ) {
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter( new FileWriter( fileName ));
// write dictionary database into file
outFile.write( toString(), 0, toString().length() );
outFile.close();
}
catch ( IOException e ) {
System.err.println("Dictionary file save error!");
}
}
/**
* load words from a file
*
* @param fileName name of file
*/
public void load( String fileName ) {
String readln = null;
String english, chinese;
int counter;
int firstLimiterLocation, secondLimitLocation;
try {
BufferedReader inFile = new BufferedReader( new FileReader( fileName ));
while ( inFile.ready() ) {
readln = inFile.readLine(); // read a line from the file
// identify the limiter
firstLimiterLocation = readln.indexOf(" ");
secondLimitLocation = readln.lastIndexOf(" ");
// analyse the english and chinese word, and their corresponding counter
english = readln.substring( 0, firstLimiterLocation ).trim();
chinese = readln.substring( firstLimiterLocation, secondLimitLocation ).trim();
counter = Integer.parseInt( readln.substring( secondLimitLocation, readln.length() ).trim() );
WordItem wi = new WordItem( english, chinese, counter ); words.loadWordDB( wi );
}
inFile.close();
}
catch ( IOException e ) {
System.err.println("Dictionary file load error.");
}
}
} // end of class Dict
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -