Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class ArrayMenu implements MenuInterface {
- private String title;
- private String[] options;
- private int maxSize;
- private int currentSize;
- public ArrayMenu(String title, String[] options, int maxSize) {
- if (options == null) {
- throw new NullPointerException("Options array cannot be null");
- }
- if (maxSize < options.length) {
- throw new IllegalArgumentException("Maximum size cannot be less than number of starting options");
- }
- this.title = title;
- this.options = options;
- this.maxSize = maxSize;
- this.currentSize = 0; //options.length
- }
- public ArrayMenu(String title, String[] options) {
- this(title, options, options.length);
- }
- public ArrayMenu(String title) {
- this(title, new String[1], 1);
- }
- public ArrayMenu(String[] options) {
- this(null, options, options.length);
- }
- public ArrayMenu(String title, int maxSize) {
- this(title, new String[Math.max(0,maxSize)], maxSize);
- if(maxSize<0)throw new IllegalArgumentException("Max Size must be a positive number.");
- }
- public ArrayMenu(int maxSize) {
- this(null, new String[Math.max(maxSize,0)], maxSize);
- if(maxSize<0)throw new IllegalArgumentException("Max Size must be a positive number.");
- }
- public ArrayMenu() {
- this(null, new String[1], 1);
- }
- public String getTitle(){
- return title;
- }
- public boolean addOption(String option) {
- //if(options==null) options = new String[1];
- if (currentSize == maxSize) {
- System.out.println("Cannot add more options. Maximum size reached.");
- return false;
- }
- options[currentSize++] = option;
- return true;
- }
- public String[] getOptions(){
- return options;
- }
- public boolean setTitle(String menuTitle){
- if(this.title != null){
- this.title = menuTitle;
- return true;
- }
- this.title = menuTitle;
- return false;
- }
- public boolean addOptions(String[] options){
- if(this.currentSize >= this.maxSize){
- System.out.println("Debug: Severe error here!");
- return false;
- }
- for(int i=0;i<options.length;i++){
- if(this.currentSize >= this.maxSize){
- break;
- }
- this.options[this.currentSize] = options[i];
- this.currentSize++;
- }
- return true;
- }
- public void removeOption(String option) {
- for (int i = 0; i < currentSize; i++) {
- if (options[i].equals(option)) {
- for (int j = i; j < currentSize - 1; j++) {
- options[j] = options[j + 1];
- }
- currentSize--;
- break;
- }
- }
- }
- public int getChoice(Scanner in) throws IllegalStateException {
- if (currentSize == 0) {
- throw new IllegalStateException("This Menu has no options.");
- }
- int choice = -1;
- int tries = 0;
- while (tries < 3) {
- System.out.println("Enter your choice: ");
- try {
- choice = in.nextInt();
- break;
- } catch (InputMismatchException e) {
- System.out.println("Invalid input. Please try again.");
- in.nextLine();
- }
- tries++;
- }
- if(tries<3)return choice;
- else return -1;
- }
- public void display() {
- if (title != null) {
- System.out.println(title);
- }
- for (int i = 0; i < currentSize; i++) {
- System.out.println((i + 1) + ". " + options[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement