egogoboy

my 1'st project

Nov 8th, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 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.         displayHello();
  10.         do {
  11.             input.userInput();
  12.             switch (input.getAns()) {
  13.                 case ("manual") : input.displayManual(); break;
  14.                 case ("new array") :
  15.                     if (mass.getStatus()) {
  16.                         input.arrayIsAlreadyCreated();
  17.                         input.userInput();
  18.                         while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
  19.                             input.userInput();
  20.                             input.displayError();
  21.                         }
  22.                         if (input.getAns().equals("n"))
  23.                             break;
  24.                     }
  25.                     mass = new array();
  26.                     mass.createArray();
  27.                 break;
  28.                 case ("set array name"): mass.setName(); break;
  29.                 case ("set array size"): mass.setSize(); break;
  30.                 case ("set array"): mass.setElements(); break;
  31.                 case ("view array"): mass.displayArray(); break;
  32.                 default : if (!input.getAns().equals("exit"))
  33.                             input.displayError();
  34.                 break;
  35.             }
  36.         } while (!input.getAns().equals("exit"));
  37.  
  38.     }
  39.     static void displayHello() {
  40.         System.out.print("Welcome to my first project!\nIf you want to see manual, enter 'manual'\n" +
  41.                 "If you want to exit, enter 'exit'\n");
  42.     }
  43. }
  44.  
  45. class actions {
  46.     static String user;
  47.     {
  48.         System.out.print("user actions is ready to use\n");
  49.     }
  50.     public void userInput(){
  51.         Scanner in = new Scanner(System.in);
  52.         System.out.print("> ");
  53.         user = in.nextLine();
  54.     }
  55.     public String getAns(){
  56.         return this.user;
  57.     }
  58.     public void displayError() {
  59.         System.out.print("Unknown command X(\n");
  60.     }
  61.     public void arrayIsAlreadyCreated() {
  62.         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");
  63.     }
  64.     public void displayManual() {
  65.         System.out.print("-----Manual-----\n" +
  66.                 "new array : deletes the old array and creates a completely new one\n" +
  67.                 "set array name : sets the name of the current array\n" +
  68.                 "set array size : sets the size of the current array (this will destroy all the elements!)\n" +
  69.                 "set array : set array elements\n" +
  70.                 "view array : outputs the contents of the current array\n" +
  71.                 "start sort : starts sorting the array\n" +
  72.                 "exit : breaks the program\n");
  73.     }
  74. }
  75. class Sort {
  76.     public static void insertionSort(array m) {
  77.  
  78.     }
  79. }
  80. class array{
  81.     Scanner in = new Scanner(System.in);
  82.     static int size;
  83.     static int[] nums;
  84.     static String name;
  85.     static boolean created = false, elements = false;
  86.     {
  87.         this.size = 0;
  88.         name = "null";
  89.         created = true;
  90.         elements = false;
  91.         System.out.print("array created successfully!\n");
  92.     }
  93.     public void createArray() throws IOException {
  94.         setName();
  95.         setSize();
  96.         nums = new int[size];
  97.         setElements();
  98.     }
  99.     public void setElements() throws IOException {
  100.         System.out.print("Enter new elements of " + name + ": ");
  101.         if (!elements) {
  102.             for (int i = 0; i < size; ++i) {
  103.                 elements = true;
  104.                 nums[i] = in.nextInt();
  105.             }
  106.             System.out.print("The array has been filled successfully!\n");
  107.         }
  108.         else {
  109.             System.out.print("The array is already filled. do you want to fill it out again? [y/n]\n> ");
  110.             char ans = (char) System.in.read();
  111.             while (ans != 'y' && ans != 'n') {
  112.                 ans = (char) System.in.read();
  113.                 System.out.print("Unknown command X(\n> ");
  114.             }
  115.             if (ans == 'y') {
  116.                 System.out.print("Enter new elements of " + name + ": ");
  117.                 for (int i = 0; i < size; ++i) {
  118.                     elements = true;
  119.                     nums[i] = in.nextInt();
  120.                 }
  121.                 System.out.print("The array has been filled successfully!\n");
  122.             }
  123.         }
  124.     }
  125.     public void setSize() throws IOException{
  126.         System.out.print("Enter new size of " + name + ": ");
  127.         int size = in.nextInt();
  128.         if (this.size == 0) {
  129.             this.size = size;
  130.         }
  131.         else {
  132.             System.out.print("The size of the array has already been set. do you want to change it? [y/n]\n> ");
  133.             char ans = (char) System.in.read();
  134.             while (ans != 'y' && ans != 'n') {
  135.                 ans = (char) System.in.read();
  136.                 System.out.print("Unknown command X(\n> ");
  137.             }
  138.             if (ans == 'y') {
  139.                 this.size = size;
  140.             }
  141.         }
  142.         nums = new int[this.size];
  143.         System.out.print("New size of array: " + this.size + "\n");
  144.     }
  145.     public void setName() throws IOException{
  146.         System.out.print("Enter the new name of array: ");
  147.         String name = in.nextLine();
  148.         if (this.name.equals("null")) {
  149.             this.name = name;
  150.             System.out.print("New name of array: " + this.name + "\n");
  151.         }
  152.         else {
  153.             System.out.print("The array already has a name. Are you sure you want to rename it? [y/n]\n> ");
  154.             char ans = (char) System.in.read();
  155.             while (ans != 'y' && ans != 'n') {
  156.                 ans = (char) System.in.read();
  157.                 System.out.print("Unknown command X(\n> ");
  158.             }
  159.             if (ans == 'y') {
  160.                 this.name = name;
  161.                 System.out.print("New name of mass: " + this.size + "\n");
  162.             }
  163.         }
  164.     }
  165.     public int getSize() {
  166.         return this.size;
  167.     }
  168.     public boolean getStatus() {
  169.         return created;
  170.     }
  171.     public int[] getArray() {
  172.         return this.nums;
  173.     }
  174.     public void displayArray() {
  175.         System.out.print("Name of array: " + name +
  176.                 "\nSize of array: " + size +
  177.                 "\nElements of array: ");
  178.         for (int i = 0; i < size; ++i)
  179.             System.out.print(nums[i] + " ");
  180.         System.out.print("\n");
  181.     }
  182.     public void notCreated() {
  183.         created = false;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment