Advertisement
AbhijitPaul

ArrayMenu.java

Feb 1st, 2023
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1.  
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. public class ArrayMenu implements MenuInterface {
  6.     private String title;
  7.     private String[] options;
  8.     private int maxSize;
  9.     private int currentSize;
  10.  
  11.     public ArrayMenu(String title, String[] options, int maxSize) {
  12.         if (options == null) {
  13.             throw new NullPointerException("Options array cannot be null");
  14.         }
  15.         if (maxSize < options.length) {
  16.             throw new IllegalArgumentException("Maximum size cannot be less than number of starting options");
  17.     }
  18.    
  19.         this.title = title;
  20.         this.options = options;
  21.         this.maxSize = maxSize;
  22.         this.currentSize = 0; //options.length
  23.     }
  24.  
  25.     public ArrayMenu(String title, String[] options) {
  26.         this(title, options, options.length);
  27.     }
  28.  
  29.     public ArrayMenu(String title) {
  30.         this(title, new String[1], 1);
  31.     }
  32.  
  33.     public ArrayMenu(String[] options) {
  34.         this(null, options, options.length);
  35.     }
  36.  
  37.     public ArrayMenu(String title, int maxSize) {
  38.         this(title, new String[Math.max(0,maxSize)], maxSize);
  39.     if(maxSize<0)throw new IllegalArgumentException("Max Size must be a positive number.");
  40.     }
  41.  
  42.     public ArrayMenu(int maxSize) {
  43.         this(null, new String[Math.max(maxSize,0)], maxSize);
  44.     if(maxSize<0)throw new IllegalArgumentException("Max Size must be a positive number.");
  45.     }
  46.  
  47.     public ArrayMenu() {
  48.         this(null, new String[1], 1);
  49.     }
  50.       public String getTitle(){
  51.         return title;
  52.     }
  53.  
  54.     public boolean addOption(String option) {
  55.     //if(options==null) options = new String[1];
  56.         if (currentSize == maxSize) {
  57.             System.out.println("Cannot add more options. Maximum size reached.");
  58.             return false;
  59.         }
  60.         options[currentSize++] = option;
  61.         return true;
  62.     }
  63.      public String[] getOptions(){
  64.        return options;
  65.     }
  66.     public boolean setTitle(String menuTitle){
  67.         if(this.title != null){
  68.             this.title = menuTitle;
  69.             return true;
  70.         }
  71.         this.title = menuTitle;
  72.         return false;
  73.     }
  74.     public boolean addOptions(String[] options){
  75.         if(this.currentSize >= this.maxSize){
  76.         System.out.println("Debug: Severe error here!");
  77.             return false;
  78.         }
  79.         for(int i=0;i<options.length;i++){
  80.             if(this.currentSize >= this.maxSize){
  81.                 break;
  82.             }
  83.             this.options[this.currentSize] = options[i];
  84.             this.currentSize++;
  85.         }
  86.         return true;    
  87.     }
  88.    
  89.    
  90.     public void removeOption(String option) {
  91.         for (int i = 0; i < currentSize; i++) {
  92.             if (options[i].equals(option)) {
  93.                 for (int j = i; j < currentSize - 1; j++) {
  94.                     options[j] = options[j + 1];
  95.                 }
  96.                 currentSize--;
  97.                 break;
  98.             }
  99.         }
  100.     }
  101.  
  102.     public int getChoice(Scanner in) throws IllegalStateException {
  103.     if (currentSize == 0) {
  104.         throw new IllegalStateException("This Menu has no options.");
  105.     }
  106.  
  107.     int choice = -1;
  108.     int tries = 0;
  109.     while (tries < 3) {
  110.         System.out.println("Enter your choice: ");
  111.         try {
  112.             choice = in.nextInt();
  113.         break;
  114.         } catch (InputMismatchException e) {
  115.             System.out.println("Invalid input. Please try again.");
  116.             in.nextLine();
  117.         }
  118.         tries++;
  119.     }
  120.     if(tries<3)return choice;
  121.     else return -1;
  122. }
  123.  
  124.  
  125.     public void display() {
  126.         if (title != null) {
  127.             System.out.println(title);
  128.         }
  129.         for (int i = 0; i < currentSize; i++) {
  130.             System.out.println((i + 1) + ". " + options[i]);
  131.         }
  132.     }
  133.  
  134.    
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement