I wrote this demo just for practice purpose, I would like to get some helps that could further improve my code quality. Any suggestion for improvement would be appreciated.
This code is a simple demonstration of stack implementation (by using array List).
- User will be asked for the option of a function to demonstrate.
- Once user made the choice (either use stack algorithm or java Stack), the corresponding function will ask user for size of the stack and elements that user wants to put into the stack.
- Program will ask for the basic stack operation(push & pop).
- Program will receive user inputs(integer) and push into the stack then print current stack.
- After the print is completed, program will continue asking for new input, until user want to clear the stack and exit the function.
Note:
- If the stack is overflowed, user will be notified and program will add additional space to the stack(perk for using Linked list).
If stack is empty, user will be notified and program will break out of the pop option until user deiced to push in some elements.
import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class basicStack { public static void main(String[] args) { System.out.println("Welcome to basic Stack test, please choose your testing subject: "); try(Scanner user_option = new Scanner(System.in)){ mainMenu(user_option); }//close the I/O before the program end System.out.println("Program terminated..."); } private static void mainMenu(Scanner input){ int option = 0; do{ System.out.println("Option 1: Simple stack algorithm implementation"); System.out.println("Option 2: Simple java library stack usage"); System.out.println("Option 0: Exit program"); option = input.nextInt(); System.out.println("Current option is: " + option); switch(option){ case 1: System.out.println("running 1"); constructStackAlg(); break; case 2: System.out.println("running 2"); //constructJavaStack(); break; case 0: return; default: System.out.println("Invaild Option, Please choose a vaild option..."); break; } System.out.println("-Main Menu-"); }while(option != 0); } private static void constructStackAlg(){ int optionS1 = 1; Scanner sc_Stack1 = new Scanner(System.in); do{ int size = getS1Size(); ArrayList<Integer> s1 = buildStack(size); stackFunction(s1, size); //printS1(s1); System.out.println("Stack cleared..."); System.out.println("Do you wish to test another Stack? " + "Press any button to contiune or " + "enter 0 to exit Stack test."); optionS1 = sc_Stack1.nextInt(); }while(optionS1 != 0); System.out.println("\nEnd of testing 1, back to main menu..."); } private static int getS1Size(){ int s1Size = 0; Scanner scGetS1Size = new Scanner(System.in); try{ System.out.println("Please enter the size of this Stack:"); s1Size = scGetS1Size.nextInt(); }catch(InputMismatchException exception){ System.out.println("Please input a vaild integer."); } return s1Size; } private static ArrayList<Integer> buildStack(int s1Size){ ArrayList<Integer> resultStack; resultStack = new ArrayList<Integer>(s1Size); return resultStack; } private static void stackFunction(ArrayList<Integer> s1, int s1Size){ ArrayList<Integer> stack1 = s1; int stack1Size = s1Size; System.out.println("Please choose the stack option:"); Scanner scStackFunction = new Scanner(System.in); Scanner scElement = new Scanner(System.in); int option = 3; int element = 0; do{ System.out.println("1.Push \t 2. Pop 3. Clear stack and exit current test"); System.out.println("Your option: "); option = scStackFunction.nextInt(); switch(option){ case 1: System.out.println("Please input the integer you would like to store: "); element = scElement.nextInt(); checkOverflow(stack1,stack1Size); push(stack1, element); printS1(stack1); break; case 2: if(checkEmptyStack(stack1)){ System.out.println("Stack is empty."); break; } pop(stack1); printS1(stack1); break; case 3: return; default: System.out.println("Please enter a vaild option."); break; } }while(option != 3); } private static void push(ArrayList<Integer> stack1, int element1){ stack1.add(element1); } private static int pop(ArrayList<Integer> stack1){ int element; element = stack1.remove((stack1.size()-1)); return element; } private static void printS1(ArrayList<Integer> stack1){ System.out.println("Now Stack: " + stack1); } private static void checkOverflow(ArrayList<Integer> stack1, int s1Size) { if(stack1.size() > s1Size - 1) System.out.println("Stack overflow, adding additional space..."); } private static boolean checkEmptyStack(ArrayList<Integer> stack1) { return stack1.isEmpty(); } }