Advertisement
Guest User

Validator

a guest
Jan 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package com.corexis.calculator;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class DataValidator {
  7.  
  8.     private int a, b;
  9.     private String op;
  10.     private boolean roman = false;
  11.  
  12.     Expression createExpression(String checkingString) {
  13.         if (checkForNumbers(checkingString)) {
  14.             return new Expression(a, b, op, roman);
  15.         } else {
  16.             return null;
  17.         }
  18.  
  19.     }
  20.  
  21.     private String[] splitString(String str) {
  22.         return str.split(" ");
  23.     }
  24.  
  25.     private boolean checkForNumbers(String str) {
  26.         boolean mAIsRoman = false;
  27.         boolean mBIsRoman = false;
  28.         String[] tempArr = splitString(str);
  29.         try {
  30.             this.a = Integer.parseInt(tempArr[0]);
  31.         } catch (NumberFormatException e) {
  32.             if (checkForRoman(tempArr[0]) != 0) {
  33.                 this.a = checkForRoman(tempArr[0]);
  34.                 mAIsRoman = true;
  35.             } else {
  36.                 throw new NumberFormatException();
  37.             }
  38.         }
  39.         try {
  40.             this.b = Integer.parseInt(tempArr[2]);
  41.         } catch (NumberFormatException e) {
  42.             if (checkForRoman(tempArr[2]) != 0) {
  43.                 this.b = checkForRoman(tempArr[2]);
  44.                 mBIsRoman = true;
  45.             } else {
  46.                 throw new NumberFormatException();
  47.             }
  48.         }
  49.         if(mAIsRoman == mBIsRoman) {
  50.             return true;
  51.         } else {
  52.             return false;
  53.         }
  54.     }
  55.  
  56.     private int checkForRoman(String checkingString) {
  57.         if (Expression.romanNumbers.containsKey(checkingString)) {
  58.             roman = true;
  59.             return Expression.romanNumbers.get(checkingString);
  60.         }
  61.         return 0;
  62.     }
  63.  
  64.     private boolean checkForOperation(String op) {
  65.         return false;
  66.     }
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement