Advertisement
Vanya_Shestakov

laba3.1 (Java)

Oct 25th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 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.         String[] unitsOfRoman = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
  128.         String[] tensOfRoman = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
  129.         String[] hundredsOfRoman = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
  130.         String[] thousandsOfRoman = {"","M","MM"};
  131.  
  132.         int j = 0;
  133.         int[] digits = new int[4];
  134.         for (int i = 4 - sourceNumber.length(); i < 4; i++) {
  135.             digits[i] = Integer.parseInt(String.valueOf(sourceNumber.charAt(j)));
  136.             j++;
  137.         }
  138.         String romanNumber = thousandsOfRoman[digits[0]] + hundredsOfRoman[digits[1]] + tensOfRoman[digits[2]] + unitsOfRoman[digits[3]];
  139.         return romanNumber;
  140.     }
  141.  
  142.     public static void outputResult(int source, String romanNumber, String sourceNumber ) throws IOException {
  143.         if (source == 1) {
  144.             System.out.println("\nEnter the absolute link to the output file");
  145.             String pathOutput = inputPath();
  146.             outputToFile(pathOutput, romanNumber, sourceNumber);
  147.         }else {
  148.             outputToConsole(romanNumber, sourceNumber);
  149.         }
  150.     }
  151.  
  152.     public static void outputToFile(String path, String romanNumber, String sourceNumber) throws IOException {
  153.         FileWriter writer = new FileWriter(path);
  154.         writer.write(sourceNumber + " = " +  romanNumber);
  155.         writer.close();
  156.         System.out.println("The data is successfully recorded in the file");
  157.     }
  158.  
  159.     public static void outputToConsole(String romanNumber, String sourceNumber){
  160.         System.out.println(sourceNumber + " = " +  romanNumber);
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement