Cod sursa(job #1361595)

Utilizator DD1994Mincu Dragos DD1994 Data 25 februarie 2015 22:23:35
Problema Convertor Scor 60
Compilator java Status done
Runda rosedu_cdl_2015 Marime 3.29 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class CSVFormat {
	public class ValuesFormat {
		protected List<String> valueFields = new ArrayList<String>();
		
		public void addField(String field) {
			valueFields.add(field);
		}
		
		public List<String> getValues() {
			return valueFields;
		}
	}
	
	protected List<String> keyFields = new ArrayList<String>();
	protected List<ValuesFormat> objects = new ArrayList<ValuesFormat>();
	
	public void addNewObject() {
		objects.add(new ValuesFormat());
	}
	
	public void addNewValue(int objectIndex, String value) {
		objects.get(objectIndex).addField(value);
	}
	
	public List<String> getValues(int index) {
		return objects.get(index).getValues();
	}
	
	public int getNumberOfValues() {
		return objects.size();
	}
	
	public void addNewKey(String field) {
		keyFields.add(field);
	}
	
	public List<String> getKeys() {
		return keyFields;
	}
}

public class Main {
	public static void main(String[] args) {
		
		Scanner reader = null;
		CSVFormat csvFormat = new CSVFormat();
		String line;
		int currentCharIndex;
		int objectIndex;
		int startIndex, finalIndex; 
		
		try {
			reader = new Scanner(new FileInputStream("convertor.in"));
			objectIndex = -1;
			while (reader.hasNextLine()) {
				line = reader.nextLine();
				currentCharIndex = 0;
				while (currentCharIndex < line.length()) {
					if (line.charAt(currentCharIndex) == '{') {
						objectIndex++;
						csvFormat.addNewObject();
					} else if (line.charAt(currentCharIndex) == ':') {
						if (objectIndex == 0) {
							startIndex = currentCharIndex - 1;
							while (line.charAt(startIndex) == ' ') {
								startIndex--;
							}
							finalIndex = startIndex;
							startIndex--;
							while (line.charAt(startIndex) != '"') {
								startIndex--;
							}
							startIndex++;
							csvFormat.addNewKey(line.substring(startIndex, finalIndex));
						}
						finalIndex = currentCharIndex + 1;
						while (line.charAt(finalIndex) == ' ' || line.charAt(finalIndex) == '"') {
							finalIndex++;
						}
						startIndex = finalIndex;
						while (line.charAt(finalIndex) != ',' && line.charAt(finalIndex) != '}' && line.charAt(finalIndex) != '"') {
							finalIndex++;
						}
						csvFormat.addNewValue(objectIndex, line.substring(startIndex, finalIndex));
						currentCharIndex = finalIndex;
					}
					currentCharIndex++;
				}
				
			}
		} catch (FileNotFoundException e) {
			System.out.println("Input file not found!");
		} finally {
			reader.close();
		}
		
		PrintWriter writer = null;
		List<String> values;
		List<String> keys;
		
		try {
			writer = new PrintWriter("convertor.out");
			keys = csvFormat.getKeys();
			for (String key : keys) {
				writer.print(key + ",");
			}
			writer.println();
			for (objectIndex = 0; objectIndex < csvFormat.getNumberOfValues(); objectIndex++) {
				values = csvFormat.getValues(objectIndex);
				for (String value : values) {
					writer.print(value + ",");
				}
				writer.println();
			}
			
		} catch (FileNotFoundException e) {
			System.out.println("Output file not found!");
		} finally {
			writer.close();
		}
	}
}