AlphaCharDocument.java 943B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package rocks.zipcode;
  2. import java.io.*;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. /**
  6. * @author leon on 16/11/2018.
  7. */
  8. public class AlphaCharDocument extends Document {
  9. public AlphaCharDocument(String fileName) throws IOException {
  10. super(fileName);
  11. }
  12. @Override
  13. public void write(String contentToBeWritten) throws IOException {
  14. if (this.isAlpha(contentToBeWritten)){
  15. super.write(contentToBeWritten);
  16. }else{
  17. throw new IllegalArgumentException();
  18. }
  19. }
  20. private Boolean isAlpha(String s) {
  21. // boolean allLetters = s.chars().allMatch(Character::isLetter);
  22. // return allLetters;
  23. // return (s.matches("[a-zA-Z]+"));
  24. Pattern pattern = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
  25. Matcher matcher = pattern.matcher(s);
  26. boolean b = matcher.find();
  27. return b;
  28. }
  29. }