egogoboy

array && sort

Nov 10th, 2023
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.20 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) throws IOException {
  6.         actions input = new actions();
  7.         array mass = new array();
  8.         mass.notCreated();
  9.         Sort sort = new Sort();
  10.         displayHello();
  11.         do {
  12.             input.userInput();
  13.             switch (input.getAns()) {
  14.                 case ("manual") : input.displayManual(); break;
  15.                 case ("new array") :
  16.                     if (mass.getStatus()) {
  17.                         input.arrayIsAlreadyCreated();
  18.                         input.userInput();
  19.                         while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
  20.                             input.userInput();
  21.                             input.displayError();
  22.                         }
  23.                         if (input.getAns().equals("n"))
  24.                             break;
  25.                     }
  26.                     mass = new array();
  27.                     mass.createArray();
  28.                 break;
  29.                 case ("set array name"): mass.setName(); break;
  30.                 case ("set array size"): mass.setSize(); break;
  31.                 case ("set array"): mass.setElements(); break;
  32.                 case ("view array"): mass.displayArray(); break;
  33.                 case ("start sort"):
  34.                     sort.insertionSort(mass); break;
  35.                 default : if (!input.getAns().equals("exit"))
  36.                             input.displayError();
  37.                 break;
  38.             }
  39.         } while (!input.getAns().equals("exit"));
  40.  
  41.     }
  42.     static void displayHello() {
  43.         System.out.print("Welcome to my first project!\nIf you want to see manual, enter 'manual'\n" +
  44.                 "If you want to exit, enter 'exit'\n");
  45.     }
  46. }
  47.  
  48. class actions {
  49.     static String user;
  50.     {
  51.         System.out.print("user actions is ready to use\n");
  52.     }
  53.     public void userInput(){
  54.         Scanner in = new Scanner(System.in);
  55.         System.out.print("> ");
  56.         user = in.nextLine();
  57.     }
  58.     public String getAns(){
  59.         return this.user;
  60.     }
  61.     public void displayError() {
  62.         System.out.print("Unknown command X(\n");
  63.     }
  64.     public void arrayIsAlreadyCreated() {
  65.         System.out.print("The array has already been created. Do you want to delete the current array and create a new one?[y/n]\n");
  66.     }
  67.     public void displayManual() {
  68.         System.out.print("-----Manual-----\n" +
  69.                 "new array : deletes the old array and creates a completely new one\n" +
  70.                 "set array name : sets the name of the current array\n" +
  71.                 "set array size : sets the size of the current array (this will destroy all the elements!)\n" +
  72.                 "set array : set array elements\n" +
  73.                 "view array : outputs the contents of the current array\n" +
  74.                 "start sort : starts sorting the array\n" +
  75.                 "exit : breaks the program\n");
  76.     }
  77. }
  78. class Sort {
  79.     static Scanner in = new Scanner(System.in);
  80.     public static void insertionSort(array m) throws IOException{
  81.         System.out.print("Enter option (>, <): ");
  82.         String option = in.next();
  83.         while (!option.equals(">") && !option.equals("<") && !option.equals(">=") && !option.equals("<=")) {
  84.             System.out.print("Unknown command X(\n> ");
  85.             option = in.next();
  86.         }
  87.         System.out.print("!!!Indexing starts with 1!!!\n");
  88.         System.out.print("Enter start index: ");
  89.         int start = in.nextInt();
  90.         System.out.print("Enter end index: ");
  91.         int stop = in.nextInt();
  92.         int ind = start, temp = m.getElement(ind);
  93.         while (ind < stop) {
  94.             temp = m.getElement(ind);
  95.             for (int i = ind; i >= start - 1; --i) {
  96.                 if (i == start - 1) {
  97.                     m.changeElement(i, temp);
  98.                 } else
  99.                     switch (option) {
  100.                         case (">"):
  101.                             if (m.getElement((i - 1)) > temp) {
  102.                                 m.swapElements((i - 1), i);
  103.                             }
  104.                             else {
  105.                                 m.changeElement(i, temp);
  106.                                 break;
  107.                             }
  108.                         break;
  109.                         case ("<"):
  110.                             if (m.getElement((i - 1)) < temp) {
  111.                                 m.swapElements((i - 1), i);
  112.                             }
  113.                             else {
  114.                                 m.changeElement(i, temp);
  115.                                 break;
  116.                             }
  117.                         break;
  118.                     }
  119.             }
  120.             ind++;
  121.         }
  122.         System.out.print("Sort has been completed successfully!\n");
  123.     }
  124. }
  125. class array{
  126.     Scanner in = new Scanner(System.in);
  127.     static int size;
  128.     static int[] nums;
  129.     static String name;
  130.     private actions input = new actions();
  131.     static boolean created = false, elements = false;
  132.     {
  133.         this.size = 0;
  134.         name = "null";
  135.         created = true;
  136.         elements = false;
  137.         System.out.print("array created successfully!\n");
  138.     }
  139.     public void createArray() throws IOException {
  140.         setName();
  141.         setSize();
  142.         nums = new int[size];
  143.         setElements();
  144.     }
  145.     public void setElements() throws IOException {
  146.         if (!elements) {
  147.             System.out.print("Enter new elements of " + name + ": ");
  148.             elements = true;
  149.             for (int i = 0; i < size; ++i) {
  150.                 nums[i] = in.nextInt();
  151.             }
  152.             System.out.print("The array has been filled successfully!\n");
  153.         }
  154.         else {
  155.             System.out.print("The array is already filled. Do you want to fill it out again? [y/n]\n");
  156.             input.userInput();
  157.             while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
  158.                 input.userInput();
  159.                 input.displayError();
  160.             }
  161.             if (input.getAns().equals("y")) {
  162.                 System.out.print("Enter new elements of " + name + ": ");
  163.                 elements = true;
  164.                 for (int i = 0; i < size; ++i) {
  165.                     nums[i] = in.nextInt();
  166.                 }
  167.                 System.out.print("The array has been filled successfully!\n");
  168.             }
  169.         }
  170.     }
  171.     public void setSize() throws IOException{
  172.         System.out.print("Enter new size of " + name + ": ");
  173.         int size = in.nextInt();
  174.         if (this.size == 0) {
  175.             this.size = size;
  176.             System.out.print("New size of array: " + this.size + "\n");
  177.         }
  178.         else {
  179.             System.out.print("The size of the array has already been set. do you want to change it?" +
  180.                     " (this will destroy all the elements!) [y/n]\n");
  181.             input.userInput();
  182.             while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
  183.                 input.userInput();
  184.                 input.displayError();
  185.             }
  186.             if (input.getAns().equals("y")) {
  187.                 this.size = size;
  188.                 elements = false;
  189.                 System.out.print("New size of array: " + this.size + "\n");
  190.             }
  191.         }
  192.         nums = new int[this.size];
  193.  
  194.     }
  195.     public void setName() throws IOException{
  196.         System.out.print("Enter the new name of array: ");
  197.         String name = in.next();
  198.         while (name.equals("null")) {
  199.             System.out.print("You cannot use this name!\nEnter the new name of array: ");
  200.             name = in.next();
  201.         }
  202.         if (this.name.equals("null")) {
  203.             this.name = name;
  204.             System.out.print("New name of array: " + this.name + "\n");
  205.         }
  206.         else {
  207.             System.out.print("The array already has a name. Are you sure you want to rename it? [y/n]\n");
  208.             input.userInput();
  209.             while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
  210.                 input.userInput();
  211.                 input.displayError();
  212.             }
  213.             if (input.getAns().equals("y")) {
  214.                 this.name = name;
  215.                 System.out.print("New name of mass: " + this.name + "\n");
  216.             }
  217.         }
  218.     }
  219.     public int getSize() {
  220.         return this.size;
  221.     }
  222.     public int getElement(int ind) {
  223.         return nums[ind];
  224.     }
  225.     public boolean getStatus() {
  226.         return created;
  227.     }
  228.     public int[] getArray() {
  229.         return this.nums;
  230.     }
  231.     public void changeElement(int ind, int temp) {
  232.         nums[ind] = temp;
  233.         return;
  234.     }
  235.     public void swapElements(int l, int r) {
  236.         int temp = nums[l];
  237.         nums[l] = nums[r];
  238.         nums[r] = temp;
  239.         return;
  240.     }
  241.     public void displayArray() {
  242.         System.out.print("Name of array: " + name +
  243.                 "\nSize of array: " + size +
  244.                 "\nElements of array: ");
  245.         for (int i = 0; i < size; ++i)
  246.             System.out.print(nums[i] + " ");
  247.         System.out.print("\n");
  248.     }
  249.     public void notCreated() {
  250.         created = false;
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment