Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.7 kB
4
Indexable
Never
package ru.vsu.cs.vvp2022.g112.sibirko;


import ru.vsu.cs.util.ArrayUtils;
import ru.vsu.cs.util.SwingUtils;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;


public class Program {

    public static class CmdParams {
        public String inputFile;
        public String outputFile;
        public boolean error;
        public boolean help;
        public boolean window;
        public boolean tests = false;
        public String[] testFiles;
    }

    public static CmdParams parseArgs(String[] args) {
        CmdParams params = new CmdParams();
        for (int arg = 0; arg < args.length; arg++) {
            if (Objects.equals(args[arg], "--window")){
                params.window = true;
                return params;
            }
            if (Objects.equals(args[arg], "--tests")){
                int iterator = 0;
                String[] testFiles = new String[args.length - arg - 1];
                params.tests = true;
                for (int i = arg + 1; i < args.length; i++) {
                    testFiles[iterator] = args[i];
                    iterator++;
                }
                params.testFiles = testFiles;
                return params;
            }
            if (Objects.equals(args[arg], "--help")){
                params.help = true;
                return params;
            }
            if (Objects.equals(args[arg], "-i")){
                if (arg + 1 >= args.length){
                    params.error = true;
                    params.help = true;
                }
                params.inputFile = args[arg + 1];
            }
            if (Objects.equals(args[arg], "-o")){
                if (arg + 1 >= args.length){
                    params.error = true;
                    params.help = true;
                }
                params.outputFile = args[arg + 1];
            }
        }
        if (!params.tests){
            if (params.inputFile == null || params.outputFile == null){
                params.error = true;
                params.help = true;
                return params;
            }
        }
        return params;
    }

    public static void winMain() {
        SwingUtils.setLookAndFeelByName("Windows");
        Locale.setDefault(Locale.ROOT);
        //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtils.setDefaultFont("Microsoft Sans Serif", 18);

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> new FrameMain().setVisible(true));
    }

    public static void main(String[] args) throws Exception {
        CmdParams params = parseArgs(args);
        if (params.help) {
            PrintStream out = params.error ? System.err : System.out;
            out.println("Usage:");
            out.println("  <cmd> args");
            out.println("    --tests  [FILES] // Run tests");
            out.println("    -i FILE // Input file");
            out.println("    -o FILE  // Output file");
            out.println("  <cmd> --help // Show help");
            out.println("  <cmd> --window  // Show window");
            System.exit(params.error ? 1 : 0);
        }
        if (params.window) {
            winMain();
        } else {
            if (params.tests){
                for (String file : params.testFiles) {
                    int[] arr = ArrayUtils.readIntArrayFromFile(file);
                    if (arr == null) {
                        System.err.printf("Can't read array from \"%s\"%n", file);
                        System.exit(2);
                    }
                    Task.createNewList(TaskUtils.convertArrayToList(arr));
                    PrintStream out = System.out;
                    out.println();
                    System.out.println("--- " + file + " ---");
                    out.println(ArrayUtils.toString(arr));
                }
                return;
            }
            int[] arr = ArrayUtils.readIntArrayFromFile(params.inputFile);
            if (arr == null) {
                System.err.printf("Can't read array from \"%s\"%n", params.inputFile);
                System.exit(2);
            }
            int[] result;
            ArrayList<Integer> list1 = TaskUtils.convertArrayToList(arr);
            ArrayList<Integer> finalList = Task.createNewList(list1);
            result = TaskUtils.convertListToArray(finalList);

            PrintStream out = new PrintStream(params.outputFile);
            out.println(ArrayUtils.toString(result));
            out.close();
        }
    }
}