Skip to content

Instantly share code, notes, and snippets.

@boyanov83
Created September 21, 2014 19:39
Show Gist options
  • Save boyanov83/e900482f3551367185ef to your computer and use it in GitHub Desktop.
Save boyanov83/e900482f3551367185ef to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class OfficeStuff
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int linesNum = Integer.parseInt(scan.nextLine());
ArrayList<String> input = new ArrayList<String>();
String[] line = new String[3];
String currentLine = "";
String formattedLine = "";
for (int i = 0; i < linesNum; i++)
{
currentLine = scan.nextLine();
for (char item : currentLine.toCharArray())
{
if (item != '|' && item != ' ')
{
formattedLine += item;
}
}
line = formattedLine.split("-");
formattedLine = "";
input.add(line[0]);
input.add(line[1]);
input.add(line[2]);
}
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> products = new ArrayList<String>();
for (int i = 0; i < input.size(); i+=3)
{
names.add(input.get(i));
}
names = removeDuplicates(names);
Collections.sort(names);
int totalAmount = 0;
for (int i = 0; i < names.size(); i++) // Cycles once for each company
{
System.out.print(names.get(i) + ": ");
products.clear();
for (int j = 0; j < input.size(); j+=3) // get all different products for each company
{
if (input.get(j).equals(names.get(i)))
{
products.add(input.get(j+2));
}
}
removeDuplicates(products);
for (int k = 0; k < products.size(); k++) // find and print results
{
for (int j = 0; j < input.size(); j+=3)
{
if (names.get(i).equals(input.get(j)) && products.get(k).equals(input.get(j+2)))
{
totalAmount += Integer.parseInt(input.get(j+1));
}
}
if (k > 0 && totalAmount > 0)
{
System.out.print(", ");
}
if (totalAmount > 0)
{
System.out.print(products.get(k) + "-" + totalAmount);
totalAmount = 0;
}
}
if (i < names.size()-1)
{
System.out.println();
}
}
}
private static ArrayList<String> removeDuplicates(ArrayList<String> list)
{
for (int i = 0; i < list.size(); i++)
{
for (int j = i+1; j < list.size(); j++)
{
if (list.get(i).equals(list.get(j)))
{
list.remove(j);
j--;
}
}
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment