Advertisement
Egor_Vakar

lab2_4(java)

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