add files
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import java.util.*;
|
||||
class WordBank {
|
||||
private Set<String> wordBank = new HashSet<>();
|
||||
private Random r = new Random();
|
||||
static WordBank obj = new WordBank();
|
||||
|
||||
public int setSize() {
|
||||
return (wordBank.size());
|
||||
|
||||
}
|
||||
|
||||
private WordBank() {
|
||||
}
|
||||
|
||||
public static WordBank getObject() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void addWord(String word) {
|
||||
|
||||
for (String i : wordBank) {
|
||||
if (word.equals(i)) {
|
||||
System.out.println("This word already exists! ");
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
if (word == null || word.equals("")) {
|
||||
System.out.println("Word is not valid!");
|
||||
return;
|
||||
}
|
||||
wordBank.add(word);
|
||||
|
||||
}
|
||||
|
||||
public String getRandomWord() {
|
||||
if (wordBank.size() == 0) {
|
||||
System.out.println("there is no word in Word Bank \n");
|
||||
return ("there is no word in Word Bank ");
|
||||
}
|
||||
Object[] wordBankArray = wordBank.toArray();
|
||||
int a = r.nextInt(wordBankArray.length);
|
||||
|
||||
return ((String) wordBankArray[a]);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user