
import java.util.ArrayList;
import java.util.Scanner;

public class Paragraph
{
	private ArrayList<String> sentences;

	public Paragraph() 
	{
    		sentences = new ArrayList<String>();
  	}

	public void addSentence(String sentence) 
	{
    		sentences.add(sentence);
  	}

  	public void addTerminalInputSentence() 
	{
    		Scanner scanner = new Scanner(System.in);

    		System.out.print("Enter a sentence:  ");
    		String data = scanner.nextLine();
    		addSentence(data);
  	}

  	public String toString() 
	{
    		String data = "";

    		/* for (String sentence : sentences) {
      		*	data = data + sentence + "\n";
    		*  }
		*/

		// for-loop version
		for( int i = 0; i < sentences.size(); i++ ) {
			data += sentences.get(i) + "\n";
		}

		// while-loop version
		/* int i = 0;
		*  while( i < sentences.size() ) {
		*	data += sentences.get(i) + "\n";
		*	i++;
		*  }
		*/

    		return data;
  	}

	public void insert( String sentence, int index )
	{
		if( index >= 0 && index < sentences.size() )
			sentences.add( index, sentence );
	}

	public String remove( int index )
	{
		if( index >= 0 && index < sentences.size() )
			return sentences.remove(index);
		return null;
	}

	public String remove( String sentence )
	{
		for( int i = 0; i < sentences.size(); i++ ) {
			if( sentence.equals( sentences.get(i) ) )
				return sentences.remove(i);
		}

		return null;
	}

	public void replace( String sentence, int index )
	{
		if( index >= 0 && index < sentences.size() )
			sentences.set( index, sentence );
	}

	public void shortestSentence()
	{
		int sIndex = 0;			// index of the shortest sentence

		if( sentences.size() == 0 )	// if there's no sentences in the paragraph,
			return;			// end the method.

		for( int i = 1; i < sentences.size(); i++ ) {
			if( sentences.get(sIndex).length() > sentences.get(i).length() ) {
				sIndex = i;
			}
		}

		// print out the search result
		System.out.println("Index = " + sIndex );
		System.out.println("Shortest sentence = " + sentences.get(sIndex) );
	}

	public void menu()
	{
		Scanner scanner = new Scanner(System.in);

		while (true)
		{
			System.out.println("\nMenu Options\n");
			System.out.println("(1) add a sentence");
			System.out.println("(2) insert a sentence at a specified index");
			System.out.println("(3) remove a sentence from a specified index");
			System.out.println("(4) remove a sentence that matches the input");
			System.out.println("(5) replace a sentence at a specified index");
			System.out.println("(6) print the shortest sentence");
			System.out.println("(7) print info");
			System.out.println("(8) quit");

			System.out.print("\nYour Seclection: ");
			int choice = scanner.nextInt();

			if( choice == 1 ) {
				addTerminalInputSentence();
			}
			else if( choice == 2 ) {
				System.out.println("Insert");
				System.out.print("Please enter a string: ");
				String input = scanner.next();
				System.out.print("Please enter an index: ");
				int index = scanner.nextInt();

				insert( input, index );
			}
			else if( choice == 3 ) {
				System.out.println("Remove");
				System.out.print("Please enter an index: ");
				int index = scanner.nextInt();

				remove( index );
			}
			else if( choice == 4 ) {
				System.out.println("Remove matching string");
				System.out.print("Please enter a string: ");
				String input = scanner.next();

				remove( input );
			}
			else if( choice == 5 ) {
				System.out.println("Replace");
				System.out.print("Please enter a string: ");
				String input = scanner.next();
				System.out.print("Please enter an index: ");
				int index = scanner.nextInt();

				replace( input, index );
			}
			else if( choice == 6 ) {
				shortestSentence();
			}
			else if( choice == 7 ) {
				printInfo();
			}
			else if( choice == 8 ) {
				break;
			}
			else
				System.out.println("Invalid choice");
		}
	}

  	public int numberOfSentences() 
	{
    		return sentences.size();
  	}

	public void printInfo()
	{
		System.out.println( toString() );
	}
}


