Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- actions input = new actions();
- array mass = new array();
- mass.notCreated();
- displayHello();
- do {
- input.userInput();
- switch (input.getAns()) {
- case ("manual") : input.displayManual(); break;
- case ("new array") :
- if (mass.getStatus()) {
- input.arrayIsAlreadyCreated();
- input.userInput();
- while (!input.getAns().equals("y") && !input.getAns().equals("n")) {
- input.userInput();
- input.displayError();
- }
- if (input.getAns().equals("n"))
- break;
- }
- mass = new array();
- mass.createArray();
- break;
- case ("set array name"): mass.setName(); break;
- case ("set array size"): mass.setSize(); break;
- case ("set array"): mass.setElements(); break;
- case ("view array"): mass.displayArray(); break;
- default : if (!input.getAns().equals("exit"))
- input.displayError();
- break;
- }
- } while (!input.getAns().equals("exit"));
- }
- static void displayHello() {
- System.out.print("Welcome to my first project!\nIf you want to see manual, enter 'manual'\n" +
- "If you want to exit, enter 'exit'\n");
- }
- }
- class actions {
- static String user;
- {
- System.out.print("user actions is ready to use\n");
- }
- public void userInput(){
- Scanner in = new Scanner(System.in);
- System.out.print("> ");
- user = in.nextLine();
- }
- public String getAns(){
- return this.user;
- }
- public void displayError() {
- System.out.print("Unknown command X(\n");
- }
- public void arrayIsAlreadyCreated() {
- 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");
- }
- public void displayManual() {
- System.out.print("-----Manual-----\n" +
- "new array : deletes the old array and creates a completely new one\n" +
- "set array name : sets the name of the current array\n" +
- "set array size : sets the size of the current array (this will destroy all the elements!)\n" +
- "set array : set array elements\n" +
- "view array : outputs the contents of the current array\n" +
- "start sort : starts sorting the array\n" +
- "exit : breaks the program\n");
- }
- }
- class Sort {
- public static void insertionSort(array m) {
- }
- }
- class array{
- Scanner in = new Scanner(System.in);
- static int size;
- static int[] nums;
- static String name;
- static boolean created = false, elements = false;
- {
- this.size = 0;
- name = "null";
- created = true;
- elements = false;
- System.out.print("array created successfully!\n");
- }
- public void createArray() throws IOException {
- setName();
- setSize();
- nums = new int[size];
- setElements();
- }
- public void setElements() throws IOException {
- System.out.print("Enter new elements of " + name + ": ");
- if (!elements) {
- for (int i = 0; i < size; ++i) {
- elements = true;
- nums[i] = in.nextInt();
- }
- System.out.print("The array has been filled successfully!\n");
- }
- else {
- System.out.print("The array is already filled. do you want to fill it out again? [y/n]\n> ");
- char ans = (char) System.in.read();
- while (ans != 'y' && ans != 'n') {
- ans = (char) System.in.read();
- System.out.print("Unknown command X(\n> ");
- }
- if (ans == 'y') {
- System.out.print("Enter new elements of " + name + ": ");
- for (int i = 0; i < size; ++i) {
- elements = true;
- nums[i] = in.nextInt();
- }
- System.out.print("The array has been filled successfully!\n");
- }
- }
- }
- public void setSize() throws IOException{
- System.out.print("Enter new size of " + name + ": ");
- int size = in.nextInt();
- if (this.size == 0) {
- this.size = size;
- }
- else {
- System.out.print("The size of the array has already been set. do you want to change it? [y/n]\n> ");
- char ans = (char) System.in.read();
- while (ans != 'y' && ans != 'n') {
- ans = (char) System.in.read();
- System.out.print("Unknown command X(\n> ");
- }
- if (ans == 'y') {
- this.size = size;
- }
- }
- nums = new int[this.size];
- System.out.print("New size of array: " + this.size + "\n");
- }
- public void setName() throws IOException{
- System.out.print("Enter the new name of array: ");
- String name = in.nextLine();
- if (this.name.equals("null")) {
- this.name = name;
- System.out.print("New name of array: " + this.name + "\n");
- }
- else {
- System.out.print("The array already has a name. Are you sure you want to rename it? [y/n]\n> ");
- char ans = (char) System.in.read();
- while (ans != 'y' && ans != 'n') {
- ans = (char) System.in.read();
- System.out.print("Unknown command X(\n> ");
- }
- if (ans == 'y') {
- this.name = name;
- System.out.print("New name of mass: " + this.size + "\n");
- }
- }
- }
- public int getSize() {
- return this.size;
- }
- public boolean getStatus() {
- return created;
- }
- public int[] getArray() {
- return this.nums;
- }
- public void displayArray() {
- System.out.print("Name of array: " + name +
- "\nSize of array: " + size +
- "\nElements of array: ");
- for (int i = 0; i < size; ++i)
- System.out.print(nums[i] + " ");
- System.out.print("\n");
- }
- public void notCreated() {
- created = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment