//In-class assignment
//IO reader/writer


import java.io.*;

public class IO_methods
{

	//Print Prompt method
	public void printPrompt(String prompt)
	{
		System.out.println(prompt);
	}

	//Read line method
	public String readLine()
	{
		int ch;
		String line = "";
		boolean done = false;
		while (!done)
		{
			try
			{
				ch = System.in.read();
				if (ch < 0 || (char)ch == '\n')		//check for invalid data
				{
					done = true;
				}
				else if ((char)ch != '\r')
				{
					line = line + (char)ch;
				}
			}
			catch (IOException e)
			{
				done = true;
			}
		}
		return line;
	}

	//overloaed Read line method - Takes a string
	public String readLine(String prompt)
		{
			printPrompt (prompt);
			int ch;
			String line = "";
			boolean done = false;
			while (!done)
			{
				try
				{
					ch = System.in.read();
					if (ch < 0 || (char)ch == '\n')
					{
						done = true;
					}
					else if ((char)ch != '\r')
					{
						line = line + (char)ch;
					}
				}
				catch (IOException e)
				{
					done = true;
				}
			}
			return line;
	}

	//Read int Method
	public int readInt (String prompt)
	{
		while (true)
		{
			printPrompt (prompt);
			try
			{
				return Integer.parseInt (readLine().trim());		//trim() is used to get rid of leading/traling spaces
			}
			catch (NumberFormatException e)
			{
				System.out.println ("Not an integer. Please try again!");
			}
		}
	}

	//Read double method
	public double readDouble (String prompt)
	{
		while (true)
		{
			printPrompt (prompt);
			try
			{
				return Double.parseDouble(readLine().trim());
			}
			catch (NumberFormatException e)
			{
				System.out.println ("Not a double type. Please try again!");
			}
		}
	}
}

