Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.Scanner;
- public class Array {
- // instance variables - replace the example below with your own
- public static int MaxW = 50;
- public static String Brdr = "|";
- public static int[] IntArray = null;
- public static int IntArraySize = 0;
- public static int tmpInArrSz = -1;
- public static int[] tmpIntBlnk = null;
- public static int DelayMS = 1500;
- public static int CurrIndx = -1;
- public static void main(String[] args) {
- /*
- * Minimum width: 70 characters
- */
- MainMenu();
- }
- public static void MainMenu() {
- while (true) {
- clear();
- println(strLine("="));
- println(strCenter(50, "Array Operations", "||"));
- println(strCenter(50, "Menu", "||"));
- println(strLine("="));
- println(strLeft("[1] Create Array", 2));
- println(strLeft("[2] Insert Elements", 2));
- println(strLeft("[3] Search", 2));
- println(strLeft("[4] Display", 2));
- println(strLeft("[5] Delete", 2));
- println(strLeft("[0] Stop", 2));
- println(strLine("_"));
- Scanner in = new Scanner(System.in);
- print("\n Enter Choice: ");
- if (in.hasNextInt()) {
- int valIn = in.nextInt();
- if (0 <= valIn && valIn <= 5) {
- switch (valIn) {
- case 0:
- System.exit(0);
- break;
- case 1:
- if (IntArray == null) {
- CreateArray();
- break;
- } else {
- errormsg("Array already created");
- continue;
- }
- case 2:
- if (CurrIndx == -2) {
- if (arrTmpCnt() != IntArraySize) {
- InsertElements2();
- break;
- } else {
- errormsg("User already inserted elements");
- continue;
- }
- } else if (IntArray == null) {
- errormsg("Create array first");
- continue;
- } else {
- InsertElements();
- break;
- }
- case 3:
- if (IntArray == null) {
- errormsg("Create array first");
- continue;
- } else if (CurrIndx != -2) {
- errormsg("Insert elements first");
- continue;
- } else {
- Search();
- break;
- }
- case 4:
- if (IntArray == null) {
- errormsg("Create array first");
- continue;
- } else if (CurrIndx != -2) {
- errormsg("Insert elements first");
- continue;
- } else {
- Display();
- break;
- }
- case 5:
- if (tmpInArrSz == 0) {
- errormsg("Array was already empty");
- continue;
- } else if (CurrIndx != -2) {
- errormsg("Complete the array values first");
- continue;
- } else {
- Delete();
- break;
- }
- }
- in.close();
- break;
- } else {
- errormsg("Please enter numbers 0 - 5 only");
- continue;
- }
- } else {
- errormsg("Please enter numbers only");
- continue;
- }
- }
- }
- public static void CreateArray() {
- while (true) {
- clear();
- println(strLine("="));
- println(strCenter(50, "Create Array", "||"));
- println(strLine("="));
- println(strLeft("Maximum size of 5", 2));
- println(strLeft("Minimum size of 20", 2));
- println(strLine("_"));
- print("\n Enter size of array: ");
- Scanner in = new Scanner(System.in);
- if (in.hasNextInt()) {
- IntArraySize = in.nextInt();
- if (IntArraySize >= 5 && IntArraySize <= 20) {
- IntArray = new int[IntArraySize];
- tmpIntBlnk = new int[IntArraySize];
- println("");
- msg("Attention!\nYou have created " + IntArraySize + " arrays.");
- MainMenu();
- in.close();
- break;
- } else {
- errormsg("Please enter numbers 5 - 20 only");
- }
- } else {
- errormsg("Please enter numbers only");
- }
- continue;
- }
- }
- public static void inElPrnt(int[] tmpArray) {
- clear();
- println(strLine("="));
- println(strCenter(50, "Insert Elements", "||"));
- println(strLine("="));
- println(strLeft("Values inside the array:", 2));
- shwTbl(tmpArray, true, true);
- println(strLine("_"));
- println(strLeft("Insert " + IntArraySize + " integers", 2));
- println(strLeft("Enter -99 to terminate", 2));
- println(strLine("_"));
- }
- public static void InsertElements() {
- clear();
- Scanner in = new Scanner(System.in);
- int tmpArray[] = arrCpy(IntArray);
- int itmp = 0;
- if (CurrIndx != -1)
- itmp = CurrIndx;
- for (int i = itmp; i < IntArraySize; i++) {
- String con = "";
- boolean loop = true;
- int input = -1;
- while (loop) {
- inElPrnt(tmpArray);
- print("\n " + strOrdinal((i + 1)) + " integer value at [" + (i) + "] index: ");
- if (in.hasNextInt()) {
- input = in.nextInt();
- if (input == -99) {
- println("");
- loop = false;
- break;
- }
- if (srch(tmpArray, input) != -1) {
- errormsg("Duplicated!");
- continue;
- } else {
- tmpArray[i] = input;
- if (i == IntArraySize - 1) {
- CurrIndx = -2;
- IntArray = arrCpy(tmpArray);
- inElPrnt(tmpArray);
- println("\n");
- msg("Successfully inserted");
- arrTmpCnt();
- loop = false;
- }
- }
- } else {
- errormsg("Please enter numbers only");
- in.next();
- continue;
- }
- break;
- }
- if (input == -99) {
- msg("Are you sure want to leave?");
- println(strLeft("[y] Discard changes and leave", 2));
- println(strLeft("[n] Cancel", 2));
- println(strLeft("[c] Continue Later", 2));
- println(strLine("_"));
- print("\n Please choose:");
- input = -1;
- con = in.next().toLowerCase();
- if (con.equals("n")) {
- i = i - 1;
- loop = true;
- CurrIndx = -1;
- continue;
- } else if (con.equals("y")) {
- loop = false;
- CurrIndx = -1;
- inElPrnt(tmpArray);
- println("\n");
- msg("Terminated by the user");
- break;
- } else if (con.equals("c")) {
- loop = false;
- CurrIndx = i;
- IntArray = arrCpy(tmpArray);
- inElPrnt(tmpArray);
- println("\n");
- msg("Saved by the user");
- arrTmpCnt();
- break;
- } else {
- errormsg("Insert y, n, and c only.");
- println(strLeft("Please try a again", 2));
- println(strLine("_"));
- sleep(1500);
- i = i - 1;
- CurrIndx = -1;
- loop = true;
- continue;
- }
- }
- }
- MainMenu();
- in.close();
- }
- public static void InsertElements2() {
- clear();
- arrTmpCnt();
- Scanner in = new Scanner(System.in);
- int tmpArray[] = arrCpy(IntArray);
- int itmp = 0;
- while (itmp < (IntArraySize - arrTmpCnt())) {
- int i = tmpIntBlnk[itmp];
- String con = "";
- boolean loop = true;
- int input = -1;
- while (loop) {
- inElPrnt(tmpArray);
- print("\n " + strOrdinal((i + 1)) + " integer value at [" + (i) + "] index: ");
- if (in.hasNextInt()) {
- input = in.nextInt();
- if (input == -99) {
- loop = true;
- break;
- }
- if (srch(tmpArray, input) != -1) {
- errormsg("Duplicated!");
- continue;
- } else {
- tmpArray[i] = input;
- if (i == (IntArraySize - arrTmpCnt() - 1)) {
- IntArray = arrCpy(tmpArray);
- inElPrnt(tmpArray);
- println("\n");
- msg("Successfully inserted");
- arrTmpCnt();
- loop = false;
- }
- itmp++;
- }
- } else {
- errormsg("Please enter numbers only");
- in.next();
- continue;
- }
- break;
- }
- if (input == -99) {
- msg("Are you sure want to leave?");
- println(strLeft("[y] Discard changes and leave", 2));
- println(strLeft("[n] Cancel", 2));
- println(strLine("_"));
- print("\n Please choose:");
- input = -1;
- con = in.next().toLowerCase();
- if (con.equals("n")) {
- loop = true;
- continue;
- } else if (con.equals("y")) {
- loop = false;
- inElPrnt(tmpArray);
- println("\n");
- msg("Terminated by the user");
- break;
- } else {
- errormsg("Insert y and n only.");
- println(strLeft("Please try a again", 2));
- println(strLine("_"));
- sleep(1500);
- loop = true;
- continue;
- }
- }
- }
- MainMenu();
- in.close();
- }
- public static void srchPrnts() {
- clear();
- println(strLine("="));
- println(strCenter(50, "Search", "||"));
- println(strLine("="));
- println(strLeft("Enter the integer search key you want", 2));
- println(strLeft("to find below inside the Array", 2));
- }
- public static void Search() {
- Scanner in = new Scanner(System.in);
- int tmpArray[] = arrCpy(IntArray);
- while (true) {
- srchPrnts();
- println(strLine("_"));
- print("\n Enter the integer: ");
- int input = (int) (in.nextInt());
- if (input == -99) {
- errormsg("Forbidden");
- continue;
- }
- int searchIn = srch(IntArray, input);
- if (searchIn == -1) {
- srchPrnts();
- println(strLine("_"));
- println(strLeft("Values inside the array:", 2));
- shwTbl(tmpArray, false, false);
- println(strLine("_"));
- println("\n");
- msg("Integer (" + input + ") was not found\nin the array!");
- sleep(1000);
- break;
- } else {
- srchPrnts();
- tmpArray[searchIn] = -99;
- println(strLine("_"));
- println(strLeft("Values inside the array:", 2));
- shwTbl(tmpArray, true, false);
- println(strLine("_"));
- println("\n");
- msg("Integer (" + input + ") was found at\nindex (" + Integer.toString(searchIn) + ") of the array!");
- sleep(2000);
- break;
- }
- }
- MainMenu();
- in.close();
- }
- public static void Display() {
- clear();
- println(strLine("="));
- println(strCenter(50, "Display", "||"));
- println(strLine("="));
- println(strLeft("Values inside the array:", 2));
- shwTbl(IntArray, false, true);
- println(strLine("_"));
- sleep(2500);
- MainMenu();
- }
- public static void prntDel(int[] tmpArray, boolean hide) {
- clear();
- println(strLine("="));
- println(strCenter(50, "Delete", "||"));
- println(strLine("="));
- println(strLeft("Values inside the array:", 2));
- shwTbl(tmpArray, true, hide);
- println(strLine("_"));
- println(strLeft("Insert the value(s) of the element(s)", 2));
- println(strLeft("you want to be deleted", 2));
- println(strLine("_"));
- }
- public static void shwTbl(int[] arry, boolean tmp, boolean hide) {
- int a = 0;
- String strFnl = "";
- String tmpS = "";
- while (a < IntArraySize) {
- if (tmp)
- if (IntArray[a] == -99)
- tmpS = "0";
- else
- tmpS = "[" + Integer.toString(IntArray[a]) + "]";
- else
- tmpS = Integer.toString(arry[a]);
- if (arry[a] == -99)
- if (hide)
- strFnl = strFnl + strLeft(9, "0", "", 1);
- else
- strFnl = strFnl + strLeft(9, tmpS, "", 1);
- else
- strFnl = strFnl + strLeft(9, Integer.toString(arry[a]), "", 1);
- if (a % 5 == 4) {
- println(strLeft(strFnl, 1));
- strFnl = "";
- }
- a++;
- }
- if (!strFnl.equals(""))
- println(strLeft(strFnl, 1));
- }
- public static void Delete() {
- int inInt = -1;
- int inCnt = -1;
- int tmpArray[] = arrCpy(IntArray);
- Scanner in = new Scanner(System.in);
- while (true) {
- prntDel(tmpArray, false);
- if (inInt == -1)
- print("\n Number of elements to be deleted: ");
- if (in.hasNextInt()) {
- if (inCnt == -1)
- inInt = (int) (in.nextInt());
- inCnt = 0;
- if (inInt > arrTmpCnt()) {
- errormsg("Only " + arrTmpCnt() + " integers are allowed");
- inInt = -1;
- inCnt = -1;
- continue;
- }
- while (inCnt < inInt) {
- clear();
- prntDel(tmpArray, false);
- if (inInt == 1)
- print("\n Insert the integer to be removed: ");
- else
- print("\n Insert the " + strOrdinal(inCnt + 1) + " integer to be removed: ");
- if (in.hasNextInt()) {
- int input = (int) (in.nextInt());
- int searchIn = srch(tmpArray, input);
- if (input == -99) {
- errormsg("Forbidden");
- continue;
- }else if (input < 1) {
- errormsg("Negative integers are prohibited");
- continue;
- }
- if (searchIn == -1) {
- errormsg("Integer (" + input + ") was not found in the array!");
- continue;
- } else {
- tmpArray[searchIn] = -99;
- inCnt++;
- while (inCnt == inInt) {
- prntDel(tmpArray, false);
- println("\n");
- msg("Do you really want to remove\nthe highlighted items?");
- println(strLeft("[y] Discard changes and leave", 2));
- println(strLeft("[n] Cancel", 2));
- println(strLine("_"));
- print("\n Please choose:");
- input = -1;
- String con = in.next().toLowerCase();
- if (con.equals("n")) {
- prntDel(IntArray, true);
- println("\n");
- msg("User cancelled the removal");
- arrTmpCnt();
- break;
- } else if (con.equals("y")) {
- IntArray = arrCpy(tmpArray);
- prntDel(tmpArray, true);
- println("\n");
- msg("Successfully removed");
- arrTmpCnt();
- break;
- } else {
- errormsg("Insert y and n only.");
- println(strLeft("Please try a again", 2));
- println(strLine("_"));
- sleep(1500);
- continue;
- }
- }
- continue;
- }
- } else {
- errormsg("Please enter numbers only");
- in.next();
- continue;
- }
- }
- } else {
- errormsg("Please enter numbers only");
- in.next();
- continue;
- }
- MainMenu();
- in.close();
- }
- }
- public static int srch(int[] lst, int num) {
- int len = lst.length;
- int tmpIndx = -1;
- for (int i = 0; i < len; i++)
- if (lst[i] == num) {
- tmpIndx = i;
- break;
- }
- return tmpIndx;
- }
- public static void msg(String msg) {
- String msgs[] = msg.split("\r\n|\n|\r");
- println(strLine(50, "=", "||"));
- for (int i = 0; i < msgs.length; i++) {
- println(strCenter(50, msgs[i], "||"));
- }
- println(strLine(50, "=", "||"));
- sleep(DelayMS);
- }
- public static void errormsg(String msg) {
- clear();
- println(strLine(50, "=", "||"));
- println(strCenter(50, "ERROR!!", "||"));
- println(strCenter(50, msg, "||"));
- println(strLine(50, "=", "||"));
- sleep(DelayMS);
- }
- public static void sleep(int msec) {
- try {
- Thread.sleep(msec);
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- }
- public static int[] arrCpy(int[] frm) {
- int[] newInt = new int[frm.length];
- for (int i = 0; i < frm.length; i++)
- newInt[i] = frm[i];
- return newInt;
- }
- public static int arrTmpCnt() {
- int cnt = 0;
- for (int i = 0; i < IntArray.length; i++)
- if (IntArray[i] == -99) {
- tmpIntBlnk[cnt] = i;
- cnt++;
- }
- tmpInArrSz = IntArraySize - cnt;
- return tmpInArrSz;
- }
- public static int arrCntZ() {
- int cnt = 0;
- for (int i = 0; i < IntArray.length; i++)
- if (IntArray[i] == 0) {
- cnt++;
- }
- return cnt;
- }
- public static String strOrdinal(int num) {
- int a = num % 100, b = num % 10;
- String ord = "";
- if (a != 11 && b == 1)
- ord = "st";
- else if (a != 12 && b == 2)
- ord = "nd";
- else if (a != 13 && b == 3)
- ord = "rd";
- else
- ord = "th";
- return Integer.toString(num) + ord;
- }
- public static String strLeft(int wdth, String str, String edg, int pdng) {
- double spcL = (double) (wdth - pdng - str.length() - edg.length() * 2);
- String spc2 = " ".repeat((int) (spcL));
- String spc1 = " ".repeat(pdng);
- String fnl = edg + spc1 + str + spc2 + edg;
- return fnl;
- }
- public static String strLeft(String str, int pdng) {
- double spcL = (double) (MaxW - pdng - str.length() - Brdr.length() * 2);
- String spc2 = " ".repeat((int) spcL);
- String spc1 = " ".repeat(pdng);
- String fnl = Brdr + spc1 + str + spc2 + Brdr;
- return fnl;
- }
- public static String strCenter(String str) {
- double spcL = (double) (MaxW - str.length() - Brdr.length() * 2);
- double spcD = (double) spcL / 2;
- String add = "";
- if (spcL % 2 != 0)
- add = " ";
- String spc = " ".repeat((int) Math.floor(spcD));
- String fnl = Brdr + spc + str + spc + add + Brdr;
- return fnl;
- }
- public static String strCenter(int wdth, String str, String edg) {
- double spcL = (double) (wdth - str.length() - edg.length() * 2);
- double spcD = (double) spcL / 2;
- String add = "";
- if (spcL % 2 != 0)
- add = " ";
- String spc = " ".repeat((int) Math.floor(spcD));
- String fnl = edg + spc + str + spc + add + edg;
- return fnl;
- }
- public static String strLine(int wdth, String str, String edg) {
- String ln = str.repeat(wdth - edg.length() * 2);
- return edg + ln + edg;
- }
- public static String strLine(String str) {
- String ln = str.repeat(MaxW - Brdr.length() * 2);
- return Brdr + ln + Brdr;
- }
- public static void println(String str) {
- System.out.println(str);
- }
- public static void print(String str) {
- System.out.print(str);
- }
- public static void clear() {
- // Clears Screen in java
- try {
- if (System.getProperty("os.name").contains("Windows"))
- new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
- else
- new ProcessBuilder("clear").inheritIO().start().waitFor();
- } catch (IOException | InterruptedException ex) {
- }
- }
- }
Add Comment
Please, Sign In to add comment