AlphaCharDocument.java 776B

12345678910111213141516171819202122232425262728293031323334353637383940
  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) {
  14. if (this.isAlpha(contentToBeWritten)==true){
  15. super.write(contentToBeWritten);
  16. }else{
  17. throw new IllegalArgumentException();
  18. }
  19. }
  20. private Boolean isAlpha(String s) {
  21. Pattern pattern = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
  22. Matcher matcher = pattern.matcher(s);
  23. boolean b = matcher.find();
  24. return b;
  25. }
  26. }