Advertisement
iamaamir

IMEI Validator

Nov 8th, 2015
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. import javafx.scene.Scene;
  2. import javafx.stage.Stage;
  3. import javafx.geometry.Pos;
  4. import javafx.scene.layout.VBox;
  5. import java.util.stream.IntStream;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.Button;
  8. import javafx.application.Application;
  9. import javafx.scene.control.TextField;
  10.  
  11. /**
  12.  *
  13.  *
  14.  * @author Aamir khan
  15.  */
  16. public class IMEIValidaotrApp extends Application {
  17.  
  18.     //components
  19.     IMEIField field;
  20.     Label status;
  21.     VBox root;
  22.     Button checkBtn;
  23.  
  24.     //this is a bad idea use a separate css file indeed
  25.     private final String INVALID_LABEL_CSS = "-fx-text-fill:white; "
  26.             + "-fx-background-color: #E26868;"
  27.             + "-fx-padding:5px;"
  28.             + "-fx-border-width:1; -fx-border-color:#B63E5A;"
  29.             + "-fx-font-size: 9pt;";
  30.  
  31.     private final String VALID_LABEL_CSS = "-fx-text-fill:white; "
  32.             + "-fx-background-color: #20A286;"
  33.             + "-fx-padding:5px;"
  34.             + "-fx-border-width:1; -fx-border-color:#19B99A;"
  35.             + "-fx-font-size: 9pt;";
  36.  
  37.     private final String CHECK_BUTTON_CSS = "-fx-background-color:#c3c4c4,"
  38.             + "linear-gradient(#d6d6d6 50%, white 100%),"
  39.             + "radial-gradient(center 50% -40%, radius 200%, #e6e6e6 45%, rgba(230,230,230,0) 50%);"
  40.             + "-fx-background-radius: 30; -fx-background-insets: 0,1,1;"
  41.             + "-fx-text-fill: black;"
  42.             + "-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 3, 0.0 , 0 , 1 );";
  43.  
  44.     @Override
  45.     public void init() {
  46.  
  47.         field = new IMEIField();
  48.         field.setText("490154203237518");//for the Demo
  49.  
  50.         status = new Label();
  51.  
  52.         checkBtn = new Button("Check");
  53.         checkBtn.setDefaultButton(true);
  54.         //disable the button if the length is less than 15
  55.         checkBtn.disableProperty().bind(field.textProperty().length().lessThan(15));
  56.         checkBtn.setStyle(CHECK_BUTTON_CSS);
  57.     }
  58.  
  59.     @Override
  60.     public void start(Stage window) throws Exception {
  61.  
  62.         root = new VBox(status, field, checkBtn);
  63.         root.setAlignment(Pos.CENTER);
  64.         root.setSpacing(5.0);
  65.         root.setStyle("-fx-padding: 10; -fx-font-size: 18;");
  66.  
  67.         window.setScene(new Scene(root, 300, 200));
  68.         window.setTitle("IMEI Checker");
  69.         window.setResizable(false);
  70.         window.show();
  71.  
  72.         checkBtn.setOnAction(e -> validateIMEI());
  73.  
  74.     }
  75.  
  76.     /**
  77.      * implementation of
  78.      * <a href = "https://en.wikipedia.org/wiki/Luhn_algorithm Luhn algorithm">Luhn
  79.      * algorithm</a>
  80.      * is also known as “Modulus 10” algorithm.<br>
  81.      * It is a simple checksum formula used to validate a variety of
  82.      * identification numbers,<br>
  83.      * such as credit card numbers, IMEI numbers, National Provider Identifier
  84.      * numbers in US and Canadian Social Insurance Numbers.<br>
  85.      * It was created by IBM scientist Hans Peter Luhn.<br>
  86.      * Verification is done by validating check digit.<br><br>
  87.      *
  88.      * <p>
  89.      * simply Double the value of every second digit from the right end (first
  90.      * right will be check digit number). Add the individual digits comprising
  91.      * both the products from step (1) and unaffected digits in the original
  92.      * number. If the total modulo 10 is equal to 0, then the number is valid,
  93.      * else it is not valid. an example of IMEI no. 490154203237518</p>
  94.      */
  95.     private void validateIMEI() {
  96.  
  97.         final int[] numbers = field.getText().chars().map(this::ConvertASCIIToNumer).toArray();
  98.         boolean isValid = IntStream.range(0, numbers.length)
  99.                 .map(i -> (((i % 2) ^ (numbers.length % 2)) == 0)
  100.                         ? ((2 * numbers[i]) / 10 + (2 * numbers[i]) % 10)
  101.                         : numbers[i])
  102.                 .sum() % 10 == 0;
  103.  
  104.         if (isValid) {
  105.             status.setStyle(VALID_LABEL_CSS);
  106.             status.setText("VALID");
  107.         } else {
  108.             status.setText("INVALID");
  109.             status.setStyle(INVALID_LABEL_CSS);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Returns the numeric value of the ASCII
  115.      */
  116.     private int ConvertASCIIToNumer(int value) {
  117.         return Character.digit(value, 10);
  118.     }
  119.  
  120.     /**
  121.      * a specific class for Holding
  122.      * <a href =
  123.      * "https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity">
  124.      * IMEI Numbers
  125.      * </a>
  126.      */
  127.     class IMEIField extends TextField {
  128.  
  129.         private final int LIMIT = 15;//A Valid IMEI Number has 15 digits
  130.  
  131.         @Override
  132.         public void replaceText(int start, int end, String text) {
  133.             if (validate(text)) {
  134.                 super.replaceText(start, end, text);
  135.                 onLengthListener();
  136.             }
  137.         }
  138.  
  139.         @Override
  140.         public void replaceSelection(String text) {
  141.             if (validate(text)) {
  142.                 super.replaceSelection(text);
  143.                 onLengthListener();
  144.             }
  145.         }
  146.  
  147.         private void onLengthListener() {
  148.             //Stop if user type more than LIMIT
  149.             textProperty().
  150.                     addListener((observable, oldValue, newValue) -> {
  151.                         if (newValue.length() > LIMIT) {
  152.                             setText(oldValue);
  153.                         }
  154.                     });
  155.  
  156.         }
  157.  
  158.         /**
  159.          * Validate input against the {@link String} and make sure input is
  160.          * Number only
  161.          *
  162.          * @return true if the value is a Numeric Value <b>0-9</b>
  163.          */
  164.         private boolean validate(String text) {
  165.  
  166.             //furtuhre check can be done for "Empty input" or for "White Space"
  167.             //return (" ".equals(text) || "".equals(text) ||text.matches("[0-9]"));
  168.             return ("".equals(text) || text.matches("[0-9]"));
  169.         }
  170.  
  171.     }
  172.  
  173.     //Mr. Main
  174.     public static void main(String[] args) {
  175.         launch(args);//Entry point for the FX Thread
  176.  
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement