-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.java
More file actions
104 lines (87 loc) · 2.17 KB
/
Language.java
File metadata and controls
104 lines (87 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.ArrayList;
import java.util.Collections;
public class Language {
private String lang;
private ArrayList<WordWrapper> words;
private int size;
public Language(String lang, ArrayList<String[]> words) {
ArrayList<WordWrapper> wrappers = new ArrayList<WordWrapper>();
this.lang = lang;
for (String[] wordPair : words) {
wrappers.add(new WordWrapper(wordPair, 0));
}
this.words = wrappers;
size = words.size();
}
// Accessor Methods
public String getLang() {
return lang;
}
public ArrayList<String[]> getWords() {
ArrayList<String[]> listOfWords = new ArrayList<String[]>();
for (WordWrapper wrapper : words) {
listOfWords.add(wrapper.wordPair);
}
return listOfWords;
}
public int size() {
return size;
}
// Difficulty Setter/Getter Methods
// Difficulty Getter Method
public int getDifficulty(String word) {
for (WordWrapper wrapper : words) {
if (wrapper.wordPair[0].equals(word)) {
return wrapper.difficulty;
}
}
// -1 means the word was not found
return -1;
}
// Dificulty Setter Method
public void setDifficulty(String word, int newDif) {
for (WordWrapper wrapper : words) {
if (wrapper.wordPair[0].equals(word)) {
wrapper.difficulty = newDif;
}
}
}
// Sort the Words based on Difficulty
public void sortWords() {
Collections.sort(words);
}
public void printWords() {
for (WordWrapper wrapper : words) {
System.out.println(wrapper.wordPair[0] + ": " + wrapper.wordPair[1]);
}
}
// Mutator Methods
// Adds a word and its translation to the list of words
public void add(String[] word) {
words.add(new WordWrapper(word, 0));
size++;
}
public String[] remove(String[] word) {
words.remove(word);
size--;
return word;
}
// Word Wrapper Class
private static class WordWrapper implements Comparable<WordWrapper> {
String[] wordPair;
int difficulty;
WordWrapper(String[] wordPair, int difficulty) {
this.wordPair = wordPair;
this.difficulty = difficulty;
}
public int compareTo(WordWrapper other) {
if (this.difficulty < other.difficulty) {
return -1;
} else if (this.difficulty > other.difficulty) {
return 1;
} else {
return 0;
}
}
}
}