import java.io.File;
import java.util.Scanner;
import java.util.Formatter;
import java.io.FileNotFoundException;

public class ArquivoTexto
{
	public void insereDados()
	{
		Formatter out = null;
		try
		{
			out = new Formatter( new File("circulo.txt") );
		}
		catch( FileNotFoundException e )
		{
			System.out.println("Erro ao criar o arquivo!");
			System.exit(0);
		}

		double x, y, r;
		Scanner scan = new Scanner(System.in);

		do
		{
			System.out.println("Digite as informacoes do Circulo: x y raio (raio negativo para terminar)");
			x = scan.nextDouble();
			y = scan.nextDouble();
			r = scan.nextDouble();

			if ( r > 0 )
			{
				out.format("%f %f %f\n", x, y, r);
			}

		}while(r > 0);

		out.close();
	}

	public void recuperaDados()
	{
		Scanner scan = null;
		try
		{
			scan = new Scanner( new File("circulo.txt") );
		}
		catch( FileNotFoundException e )
		{
			System.out.println("Erro ao abrir o arquivo!");
			System.exit(0);
		}

		double x, y, r;

		System.out.println("Dados dos circulos armazenados");
		while( scan.hasNext() )
		{
			x = scan.nextDouble();
			y = scan.nextDouble();
			r = scan.nextDouble();

			System.out.printf("%f %f %f\n", x, y, r);
		}

		scan.close();
	}

	public static void main(String [] args)
	{
		int op;
		ArquivoTexto t = new ArquivoTexto();

		do
		{
			System.out.println("----- MENU -----");
			System.out.println("1) Inserir Circulos no arquivo");
			System.out.println("2) Recuperar Circulos do arquivo");
			System.out.println("3) Sair\n");
			op = new Scanner(System.in).nextInt();

			if ( op == 1 ) t.insereDados();
			else if ( op == 2 ) t.recuperaDados();
			else if ( op == 3 ) System.out.println("Finalizando aplicacao...");
			else System.out.println("Opcao invalida!");

		}while(op != 3 );
	}
}
