/**********************
* Steven Giovneco     *
* 7/22/2003           *
* Fun With Magnets    *
* String Manipulation *
**********************/

import java.io.*;

class magnets
{

	public static int promptInt(String thePrompt) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		//output prompt
		System.out.print(thePrompt);
		//get and format input
		return Integer.parseInt(in.readLine());
	}

	public static String promptString(String thePrompt) throws IOException
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		//output prompt
		System.out.print(thePrompt);
		//get input
		return in.readLine();
	}

	//prints a menu to the screen and returns the user's choice
	public static int menu() throws IOException
	{
		//declare an input stream reader
		BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
		
		//print the menu
		System.out.println("Welcome to the Magnet Program!");
		System.out.println("Here are your choices:");
                System.out.println("1) Concatenate a string to the beginning of the current string.");
                System.out.println("2) Concatenate a string to the end of the current string.");
                System.out.println("3) Turn the string into all uppercase characters.");
                System.out.println("4) Turn the string into all lowercase characters.");
                System.out.println("5) Replace all occurences of one character with another character");
                System.out.println("6) Print out final string and Quit.");
		
		//get an int choice
		return promptInt("Your choice: ");
	}

	public static String replaceChars(String toReplaceIn) throws IOException
	{
		//the the char to replace
		char toReplace = promptString("Enter the character you wish replaced: ").charAt(0);
		
		//get the char to replace it with
		char replaceWith = promptString("Enter the character you would like "+ toReplace + " replaced with: ").charAt(0);
		
		//replace that character and return the reference to it
		return toReplaceIn.replace(toReplace, replaceWith);
	}

	public static void main(String args[]) throws IOException
	{
		//declare an input stream reader
		BufferedReader in = new BufferedReader(new InputStreamReader (System.in));

		//menu choice
		int choice;

		//current string
		String currString = "";

		//repeat until the user quits
		do
		{
			//get the choice from the menu
			choice = menu();
			
			//do the action based on the choices
			if (choice == 1)
				currString = promptString("Which string would you like to concatenate to the beginning?\n") + currString;
			else if (choice == 2)
				currString += promptString("Which string would you like to concatenate to the end?\n");
			else if (choice == 3)
				currString = currString.toUpperCase();
			else if (choice == 4)
				currString = currString.toLowerCase();
			else if (choice == 5)
				currString = replaceChars(currString);
			else if (choice == 6)
				System.out.println("Thank you for using the magnet program.");
			else
				System.out.println("Invalid choice.  Please try again.");
			
			//output a blank line
			System.out.println();
			
			//if the choice was valid and not quit, output the current string
			if (choice >= 1 && choice <= 5) 
				System.out.println("Your current string is: " + currString + '\n');
		}
		while (choice != 6); //while the user didn't pick quit
	}
}
