Advertisement
LukacikPavel

RomanCalculator

Nov 30th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. package sk.upjs.ics.agilnaRuka.romanCalculator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class GeneralRomanNumber {
  7.  
  8.     public static final String DEFAULT_ROMAN_DIGITS = "OIVXLCDM";
  9.     public static final int DIVIDING_BY_ZERO = -9999;
  10.     public static final int UNKNOWN_OPERATION = -9999;
  11.     public static final int INVALID_NUMBER = -9999;
  12.     private String RomanDigits;
  13.     private String RomanNumber;
  14.     private GeneralRomanNumberConverter converter;
  15.  
  16.     public GeneralRomanNumber(String RomanDigits) {
  17.         converter = new GeneralRomanNumberConverter();
  18.  
  19.         String RomanDigitsWithoutWhitespaces = removeSpaces(RomanDigits);
  20.  
  21.         if (RomanDigitsWithoutWhitespaces.length() < 2
  22.                 || converter.areRomanDigitsValid(RomanDigitsWithoutWhitespaces) == false) {
  23.             this.RomanDigits = DEFAULT_ROMAN_DIGITS;
  24.         } else {
  25.             this.RomanDigits = RomanDigits;
  26.         }
  27.     }
  28.  
  29.     public String getRomanDigits() {
  30.         return RomanDigits;
  31.     }
  32.  
  33.     public String getRomanDigitsWithoutZero() {
  34.         return RomanDigits.substring(1);
  35.     }
  36.  
  37.     public String getZeroFromRomanDigits() {
  38.         return String.valueOf(RomanDigits.charAt(0));
  39.     }
  40.  
  41.     public String getPositiveRomanNumber() {
  42.         if (RomanNumber.startsWith("-")) {
  43.             return RomanNumber.substring(1);
  44.         }
  45.         return RomanNumber;
  46.     }
  47.  
  48.     String removeSpaces(String string) {
  49.         StringBuilder stringBuilder = new StringBuilder();
  50.         char character;
  51.         for (int index = 0; index < string.length(); index++) {
  52.             character = string.charAt(index);
  53.             if (character != ' ') {
  54.                 stringBuilder.append(character);
  55.             }
  56.         }
  57.         return stringBuilder.toString();
  58.     }
  59.  
  60.     boolean isRomanNumber(String RomanDigits, String RomanNumber) {
  61.         for (int index = 0; index < RomanNumber.length(); index++) {
  62.             if (converter.isSymbolInRomanDigits(RomanNumber.charAt(index), RomanDigits) == false) {
  63.                 return false;
  64.             }
  65.         }
  66.         return true;
  67.     }
  68.  
  69.     boolean containsOnlyValidDigits(String RomanNumber) {
  70.         if (converter.isEachUppercaseEnglishLetter(RomanNumber) == false) {
  71.             return false;
  72.         }
  73.  
  74.         if (RomanNumber.length() > 1) {
  75.             return isRomanNumber(getRomanDigitsWithoutZero(), RomanNumber);
  76.         }
  77.         return isRomanNumber(getRomanDigits(), RomanNumber);
  78.     }
  79.  
  80.     int put(String RomanNumber) {
  81.         String RomanNumberWithoutSpaces = removeSpaces(RomanNumber);
  82.  
  83.         if (isRomanNumber(getRomanDigits(), RomanNumberWithoutSpaces.substring(1)) == false
  84.                 && isRomanNumber(getRomanDigits(), RomanNumberWithoutSpaces) == false) {
  85.             return -9999;
  86.         }
  87.  
  88.         this.RomanNumber = RomanNumberWithoutSpaces;
  89.  
  90.         return value();
  91.     }
  92.  
  93.     int value() {
  94.         if (RomanNumber == null || RomanNumber.equals(getZeroFromRomanDigits())
  95.                 || getPositiveRomanNumber().equals(getZeroFromRomanDigits())) {
  96.             return 0;
  97.         }
  98.  
  99.         if (RomanNumber.startsWith("-")) {
  100.             return -converter.RomanNumeralsConverter(getRomanDigitsWithoutZero(), getPositiveRomanNumber());
  101.         }
  102.  
  103.         return converter.RomanNumeralsConverter(getRomanDigitsWithoutZero(), RomanNumber);
  104.     }
  105.  
  106.     String romanValue() {
  107.         return RomanNumber;
  108.     }
  109.  
  110.     int normalCalculator(int number1, String operation, int number2) {
  111.         if (number1 == GeneralRomanNumber.INVALID_NUMBER || number2 == GeneralRomanNumber.INVALID_NUMBER) {
  112.             return GeneralRomanNumber.INVALID_NUMBER;
  113.         }
  114.  
  115.         int result = 0;
  116.  
  117.         if (operation.equals("+")) {
  118.             result = number1 + number2;
  119.         } else if (operation.equals("-")) {
  120.             result = number1 - number2;
  121.         } else if (operation.equals("*")) {
  122.             result = number1 * number2;
  123.         } else if (operation.equals("/")) {
  124.             if (number2 == 0) {
  125.                 return DIVIDING_BY_ZERO;
  126.             }
  127.             result = number1 / number2;
  128.         } else {
  129.             result = UNKNOWN_OPERATION;
  130.         }
  131.         return result;
  132.     }
  133.  
  134.     List<String> parseExpression(String expression) {
  135.         String expressionWithoutSpaces = removeSpaces(expression);
  136.         List<String> parsedExpression = new ArrayList<>();
  137.  
  138.         for (int index = 1; index < expressionWithoutSpaces.length(); index++) {
  139.             String character = String.valueOf(expressionWithoutSpaces.charAt(index));
  140.             if (character.equals("+") || character.equals("-") || character.equals("*") || character.equals("/")) {
  141.                 parsedExpression.add(expressionWithoutSpaces.substring(0, index));
  142.                 parsedExpression.add(character);
  143.                 parsedExpression.add(expressionWithoutSpaces.substring(index + 1, expressionWithoutSpaces.length()));
  144.                 break;
  145.             }
  146.         }
  147.         return parsedExpression;
  148.     }
  149.  
  150.     void saveResult(int result) {
  151.         if (result == 0) {
  152.             RomanNumber = getZeroFromRomanDigits();
  153.         } else if (result < 0) {
  154.             RomanNumber = "-" + converter.integerToRoman(result * (-1), getRomanDigitsWithoutZero());
  155.         } else {
  156.             RomanNumber = converter.integerToRoman(result, getRomanDigitsWithoutZero());
  157.         }
  158.     }
  159.  
  160.     int calculator(String input) {
  161.         List<String> parsedExpression = parseExpression(input);
  162.  
  163.         if (parsedExpression.isEmpty()) {
  164.             return -9999;
  165.         }
  166.  
  167.         int number1 = put(parsedExpression.get(0));
  168.         String operation = parsedExpression.get(1);
  169.         int number2 = put(parsedExpression.get(2));
  170.  
  171.         int result = normalCalculator(number1, operation, number2);
  172.         saveResult(result);
  173.         return result;
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement