Advertisement
Vanya_Shestakov

Untitled

Nov 27th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. package com.company;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         System.out.println("The program converts the number to the Roman numeral system");
  12.         int source = chooseSourceOfInput();
  13.         String sourceNumber = inputLine(source);
  14.         String romanNumber = convertToRoman(sourceNumber);
  15.         outputResult(source, romanNumber, sourceNumber);
  16.     }
  17.  
  18.     public static int chooseSourceOfInput() {
  19.         System.out.println("Choose where to enter data. Enter 1 or 2:\n 1.File\n 2.Console");
  20.         Scanner scan = new Scanner(System.in);
  21.         int choice = 0;
  22.         boolean isIncorrect;
  23.         do {
  24.             isIncorrect = false;
  25.             try {
  26.                 choice = Integer.parseInt(scan.nextLine());
  27.             } catch (Exception e) {
  28.                 System.out.println("Enter an integer!");
  29.                 isIncorrect = true;
  30.             }
  31.             if (!isIncorrect && choice != 1 && choice != 2) {
  32.                 System.out.println("Enter 1 or 2!");
  33.                 isIncorrect = true;
  34.             }
  35.         } while (isIncorrect);
  36.         return choice;
  37.     }
  38.  
  39.     public static String inputLine(int source) throws FileNotFoundException {
  40.         String line = "";
  41.         switch (source) {
  42.             case 1:
  43.                 System.out.println("Enter the absolute link to the input file");
  44.                 String pathInput = inputPath();
  45.                 line = inputLineFromFile(pathInput);
  46.                 break;
  47.             case 2:
  48.                 line = inputLineFromConsole();
  49.                 break;
  50.         }
  51.         return line;
  52.     }
  53.  
  54.     public static String inputPath() {
  55.         Scanner scan = new Scanner(System.in);
  56.         boolean isIncorrect;
  57.         String path;
  58.         do {
  59.             isIncorrect = false;
  60.             path = scan.nextLine();
  61.             File file = new File(path);
  62.  
  63.             if (!file.exists()) {
  64.                 System.out.println("File not found!\nEnter the absolute link to the file");
  65.                 isIncorrect = true;
  66.             }
  67.         } while (isIncorrect);
  68.         System.out.println();
  69.         return path;
  70.     }
  71.  
  72.     public static String inputLineFromFile(String path) throws FileNotFoundException {
  73.         File inputFile = new File(path);
  74.         Scanner scan = new Scanner(inputFile);
  75.         String line;
  76.  
  77.         if (inputFile.length() != 0) {
  78.             line = scan.nextLine();
  79.         } else {
  80.             System.out.println("The line is missing from your file!\nEnter line from console ");
  81.             line = inputLineFromConsole();
  82.         }
  83.         boolean isIncorrect = checkLine(line);
  84.         if (isIncorrect) {
  85.             line = inputLineFromConsole();
  86.         }
  87.         return line;
  88.     }
  89.  
  90.     public static boolean checkLine(String line) {
  91.         boolean flag = false;
  92.         int number = 0;
  93.  
  94.         if (line.length() == 0) {
  95.             System.out.println("You entered an empty line!\nRepeat enter");
  96.             flag = true;
  97.         }
  98.         if (!flag) {
  99.             try {
  100.                 number = Integer.parseInt(line);
  101.             } catch (NumberFormatException e) {
  102.                 System.out.println("This line cannot be represented as an integer!\nEnter line from console ");
  103.                 flag = true;
  104.             }
  105.         }
  106.         if ((number < 1 || number > 2000) && !flag) {
  107.             System.out.println("The number must be in the range from 1 to 2000\nEnter line from console ");
  108.             flag = true;
  109.         }
  110.         return flag;
  111.     }
  112.  
  113.     public static String inputLineFromConsole() {
  114.         Scanner scan = new Scanner(System.in);
  115.         boolean isIncorrect;
  116.         String line;
  117.         System.out.println("Enter the line");
  118.         do {
  119.             isIncorrect = false;
  120.             line = scan.nextLine();
  121.             isIncorrect = checkLine(line);
  122.         } while (isIncorrect);
  123.         return line;
  124.     }
  125.  
  126.     public static String convertToRoman(String sourceNumber) {
  127.         final int UNIT = 0;
  128.         final int FIVE = 1;
  129.         final int TEN = 2;
  130.         final int FIFTY = 3;
  131.         final int ONE_HUNDRED = 4;
  132.         final int FIVE_HUNDRED = 5;
  133.         final int ONE_THOUSAND = 6;
  134.         int j = 0;
  135.         int[] digits = new int[4];
  136.         for (int i = 4 - sourceNumber.length(); i < 4; i++) {
  137.             digits[i] = Integer.parseInt(String.valueOf(sourceNumber.charAt(j)));
  138.             j++;
  139.         }
  140.         StringBuilder romanNumber = new StringBuilder();
  141.         romanNumber.append(addDigit(digits[0]));
  142.         romanNumber.append(addDigit(digits[1], ONE_HUNDRED, FIVE_HUNDRED, ONE_THOUSAND ));
  143.         romanNumber.append(addDigit(digits[2], TEN, FIFTY, ONE_HUNDRED ));
  144.         romanNumber.append(addDigit(digits[3], UNIT, FIVE, TEN ));
  145.         return romanNumber.toString();
  146.     }
  147.  
  148.     public static String addDigit(int digit, int firstRomanSymbol, int secondRomanSymbol, int thirdRomanSymbol) {
  149.         final String[] romanSymbols = {"I", "V", "X", "L", "C", "D", "M"};
  150.         String romanDigit = "";
  151.         if (digit == 1 || digit == 2 || digit == 3) {
  152.             for (int i = 0; i < digit; i++) {
  153.                 romanDigit += romanSymbols[firstRomanSymbol];
  154.             }
  155.         } else if (digit == 4) {
  156.             romanDigit += romanSymbols[firstRomanSymbol] + romanSymbols[secondRomanSymbol];
  157.         } else if (digit == 6 || digit == 7 || digit == 8) {
  158.             romanDigit += romanSymbols[secondRomanSymbol];
  159.             for (int i = 0; i < digit - 5; i++) {
  160.                 romanDigit += romanSymbols[firstRomanSymbol];
  161.             }
  162.         } else if (digit == 9) {
  163.             romanDigit += romanSymbols[firstRomanSymbol] + romanSymbols[thirdRomanSymbol];
  164.         }
  165.         return romanDigit;
  166.     }
  167.  
  168.     public static String addDigit(int digit) {
  169.         String romanDigit = "";
  170.         for (int i = 0; i < digit; i++) {
  171.             romanDigit += "M";
  172.         }
  173.         return romanDigit;
  174.     }
  175.  
  176.     public static void outputResult(int source, String romanNumber, String sourceNumber) throws IOException {
  177.         if (source == 1) {
  178.             System.out.println("\nEnter the absolute link to the output file");
  179.             String pathOutput = inputPath();
  180.             outputToFile(pathOutput, romanNumber, sourceNumber);
  181.         } else {
  182.             outputToConsole(romanNumber, sourceNumber);
  183.         }
  184.     }
  185.  
  186.     public static void outputToFile(String path, String romanNumber, String sourceNumber) throws IOException {
  187.         FileWriter writer = new FileWriter(path);
  188.         writer.write(sourceNumber + " = " + romanNumber);
  189.         writer.close();
  190.         System.out.println("The data is successfully recorded in the file");
  191.     }
  192.  
  193.     public static void outputToConsole(String romanNumber, String sourceNumber) {
  194.         System.out.println(sourceNumber + " = " + romanNumber);
  195.     }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement