package dados;

import cap5.*;
import java.io.*;

public class DataArea
extends AbstractObject
implements Serializable
{
    private String numero;
    private String nome;
    private double salario;
    private String transacao;
    
    final private int TAMANHO_INT = 2;
    final private int TAMANHO_NOME = 10;
    final private int TAMANHO_SALARIO = 7;
    
    public DataArea()
    {
    	this.numero 	= "";
    	this.nome 		= "";
    	this.salario 	= 0f;
    }
    
    public DataArea( String numero, String nome, double salario )
    {
    	if ( numero.length() < TAMANHO_INT )
    	{	numero = "0"+numero;
    	}
    	
    	if ( nome.length() > TAMANHO_NOME )
    	{	nome = nome.substring( 0, TAMANHO_NOME );
    	}
    	
    	while ( nome.length() < TAMANHO_NOME )
    	{	
    		nome += "_";
    	}
    	
    	this.numero 	= numero;
    	this.nome 		= nome;
    	this.salario 	= salario;
    }
    
    public DataArea( BufferedReader br )
    throws IOException
    {
    	int 	i = 0;
    	String 	s, line = br.readLine();
 	
 		if ( line == null )
 		{	
 			transacao = "err";
 			return;
 		}
 		   		
    	transacao = line.substring(i,1);
		i = 2;
			
		if ( transacao.compareToIgnoreCase( "i" ) == 0 )
		{
			numero = line.substring(i,i+TAMANHO_INT);
			i += TAMANHO_INT+1;
		    	    	    
	    	nome = line.substring(i,i+TAMANHO_NOME);
			i += TAMANHO_NOME+2;
	
	    	s = line.substring(i,line.length());   	        
	        salario = Double.parseDouble( s );
	      }
    }
    
    public String getNumero()
    {
    	return numero;
    }
    
    public String getNome()
    {
    	return nome;
    }
    
    public double getSalario()
    {
    	return salario;
    }
    
	public String getTransacao()
    {
    	return transacao;
    }

    public void setNumero( String numero )
    {
    	this.numero = numero;
    }
    
    public void setNome( String nome )
    {
    	this.nome = nome;
    }
    
    public void setSalario( double salario )
    {
    	this.salario = salario;
    }
    
    public void imprime()
    {
    	System.out.println ("Numero = " + numero + "  Nome = " + nome + "  Salario = " + salario);
    }
    
    public void gravaArquivo( BufferedWriter bw )
    throws IOException
    {
       	bw.write("i " + numero + " " + nome + "  " + salario);
    	bw.newLine();
    }
    
    protected int compareTo (cap5.Comparable object)
    {
		DataArea arg = (DataArea) object;
		return numero.compareToIgnoreCase( arg.numero );
    }
}

