//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import Classes.Student;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {


    String sourceDir = "C:\\Users\\Akivk0\\Desktop\\Новая папка";
    String outputFile = "C:\\Users\\Akivk0\\Desktop\\Новая папка\\1.txt";
    String extension = ".cs";

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
        Files.walk(Paths.get(sourceDir))
                .filter(Files::isRegularFile)
                .filter(filePath -> filePath.toString().endsWith(extension))
                .filter(filePath -> !filePath.toString().endsWith(".Designer" + extension))
                .forEach(filePath -> {
                    try {
                        writer.write("Файл \"" + filePath.getFileName().toString() + "\"");
                        writer.newLine();
                        Files.lines(filePath)
                                .filter(line -> !line.trim().isEmpty())
                                .forEach(line -> {
                                    try {
                                        writer.write(line);
                                        writer.newLine();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                });
                        writer.newLine(); // Add a newline between files
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
        System.out.println("Merging completed. Output file: " + outputFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}


    private static void FirstProgram(){


        Vector<Integer> firstVector = new Vector<>();

        System.out.println("Enter the number of elements: ");
        Scanner in = new Scanner(System.in);
        int numberOfElementsInArray = in.nextInt();

        Random randomNumber = new Random();
        for (int i = 0; i < numberOfElementsInArray; i++) firstVector.add(randomNumber.nextInt(20));

        System.out.print("First vector: ");
        for (int element: firstVector) System.out.print(element + " ");
        System.out.println();


        System.out.println();
        System.out.println("Enter the number of the element that will be replaced ");
        int numberOfElement = in.nextInt();
        if (firstVector.size() > numberOfElement ) {

            int firstVectorSizeReplace = firstVector.size();
            firstVector.subList(numberOfElement, firstVector.size()).clear();

            for (int i = 0; i < firstVectorSizeReplace - numberOfElement; i++) firstVector.add(randomNumber.nextInt(20));

            System.out.print("First vector: ");
            Iterator<Integer> iterator = firstVector.iterator();
            while(iterator.hasNext()){
                System.out.print(iterator.next() + " ");
            }
            System.out.println();
        }




        Vector<Integer> secondVector = new Vector<>();

        System.out.println("Enter the number of elements for second vector: ");
        numberOfElementsInArray = in.nextInt();
        for (int i = 0; i < numberOfElementsInArray; i++) secondVector.add(randomNumber.nextInt(20));




        System.out.println("Enter the number of the element that will be replaced ");
        numberOfElement = in.nextInt();
        if (firstVector.size() > numberOfElement ) {

            firstVector.subList(numberOfElement, firstVector.size()).clear();
            firstVector.addAll(secondVector);

            System.out.print("First vector: ");
            for (int element: firstVector) System.out.print(element + " ");
            System.out.println();

            System.out.print("Second vector: ");
            for (int element: secondVector) System.out.print(element + " ");
            System.out.println();
        }


    }

    private static void SecondProgram(){
        Vector<Student> firstVector = new Vector<>();
        String[] names = {"Harry", "Ron", "Hermione",  "Snape", "Dumbledore"};

        System.out.println("////////////Enter the number of elements: ");
        Scanner in = new Scanner(System.in);
        int numberOfElements = in.nextInt();
        Random randomNumber = new Random();
        for (int i = 0; i < numberOfElements; i++) firstVector.add(new Student(names[randomNumber.nextInt(names.length)],
                                                            randomNumber.nextInt(4) + 1, randomNumber.nextBoolean()));

        System.out.println("First vector: ");
        for (Student element: firstVector) {
            element.Print();
            System.out.println();
        }



        System.out.println("////////////Enter the number of the element that will be replaced ");
        int numberOfElement = in.nextInt();
        if (firstVector.size() > numberOfElement ) {

            int firstVectorSizeReplace = firstVector.size();
            firstVector.subList(numberOfElement, firstVector.size()).clear();

            for (int i = 0; i < firstVectorSizeReplace - numberOfElement; i++)
                firstVector.add(new Student(names[randomNumber.nextInt(names.length - 1 )],
                                        randomNumber.nextInt(3) + 1,
                                                randomNumber.nextBoolean()));

            System.out.print("First vector: ");
            Iterator<Student> iterator = firstVector.iterator();
            while(iterator.hasNext()){
                iterator.next().Print();
                System.out.println();
            }
        }


        Vector<Student> secondVector = new Vector<>();

        System.out.println("////////////Enter the number of elements for second vector: ");
        numberOfElements = in.nextInt();
        for (int i = 0; i < numberOfElements; i++) secondVector.add(new Student(names[randomNumber.nextInt(names.length - 1 )],
                randomNumber.nextInt(3) + 1,
                randomNumber.nextBoolean()));




        System.out.println("Enter the number of the element that will be replaced ");
        numberOfElement = in.nextInt();
        if (firstVector.size() > numberOfElement ) {

            firstVector.subList(numberOfElement, firstVector.size()).clear();
            firstVector.addAll(secondVector);

            System.out.print("First vector: ");
            for (Student element: firstVector) {
                element.Print();
                System.out.println();
            }

            System.out.print("Second vector: ");
            for (Student element: secondVector) {
                element.Print();
                System.out.println();
            }
        }


    }

    private static void ThirdProgram() {
        Vector<Student> firstVector = new Vector<>();
        String[] names = {"Harry", "Ron", "Hermione",  "Snape", "Dumbledore"};

        System.out.println("////////////Enter the number of elements: ");
        Scanner in = new Scanner(System.in);
        int numberOfElements = in.nextInt();
        Random randomNumber = new Random();
        for (int i = 0; i < numberOfElements; i++) firstVector.add(new Student(names[randomNumber.nextInt(names.length)],
                randomNumber.nextInt(4) + 1,
                randomNumber.nextBoolean()));

        System.out.println("First vector: ");
        for (Student element: firstVector) {
            element.Print();
            System.out.println();
        }




        Collections.sort(firstVector);

        System.out.println("////////////Sorted first vector: ");
        for (Student element: firstVector) {
            element.Print();
            System.out.println();
        }



        System.out.println("////////////Enter desired student: ");

        System.out.println("Name: ");
        String nameStudent = in.next();

        System.out.println("Course: ");
        int courseStudent = in.nextInt();

        System.out.println("Sex: ");
        Boolean sexStudent = in.nextBoolean();

        Student searchedStudent = new Student(nameStudent, courseStudent, sexStudent);

        TreeMap<Integer, Student> studentTreeMap = new TreeMap<>();




        for (int i = 0; i < firstVector.size(); i++){
            if(searchedStudent.compareTo(firstVector.get(i)) == 0) {
                System.out.println("Index of desired student = " + i);
                studentTreeMap.put(i,searchedStudent);
            }
        }

        for (Map.Entry<Integer, Student> entry: studentTreeMap.entrySet()){
            System.out.println("Index of student: " + entry.getKey());
            entry.getValue().Print();
            System.out.println();

        }




        Collections.reverse(firstVector);

        System.out.println("////////////Sorted first vector in descending order: ");
        for (Student element: firstVector) {
            element.Print();
            System.out.println();
        }

        System.out.println("Tree in descending order:");
        for (Map.Entry<Integer, Student> entry: studentTreeMap.reversed().entrySet()){
            System.out.println("Index of student: " + entry.getKey());
            entry.getValue().Print();
            System.out.println();

        }



        ArrayList<Student> studentArray = new ArrayList<>();

        studentArray.addAll(firstVector);

        for (Map.Entry<Integer, Student> entry: studentTreeMap.reversed().entrySet()){
            studentArray.add(entry.getValue());
        }

        System.out.println("////////////Third container: ");
        for (Student element: studentArray) {
            element.Print();
            System.out.println();
        }

    }

}