Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.regex.PatternSyntaxException;
- public class Main {
- static final HashSet<Character> setOfDigitsAndOperations = new HashSet<>(Set.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '^', '√'));
- static Scanner scan = new Scanner(System.in);
- public static void main(String[] args) {
- 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: ");
- byte inputSource = takeSource();
- Set<Character> initialSet = takeSet(inputSource);
- Set<Character> answer = takeAnswer(initialSet);
- System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
- byte outputSource = takeSource();
- output(outputSource, answer);
- scan.close();
- }
- static int takeSize() {
- final int MIN_SIZE = 2;
- final int MAX_SIZE = 30;
- int number = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- number = Integer.parseInt(scan.nextLine());
- } catch (NumberFormatException e) {
- System.out.println("Incorrect input!!!");
- isIncorrect = true;
- }
- if (!isIncorrect && (number < MIN_SIZE || number > MAX_SIZE)) {
- System.out.println("Incorrect input!!!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return number;
- }
- static char takeChar() {
- char inputChar = 0;
- boolean isIncorrect;
- String line;
- do {
- line = scan.nextLine();
- isIncorrect = false;
- if ((line.length() != 1)){
- System.out.println("Incorrect input!!! Must be char:");
- isIncorrect = true;
- } else {
- try {
- inputChar = (line).charAt(0);
- } catch (NumberFormatException e) {
- System.out.println("Incorrect input!!!");
- isIncorrect = true;
- }
- }
- } while (isIncorrect);
- return inputChar;
- }
- static byte takeSource() {
- final byte CONSOLE = 1;
- final byte FILE = 2;
- boolean isIncorrect;
- byte choice = 0;
- do {
- isIncorrect = false;
- try {
- choice = Byte.parseByte(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- static String takeInPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (Files.notExists(Path.of(path))) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static String takeOutPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static Set<Character> takeSetFromConsole() {
- System.out.print("Enter size: ");
- int size = takeSize();
- Set<Character> initialSet = new HashSet<>();
- for (int i = 0; i < size; i++) {
- System.out.print("Enter element " + (i + 1) + ": ");
- initialSet.add(takeChar());
- }
- return initialSet;
- }
- static Set<Character> takeSetFromFile(final String path) {
- final int SET_LINE_NUMBER = 1;
- int lineCounter = 0;
- String line;
- boolean isCorrect = true;
- String[] stringArray = null;
- try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
- while ((isCorrect) && ((line = reader.readLine()) != null)) {
- lineCounter++;
- if (lineCounter == SET_LINE_NUMBER) {
- try {
- stringArray = line.split(" ");
- } catch (PatternSyntaxException e) {
- System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
- isCorrect = false;
- }
- }
- }
- } catch (IOException e) {
- System.out.println("Input/Output error!!!");
- isCorrect = false;
- }
- Set<Character> initialSet = new HashSet<>();
- if (stringArray != null) {
- if ((isCorrect) && (lineCounter > SET_LINE_NUMBER)) {
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- } else {
- for (int i = 0; i < stringArray.length; i++) {
- try {
- initialSet.add((stringArray[i]).charAt(0));
- } catch (Exception e) {
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }
- }
- }
- }
- if (isCorrect) {
- return initialSet;
- } else {
- return null;
- }
- }
- static Set<Character> takeSet(final byte source) {
- String inPath;
- Set<Character> initialSet;
- if (source == 1) {
- initialSet = takeSetFromConsole();
- } else {
- inPath = takeInPath();
- initialSet = takeSetFromFile(inPath);
- while (initialSet == null) {
- inPath = takeInPath();
- initialSet = takeSetFromFile(inPath);
- }
- }
- return initialSet;
- }
- static Set<Character> takeAnswer(Set<Character> initialSet) {
- initialSet.retainAll(setOfDigitsAndOperations);
- return initialSet;
- }
- static void outputToConsole(Set<Character> answer) {
- System.out.print("Answer is:\n(");
- Iterator<Character> iterator = answer.iterator();
- while (iterator.hasNext()) {
- System.out.print( "'" + iterator.next());
- if(iterator.hasNext()){
- System.out.print("', ");
- } else {
- System.out.println("')");
- }
- }
- System.out.println();
- }
- static void outputToFile(final String path, final Set<Character> answer) {
- try (FileWriter fw = new FileWriter(path)) {
- fw.write("Answer is:\n(");
- Iterator<Character> iterator = answer.iterator();
- while (iterator.hasNext()) {
- fw.write("'" + iterator.next() + "', ");
- }
- fw.write(")");
- System.out.println("Done");
- } catch (Exception e) {
- System.out.println("File error!");
- }
- }
- static void output(final byte source, final Set<Character> answer) {
- String outPath;
- if (source == 1) {
- outputToConsole(answer);
- } else {
- outPath = takeOutPath();
- outputToFile(outPath, answer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement