package oop_lab2;

import java.io.*;
import oop_lab2.Name;

public class Namelist
{
	private Name[] data;
	
	public Namelist()
	{
		this.data = new Name[0];
	}
	
	public void readFrom(InputStream in)
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		try
		{
			
			String line;
			while((line = reader.readLine()) != null)
			{
				if(line.contains(" "))
				{
					String splits[] = line.split(" ");
					switch(splits.length)
					{
					case 3:
						this.add(new Name(splits[0], splits[1], splits[2]));
						break;
					case 2:
						this.add(new Name(splits[0], splits[1]));
						break;
					default:
						System.out.println("Bad string for name " + line);
						break;
					}
				}
				else
				{
					System.out.println("Bad string for name " + line);
				}
			}
		}
		catch(IOException e)
		{
			//stream bad
			System.out.println("Bad stream");
			e.printStackTrace();
		}
		
		
		
	}
	
	public void printTo(PrintStream out)
	{
		for(Name e : this.data)
		{
			out.println(e);
		}
	}
	
	public Name get(int pos)
	{
		if(pos < data.length)
			return data[pos];
		else
			return null;	
	}
	
	public void add(Name e)
	{
		Name[] newdata = new Name[this.data.length + 1];
		System.arraycopy(data, 0, newdata, 0, this.data.length);
		data = newdata;
		newdata[newdata.length-1] = e;
	}
	
	public int length()
	{
		return this.data.length;
	}
	
}
