Created
March 6, 2018 15:46
-
-
Save rashemihmih/eceaa0ce63d849f4fa5db3115a1873f6 to your computer and use it in GitHub Desktop.
FORECSYS test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Country { | |
private String name; | |
private int sum; | |
private HashSet<Integer> users = new HashSet<>(); | |
public Country(Record record) { | |
name = record.getCountry(); | |
sum = record.getCount(); | |
users.add(record.getId()); | |
} | |
public String getName() { | |
return name; | |
} | |
public int getSum() { | |
return sum; | |
} | |
public int getUniqueCount() { | |
return users.size(); | |
} | |
public void add(Record record) { | |
if (sum > Integer.MAX_VALUE - record.getCount()) { | |
throw new ArithmeticException("Integer overflow"); | |
} | |
sum += record.getCount(); | |
users.add(record.getId()); | |
} | |
@Override | |
public int hashCode() { | |
return name.hashCode(); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return Country.class == obj.getClass() && name.equals(((Country) obj).name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CountryRecord { | |
private String name; | |
private int sum; | |
private int unique; | |
public CountryRecord(String name, int sum, int unique) { | |
this.name = name; | |
this.sum = sum; | |
this.unique = unique; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getSum() { | |
return sum; | |
} | |
public int getUnique() { | |
return unique; | |
} | |
public static CountryRecord parse(String source) { | |
String[] split = source.split("\\s"); | |
if (split.length != 3) { | |
System.out.println("Неверный формат строки"); | |
return null; | |
} | |
int sum; | |
try { | |
sum = Integer.parseInt(split[1]); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат числа " + e.getMessage()); | |
return null; | |
} | |
int unique; | |
try { | |
unique = Integer.parseInt(split[2]); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат числа " + e.getMessage()); | |
return null; | |
} | |
return new CountryRecord(split[0], sum, unique); | |
} | |
@Override | |
public String toString() { | |
return name + " " + sum + " " + unique; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CountrySet extends HashSet<Country> { | |
public void add(Record record) { | |
Country country = new Country(record); | |
Country stored = get(country); | |
if (stored == null) { | |
add(country); | |
return; | |
} | |
stored.add(record); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class HashSet<T> { | |
protected static final int INITIAL_CAPACITY = 10; | |
protected static final double LOAD_FACTOR = 0.75; | |
@SuppressWarnings("unchecked") | |
protected Node<T>[] set = (Node<T>[]) new Node[INITIAL_CAPACITY]; | |
protected int size = 0; | |
public void add(T value) { | |
if (get(value) != null) { | |
return; | |
} | |
size++; | |
if (size > set.length * LOAD_FACTOR) { | |
rehash(); | |
} | |
addNew(value); | |
} | |
public T[] values(Class<T[]> clazz) { | |
//noinspection unchecked | |
Object[] values = new Object[size]; | |
int i = 0; | |
for (Node<T> node : set) { | |
while (node != null) { | |
values[i++] = node.value; | |
node = node.next; | |
} | |
} | |
return Arrays.copyOf(values, values.length, clazz); | |
} | |
protected int size() { | |
return size; | |
} | |
protected int hash(T value) { | |
return Math.abs(value.hashCode()) % set.length; | |
} | |
protected void rehash() { | |
int newCapacity = set.length * 2; | |
Node<T>[] oldSet = set; | |
//noinspection unchecked | |
set = (Node<T>[]) new Node[newCapacity]; | |
for (Node<T> node : oldSet) { | |
while (node != null) { | |
addNew(node.value); | |
node = node.next; | |
} | |
} | |
} | |
protected void addNew(T value) { | |
int pos = hash(value); | |
if (set[pos] == null) { | |
set[pos] = new Node<>(value); | |
return; | |
} | |
Node<T> node = set[hash(value)]; | |
while (node.next != null) { | |
node = node.next; | |
} | |
node.next = new Node<>(value); | |
} | |
protected T get(T value) { | |
Node<T> node = set[hash(value)]; | |
while (node != null) { | |
if (node.value.equals(value)) { | |
return node.value; | |
} | |
node = node.next; | |
} | |
return null; | |
} | |
protected class Node<K> { | |
K value; | |
Node<K> next; | |
public Node(K value) { | |
this.value = value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.stream.Collectors; | |
public class Main { | |
public static final String RESULT = "result.txt"; | |
public static void main(String[] args) { | |
if (args.length != 1) { | |
System.out.println("Имя файла не указано"); | |
return; | |
} | |
String in = args[0]; | |
List<String> lines; | |
try { | |
lines = Files.readAllLines(Paths.get(in)); | |
} catch (IOException e) { | |
System.out.println("Не удалось прочитать файл " + e.getMessage()); | |
return; | |
} | |
CountrySet countrySet = new CountrySet(); | |
try { | |
lines.stream().map(Record::parse).filter(Objects::nonNull).forEach(countrySet::add); | |
} catch (ArithmeticException e) { | |
System.out.println(e.getMessage()); | |
} | |
Country[] countries = countrySet.values(Country[].class); | |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(RESULT))) { | |
for (Country country : countries) { | |
writer.write(country.getName() + " " + country.getSum() + " " + country.getUniqueCount() + "\n"); | |
} | |
} catch (IOException e) { | |
System.out.println("Не удалось записать файл " + e.getMessage()); | |
return; | |
} | |
sortFile(); | |
} | |
private static void sortFile() { | |
List<String> lines; | |
try { | |
lines = Files.readAllLines(Paths.get(RESULT)); | |
} catch (IOException e) { | |
System.out.println("Не удалось прочитать файл " + e.getMessage()); | |
return; | |
} | |
String result = lines.stream() | |
.map(CountryRecord::parse) | |
.filter(Objects::nonNull) | |
.sorted((o1, o2) -> { | |
if (o1.getSum() != o2.getSum()) { | |
return o2.getSum() - o1.getSum(); | |
} | |
return o2.getUnique() - o1.getUnique(); | |
}) | |
.map(CountryRecord::toString) | |
.collect(Collectors.joining("\n")); | |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(RESULT, true))) { | |
writer.newLine(); | |
writer.write(result); | |
} catch (IOException e) { | |
System.out.println("Не удалось записать файл " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Record { | |
private int id; | |
private int count; | |
private String country; | |
public Record(int id, int count, String country) { | |
this.id = id; | |
this.count = count; | |
this.country = country; | |
} | |
public int getId() { | |
return id; | |
} | |
public int getCount() { | |
return count; | |
} | |
public String getCountry() { | |
return country; | |
} | |
public static Record parse(String source) { | |
String[] split = source.split(";"); | |
if (split.length != 3) { | |
System.out.println("Неверный формат строки"); | |
return null; | |
} | |
int id; | |
try { | |
id = Integer.parseInt(split[0]); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат числа " + e.getMessage()); | |
return null; | |
} | |
int count; | |
try { | |
count = Integer.parseInt(split[1]); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат числа " + e.getMessage()); | |
return null; | |
} | |
return new Record(id, count, split[2]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment