oop-labs-collection/labs/5/Word.java

93 lines
2.1 KiB
Java

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Word {
private Symbol[] symbols;
private Symbol ending;
public Word(String rawWord) {
Symbol p;
Symbol[] w;
Matcher punctuationMatcher = Pattern.compile("[,;:]?").matcher(rawWord);
String foundPunctuation;
if (punctuationMatcher.find()) {
foundPunctuation = punctuationMatcher.group();
} else {
foundPunctuation = "";
}
if ("".equals(foundPunctuation)) {
p = new Symbol(' ');
} else {
p = new Symbol(foundPunctuation.charAt(0));
}
Matcher wordMatcher = Pattern.compile("[A-Za-z]*").matcher(rawWord);
String foundWord;
if (wordMatcher.find()) {
foundWord = wordMatcher.group();
} else {
foundWord = "";
}
Symbol[] newWordArray = new Symbol[foundWord.length()];
for (int i = 0; i < foundWord.length(); i++) {
newWordArray[i] = new Symbol(foundWord.charAt(i));
}
this.symbols = newWordArray;
this.ending = p;
}
/*
public Word(Symbol[] word, Symbol ending) {
this.word = word;
this.ending = ending;
}
public Word(Symbol[] word) {
this.word = word;
this.ending = new Symbol();
}
public Word() {
this.word = new Word;
this.ending = new Symbol();
}
*/
//@Override
public boolean equals(Word o) {
if (o.symbols.length != this.symbols.length) {
return false;
}
for (int i = 0; i < this.symbols.length; i++) {
if (this.symbols[i] != o.symbols[i]) {
return false;
}
}
return true;
}
@Override
public String toString() {
String result = "";
for (Symbol i : this.symbols) {
result += i.toString();
}
if (!this.ending.equals(new Symbol(' '))) {
result += this.ending.toString();
}
return result;
}
}