Advertisement
Egor_Vakar

lab3_2

Nov 8th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.29 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Scanner;
  9. import java.util.Set;
  10. import java.util.regex.PatternSyntaxException;
  11.  
  12. public class Main {
  13.     static final HashSet<Character> setOfDigitsAndOperations = new HashSet<>(Set.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '^', '√'));
  14.  
  15.  
  16.     static Scanner scan = new Scanner(System.in);
  17.  
  18.  
  19.     public static void main(String[] args) {
  20.         System.out.print("Welcome to the program that will sort an array using cocktail sort\nSelect the source for entering the sequence of numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
  21.         byte inputSource = takeSource();
  22.         Set<Character> initialSet = takeSet(inputSource);
  23.         Set<Character> answer = takeAnswer(initialSet);
  24.         System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
  25.         byte outputSource = takeSource();
  26.         output(outputSource, answer);
  27.         scan.close();
  28.     }
  29.  
  30.     static int takeSize() {
  31.         final int MIN_SIZE = 2;
  32.         final int MAX_SIZE = 30;
  33.         int number = 0;
  34.         boolean isIncorrect;
  35.         do {
  36.             isIncorrect = false;
  37.             try {
  38.                 number = Integer.parseInt(scan.nextLine());
  39.             } catch (NumberFormatException e) {
  40.                 System.out.println("Incorrect input!!!");
  41.                 isIncorrect = true;
  42.             }
  43.             if (!isIncorrect && (number < MIN_SIZE || number > MAX_SIZE)) {
  44.                 System.out.println("Incorrect input!!!");
  45.                 isIncorrect = true;
  46.             }
  47.         } while (isIncorrect);
  48.         return number;
  49.     }
  50.  
  51.     static char takeChar() {
  52.         char inputChar = 0;
  53.         boolean isIncorrect;
  54.         String line;
  55.         do {
  56.             line = scan.nextLine();
  57.             isIncorrect = false;
  58.             if ((line.length() != 1)){
  59.                 System.out.println("Incorrect input!!! Must be char:");
  60.                 isIncorrect = true;
  61.             } else {
  62.                 try {
  63.                     inputChar = (line).charAt(0);
  64.                 } catch (NumberFormatException e) {
  65.                     System.out.println("Incorrect input!!!");
  66.                     isIncorrect = true;
  67.                 }
  68.             }
  69.         } while (isIncorrect);
  70.         return inputChar;
  71.     }
  72.  
  73.     static byte takeSource() {
  74.         final byte CONSOLE = 1;
  75.         final byte FILE = 2;
  76.         boolean isIncorrect;
  77.         byte choice = 0;
  78.         do {
  79.             isIncorrect = false;
  80.             try {
  81.                 choice = Byte.parseByte(scan.nextLine());
  82.             } catch (Exception e) {
  83.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  84.                 isIncorrect = true;
  85.             }
  86.             if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
  87.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  88.                 isIncorrect = true;
  89.             }
  90.         } while (isIncorrect);
  91.         return choice;
  92.     }
  93.  
  94.     static String takeInPath() {
  95.         String path;
  96.         boolean isIncorrect;
  97.         System.out.print("Enter file path: ");
  98.         do {
  99.             isIncorrect = false;
  100.             path = scan.nextLine();
  101.             if (Files.notExists(Path.of(path))) {
  102.                 System.out.print("File is not found\nEnter file path: ");
  103.                 isIncorrect = true;
  104.             }
  105.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  106.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  107.                 isIncorrect = true;
  108.             }
  109.         } while (isIncorrect);
  110.         return path;
  111.     }
  112.  
  113.     static String takeOutPath() {
  114.         String path;
  115.         boolean isIncorrect;
  116.         System.out.print("Enter file path: ");
  117.         do {
  118.             isIncorrect = false;
  119.             path = scan.nextLine();
  120.             if (!path.endsWith(".txt")) {
  121.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  122.                 isIncorrect = true;
  123.             }
  124.         } while (isIncorrect);
  125.         return path;
  126.     }
  127.  
  128.     static Set<Character> takeSetFromConsole() {
  129.         System.out.print("Enter size: ");
  130.         int size = takeSize();
  131.         Set<Character> initialSet = new HashSet<>();
  132.         for (int i = 0; i < size; i++) {
  133.             System.out.print("Enter element " + (i + 1) + ": ");
  134.             initialSet.add(takeChar());
  135.         }
  136.         return initialSet;
  137.     }
  138.  
  139.     static Set<Character> takeSetFromFile(final String path) {
  140.         final int SET_LINE_NUMBER = 1;
  141.         int lineCounter = 0;
  142.         String line;
  143.         boolean isCorrect = true;
  144.         String[] stringArray = null;
  145.         try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
  146.             while ((isCorrect) && ((line = reader.readLine()) != null)) {
  147.                 lineCounter++;
  148.                 if (lineCounter == SET_LINE_NUMBER) {
  149.                     try {
  150.                         stringArray = line.split(" ");
  151.                     } catch (PatternSyntaxException e) {
  152.                         System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
  153.                         isCorrect = false;
  154.                     }
  155.                 }
  156.             }
  157.         } catch (IOException e) {
  158.             System.out.println("Input/Output error!!!");
  159.             isCorrect = false;
  160.         }
  161.         Set<Character> initialSet = new HashSet<>();
  162.         if (stringArray != null) {
  163.             if ((isCorrect) && (lineCounter > SET_LINE_NUMBER)) {
  164.                 System.out.println("Incorrect file content!!!");
  165.                 isCorrect = false;
  166.             } else {
  167.                 for (int i = 0; i < stringArray.length; i++) {
  168.                     try {
  169.                         initialSet.add((stringArray[i]).charAt(0));
  170.                     } catch (Exception e) {
  171.                         System.out.println("Incorrect file content!!!");
  172.                         isCorrect = false;
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.         if (isCorrect) {
  178.             return initialSet;
  179.         } else {
  180.             return null;
  181.         }
  182.     }
  183.  
  184.     static Set<Character> takeSet(final byte source) {
  185.         String inPath;
  186.         Set<Character> initialSet;
  187.         if (source == 1) {
  188.             initialSet = takeSetFromConsole();
  189.         } else {
  190.             inPath = takeInPath();
  191.             initialSet = takeSetFromFile(inPath);
  192.             while (initialSet == null) {
  193.                 inPath = takeInPath();
  194.                 initialSet = takeSetFromFile(inPath);
  195.             }
  196.         }
  197.         return initialSet;
  198.     }
  199.  
  200.     static Set<Character> takeAnswer(Set<Character> initialSet) {
  201.         initialSet.retainAll(setOfDigitsAndOperations);
  202.         return initialSet;
  203.     }
  204.  
  205.     static void outputToConsole(Set<Character> answer) {
  206.         System.out.print("Answer is:\n(");
  207.         Iterator<Character> iterator = answer.iterator();
  208.         while (iterator.hasNext()) {
  209.             System.out.print( "'" + iterator.next());
  210.             if(iterator.hasNext()){
  211.                 System.out.print("', ");
  212.             } else {
  213.                 System.out.println("')");
  214.             }
  215.         }
  216.         System.out.println();
  217.     }
  218.  
  219.     static void outputToFile(final String path, final Set<Character> answer) {
  220.         try (FileWriter fw = new FileWriter(path)) {
  221.             fw.write("Answer is:\n(");
  222.             Iterator<Character> iterator = answer.iterator();
  223.             while (iterator.hasNext()) {
  224.                 fw.write("'" + iterator.next() + "', ");
  225.             }
  226.             fw.write(")");
  227.             System.out.println("Done");
  228.         } catch (Exception e) {
  229.             System.out.println("File error!");
  230.         }
  231.     }
  232.  
  233.     static void output(final byte source, final Set<Character> answer) {
  234.         String outPath;
  235.         if (source == 1) {
  236.             outputToConsole(answer);
  237.         } else {
  238.             outPath = takeOutPath();
  239.             outputToFile(outPath, answer);
  240.         }
  241.     }
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement