18126

Untitled

Jun 8th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Task {
  4.  
  5.     public static void Writing(String text) {
  6.         for (int i = 0; i < text.length(); i++) {
  7.             System.out.print("" + text.charAt(i));
  8.             try {
  9.                 Thread.sleep(1);
  10.             } catch (Exception e) {
  11.                 e.printStackTrace();
  12.             }
  13.         }
  14.     }
  15.  
  16.     public static void Waiting(String text, int n) {
  17.         for (int i = 0; i < text.length(); i++) {
  18.             System.out.print("" + text.charAt(i));
  19.             try {
  20.                 Thread.sleep(1);
  21.             } catch (Exception e) {
  22.                 e.printStackTrace();
  23.             }
  24.         }
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.         Scanner scan = new Scanner(System.in);
  29.         int spaces = 154;
  30.         int number = 1;
  31.         int parkingNumber = 1;
  32.         int firstSortedNumber = 74;
  33.         int[] spaceNumber = new int[spaces];
  34.         int[] spaceCheck = new int[spaces];
  35.         for (int i = 0; i < spaceCheck.length; i++) {
  36.             spaceCheck[i] = i;
  37.         }
  38.         double[] spaceSize = new double[spaces];
  39.         String[] spaceOwner = new String[spaces];
  40.         String[] spaceDescription = new String[spaces];
  41.         String[] spaceOwnerSort = new String[spaceOwner.length];
  42.         boolean running = true;
  43.         boolean choice = false;
  44.         boolean choiceSort = false;
  45.         boolean choiceRegister = false;
  46.         System.out.println("|----------Welcome!----------|\n");
  47.         Writing("Welcome to Ivan's parking slots.\n");
  48.         Writing("What would you like to do?");
  49.         while (running) {
  50.             System.out.println("\n1 - Register my parking slot.");
  51.             System.out.println("2 - Sort parking slots.");
  52.             int tempInt = Integer.parseInt(scan.nextLine());
  53.             while (!choice) {
  54.                 if (tempInt == 1) {
  55.                     choice = true;
  56.                     choiceRegister = true;
  57.                     System.out.println();
  58.                     break;
  59.                 } else if (tempInt == 2) {
  60.                     choice = true;
  61.                     choiceSort = true;
  62.                     System.out.println();
  63.                     break;
  64.                 } else {
  65.                     Writing(tempInt + " is not a valid number!\n");
  66.                     System.out.println("1 - Register my parking slot.");
  67.                     System.out.println("2 - Sort parking slots.");
  68.                     tempInt = Integer.parseInt(scan.nextLine());
  69.                 }
  70.             }
  71.             try {
  72.                 if (choiceRegister) {
  73.                     if (number <= spaces) {
  74.                         Writing("|===STARTING REGISTRATION===|\n");
  75.                         Writing("\t\t[");
  76.                         for (int i = 0; i < 7; i++) {
  77.                             Waiting("-", 800);
  78.                         }
  79.                         Writing("]");
  80.                         Writing("\n\nPlease, insert your first and last name: ");
  81.                         String tempString = scan.nextLine();
  82.                         spaceOwner[number - 1] = tempString;
  83.                         Writing("Please, insert parking slot's size(in square meters): ");
  84.                         double tempDouble = Double.parseDouble(scan.nextLine());
  85.                         spaceSize[number - 1] = tempDouble;
  86.                         Writing("Before getting your parking number, please input description: ");
  87.                         tempString = scan.nextLine();
  88.                         spaceDescription[number - 1] = tempString;
  89.                         Writing("Your parking number is: " + number + " (out of " + spaces + " parking slots)\n");
  90.                         spaceNumber[number - 1] = parkingNumber;
  91.                         System.out.println();
  92.                         Writing("Your parking space is successfully registered.\n");
  93.                         System.out.println("----------Have a nice day!----------");
  94.                         Writing("\nOwner: " + spaceOwner[number - 1]);
  95.                         Writing("\nNumber: " + spaceNumber[number - 1]);
  96.                         Writing("\nSize: " + spaceSize[number - 1]);
  97.                         Writing("\nDescription: " + spaceDescription[number - 1]);
  98.                         System.out.println();
  99.                         System.out.println();
  100.                         number++;
  101.                         parkingNumber++;
  102.                         System.out.println("\n|------------------------------------|");
  103.                         choiceRegister = false;
  104.                         choice = false;
  105.                     } else {
  106.                         System.out.println("Sorry, no more free parking slots :(");
  107.                         choiceRegister = false;
  108.                         choice = false;
  109.                     }
  110.                 }
  111.             } catch (Exception e) {
  112.                 System.out.println("Error accused: " + e);
  113.                 choiceRegister = false;
  114.                 choice = false;
  115.             }
  116.             if (choiceSort) {
  117.                 for (int i = 0; i < spaceCheck.length; i++) {
  118.                     spaceCheck[i] = i;
  119.                 }
  120.                 choiceSort = false;
  121.                 choice = false;
  122.                 Writing("What would you like to do?\n");
  123.                 System.out.println("1 - Sort by parking slots");
  124.                 System.out.println("2 - Sort by parking owner");
  125.                 System.out.println("3 - Sort by parking size");
  126.                 Writing("Insert sorting method: ");
  127.                 tempInt = Integer.parseInt(scan.nextLine());
  128.                 if (tempInt == 1) {//by slots
  129.                     System.out.println("Slot : Owner : Size : Description");
  130.                     for (int i = (spaces - (spaces - firstSortedNumber)) - 1; i < spaces - (spaces - firstSortedNumber); i++) {
  131.                         System.out.printf("%d : %s : %.2f m2 : %s\n", i + 1, spaceOwner[i], spaceSize[i], spaceDescription[i]);
  132.                     }
  133.                     for (int i = 0; i < (spaces - (spaces - firstSortedNumber)) - 1; i++) {
  134.                         System.out.printf("%d : %s : %.2f m2 : %s\n", i + 1, spaceOwner[i], spaceSize[i], spaceDescription[i]);
  135.                     }
  136.                     for (int i = spaces - (spaces - firstSortedNumber); i < spaces; i++) {
  137.                         System.out.printf("%d : %s : %.2f m2 : %s\n", i + 1, spaceOwner[i], spaceSize[i], spaceDescription[i]);
  138.                     }
  139.                     System.out.println("Slot : Owner : Size : Description");
  140.                 } else if (tempInt == 2) {//by owner
  141.                     System.arraycopy(spaceOwner, 0, spaceOwnerSort, 0, spaceOwner.length);
  142.                     System.out.println("Slot : Owner : Size : Description");
  143.                     for(int i = 0; i < spaceOwnerSort.length; i++) {
  144.                         if(spaceOwnerSort[i] == null) {
  145.                             spaceOwnerSort[i] = "Empty";
  146.                         }
  147.                     }
  148.                     for(int a = 0; a < spaceOwnerSort.length - 1; a++) {
  149.                         for(int b = a + 1; b < spaceOwnerSort.length; b++) {
  150.                             if(spaceOwnerSort[a].compareTo(spaceOwnerSort[b]) > 0 && !spaceOwnerSort[a].equals("Empty") && !spaceOwnerSort[b].equals("Empty")) {
  151.                                 String temp = spaceOwnerSort[a];
  152.                                 spaceOwnerSort[a] = spaceOwnerSort[b];
  153.                                 spaceOwnerSort[b] = temp;
  154.                             }
  155.                         }
  156.                     }
  157.                     for (int i = (spaces - (spaces - firstSortedNumber)) - 1; i < spaces - (spaces - firstSortedNumber); i++) {
  158.                         System.out.printf("%d : %s : %.2f m2 : %s\n", spaceCheck[i]+1, spaceOwnerSort[spaceCheck[i]], spaceSize[spaceCheck[i]], spaceDescription[spaceCheck[i]]);
  159.                     }
  160.                     for (int i = 0; i < (spaces - (spaces - firstSortedNumber)) - 1; i++) {
  161.                         System.out.printf("%d : %s : %.2f m2 : %s\n", spaceCheck[i]+1, spaceOwnerSort[spaceCheck[i]], spaceSize[spaceCheck[i]], spaceDescription[spaceCheck[i]]);
  162.                     }
  163.                     for (int i = spaces - (spaces - firstSortedNumber); i < spaces; i++) {
  164.                         System.out.printf("%d : %s : %.2f m2 : %s\n", spaceCheck[i]+1, spaceOwnerSort[spaceCheck[i]], spaceSize[spaceCheck[i]], spaceDescription[spaceCheck[i]]);
  165.                     }
  166.                 } else if (tempInt == 3) {//by size
  167.                     int[] slots = new int[154];
  168.                     for (int i = 0; i < slots.length; i++) {
  169.                         slots[i] = i;
  170.                     }
  171.                     for (int i = 0; i < spaceSize.length; i++) {
  172.                         int temp = 0;
  173.                         for (int j = 0; j < spaceSize.length - i; j++) {
  174.                             if (spaceSize[i] < spaceSize[i + j]) {
  175.                                 temp = slots[i];
  176.                                 slots[i] = slots[i + j];
  177.                                 slots[i + j] = temp;
  178.                             }
  179.                         }
  180.                     }
  181.                     System.out.println("Slot : Owner : Size : Description");
  182.                     for (int i = (spaces - (spaces - firstSortedNumber)) - 1; i < spaces - (spaces - firstSortedNumber); i++) {
  183.                         System.out.printf("%d : %s : %.2f m2 : %s\n", slots[i] + 1, spaceOwner[slots[i]], spaceSize[slots[i]], spaceDescription[slots[i]]);
  184.                     }
  185.                     for (int i = 0; i < (spaces - (spaces - firstSortedNumber)) - 1; i++) {
  186.                         System.out.printf("%d : %s : %.2f m2 : %s\n", slots[i] + 1, spaceOwner[slots[i]], spaceSize[slots[i]], spaceDescription[slots[i]]);
  187.                     }
  188.                     for (int i = spaces - (spaces - firstSortedNumber); i < spaces; i++) {
  189.                         System.out.printf("%d : %s : %.2f m2 : %s\n", slots[i] + 1, spaceOwner[slots[i]], spaceSize[slots[i]], spaceDescription[slots[i]]);
  190.                     }
  191.                     System.out.println("Slot : Owner : Size : Description");
  192.                 } else {
  193.                     Writing(tempInt + " is not a valid number!\n");
  194.                     choiceSort = true;
  195.                 }
  196.             }
  197.         }
  198.     }
  199. }
  200.  
Add Comment
Please, Sign In to add comment