Advertisement
KeepCoding

SmartPhone class

Jun 30th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. package p04_telephony;
  2.  
  3. public class SmartPhone implements Callable, Browsable {
  4.     private static final String INVALID_PHONE_NUMBER_MESSAGE = "Invalid number!";
  5.     private static final String INVALID_URL_MESSAGE = "Invalid URL!";
  6.  
  7.     private String number;
  8.  
  9.     public SmartPhone(String number) {
  10.         this.number = number;
  11.     }
  12.  
  13.     public String getNumber() {
  14.         return this.number;
  15.     }
  16.  
  17.     @Override
  18.     public void browseWebsite(String website) { // TODO see why the commented part gives max points, and the uncommented part doesn't
  19. //        for (int i = 0; i < website.length(); i++) {
  20. //            if (Character.isDigit(website.charAt(i))) {
  21. //                System.out.println(INVALID_URL_MESSAGE);
  22. //                return;
  23. //            }
  24. //        }
  25. //        System.out.printf("Browsing: %s!%n", website);
  26.  
  27.         if (website.matches("[^0-9]+")) {
  28.             System.out.printf("Browsing: %s!%n", website);
  29.         } else {
  30.             System.out.println(INVALID_URL_MESSAGE);
  31.         }
  32.     }
  33.  
  34.     @Override
  35.     public void call(SmartPhone phone) {
  36. //        for (int i = 0; i < phone.getNumber().length(); i++) {
  37. //            if (!Character.isDigit(phone.getNumber().charAt(i))) {
  38. //                System.out.println(INVALID_PHONE_NUMBER_MESSAGE);
  39. //                return;
  40. //            }
  41. //        }
  42. //
  43. //        System.out.printf("Calling... %s%n", phone.getNumber());
  44.  
  45.         if (phone.getNumber().matches("[0-9]+")) {
  46.             System.out.printf("Calling... %s%n", phone.getNumber());
  47.         } else {
  48.             System.out.println(INVALID_PHONE_NUMBER_MESSAGE);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement