Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IOException {
- System.out.println("The program converts the number to the Roman numeral system");
- int source = chooseSourceOfInput();
- String sourceNumber = inputLine(source);
- String romanNumber = convertToRoman(sourceNumber);
- outputResult(source, romanNumber, sourceNumber);
- }
- public static int chooseSourceOfInput() {
- System.out.println("Choose where to enter data. Enter 1 or 2:\n 1.File\n 2.Console");
- Scanner scan = new Scanner(System.in);
- int choice = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- choice = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Enter an integer!");
- isIncorrect = true;
- }
- if (!isIncorrect && choice != 1 && choice != 2) {
- System.out.println("Enter 1 or 2!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- public static String inputLine(int source) throws FileNotFoundException {
- String line = "";
- switch (source) {
- case 1:
- System.out.println("Enter the absolute link to the input file");
- String pathInput = inputPath();
- line = inputLineFromFile(pathInput);
- break;
- case 2:
- line = inputLineFromConsole();
- break;
- }
- return line;
- }
- public static String inputPath() {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String path;
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("File not found!\nEnter the absolute link to the file");
- isIncorrect = true;
- }
- } while (isIncorrect);
- System.out.println();
- return path;
- }
- public static String inputLineFromFile(String path) throws FileNotFoundException {
- File inputFile = new File(path);
- Scanner scan = new Scanner(inputFile);
- String line;
- if (inputFile.length() != 0) {
- line = scan.nextLine();
- } else {
- System.out.println("The line is missing from your file!\nEnter line from console ");
- line = inputLineFromConsole();
- }
- boolean isIncorrect = checkLine(line);
- if (isIncorrect) {
- line = inputLineFromConsole();
- }
- return line;
- }
- public static boolean checkLine(String line) {
- boolean flag = false;
- int number = 0;
- if (line.length() == 0) {
- System.out.println("You entered an empty line!\nRepeat enter");
- flag = true;
- }
- if (!flag) {
- try {
- number = Integer.parseInt(line);
- } catch (NumberFormatException e) {
- System.out.println("This line cannot be represented as an integer!\nEnter line from console ");
- flag = true;
- }
- }
- if ((number < 1 || number > 2000) && !flag) {
- System.out.println("The number must be in the range from 1 to 2000\nEnter line from console ");
- flag = true;
- }
- return flag;
- }
- public static String inputLineFromConsole() {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String line;
- System.out.println("Enter the line");
- do {
- isIncorrect = false;
- line = scan.nextLine();
- isIncorrect = checkLine(line);
- } while (isIncorrect);
- return line;
- }
- public static String convertToRoman(String sourceNumber) {
- final int UNIT = 0;
- final int FIVE = 1;
- final int TEN = 2;
- final int FIFTY = 3;
- final int ONE_HUNDRED = 4;
- final int FIVE_HUNDRED = 5;
- final int ONE_THOUSAND = 6;
- int j = 0;
- int[] digits = new int[4];
- for (int i = 4 - sourceNumber.length(); i < 4; i++) {
- digits[i] = Integer.parseInt(String.valueOf(sourceNumber.charAt(j)));
- j++;
- }
- StringBuilder romanNumber = new StringBuilder();
- romanNumber.append(addDigit(digits[0]));
- romanNumber.append(addDigit(digits[1], ONE_HUNDRED, FIVE_HUNDRED, ONE_THOUSAND ));
- romanNumber.append(addDigit(digits[2], TEN, FIFTY, ONE_HUNDRED ));
- romanNumber.append(addDigit(digits[3], UNIT, FIVE, TEN ));
- return romanNumber.toString();
- }
- public static String addDigit(int digit, int firstRomanSymbol, int secondRomanSymbol, int thirdRomanSymbol) {
- final String[] romanSymbols = {"I", "V", "X", "L", "C", "D", "M"};
- String romanDigit = "";
- if (digit == 1 || digit == 2 || digit == 3) {
- for (int i = 0; i < digit; i++) {
- romanDigit += romanSymbols[firstRomanSymbol];
- }
- } else if (digit == 4) {
- romanDigit += romanSymbols[firstRomanSymbol] + romanSymbols[secondRomanSymbol];
- } else if (digit == 6 || digit == 7 || digit == 8) {
- romanDigit += romanSymbols[secondRomanSymbol];
- for (int i = 0; i < digit - 5; i++) {
- romanDigit += romanSymbols[firstRomanSymbol];
- }
- } else if (digit == 9) {
- romanDigit += romanSymbols[firstRomanSymbol] + romanSymbols[thirdRomanSymbol];
- }
- return romanDigit;
- }
- public static String addDigit(int digit) {
- String romanDigit = "";
- for (int i = 0; i < digit; i++) {
- romanDigit += "M";
- }
- return romanDigit;
- }
- public static void outputResult(int source, String romanNumber, String sourceNumber) throws IOException {
- if (source == 1) {
- System.out.println("\nEnter the absolute link to the output file");
- String pathOutput = inputPath();
- outputToFile(pathOutput, romanNumber, sourceNumber);
- } else {
- outputToConsole(romanNumber, sourceNumber);
- }
- }
- public static void outputToFile(String path, String romanNumber, String sourceNumber) throws IOException {
- FileWriter writer = new FileWriter(path);
- writer.write(sourceNumber + " = " + romanNumber);
- writer.close();
- System.out.println("The data is successfully recorded in the file");
- }
- public static void outputToConsole(String romanNumber, String sourceNumber) {
- System.out.println(sourceNumber + " = " + romanNumber);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement