SpecialCharDocument.java 755B

12345678910111213141516171819202122232425262728293031
  1. package rocks.zipcode;
  2. import java.io.IOException;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. /**
  6. * @author leon on 18/11/2018.
  7. */
  8. public class SpecialCharDocument extends Document {
  9. public SpecialCharDocument(String fileName) throws IOException {
  10. super(fileName);
  11. }
  12. @Override
  13. public void write(String contentToBeWritten) {
  14. if(isSpecialCharacters(contentToBeWritten)) {
  15. super.write(contentToBeWritten);
  16. } else {
  17. throw new IllegalArgumentException();
  18. }
  19. }
  20. private Boolean isSpecialCharacters(String s) {
  21. Pattern pattern = Pattern.compile("^[!@#$%^&*()_ ]+$");
  22. Matcher match = pattern.matcher(s);
  23. return match.find();
  24. }
  25. }