SpecialCharDocument.java 902B

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