Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class Nuts {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int number = Integer.parseInt(sc.nextLine());
- ArrayList<String> inputArrayList = new ArrayList<String>();
- for (int i = 0; i < number; i++) {
- inputArrayList.add(sc.nextLine());
- }
- LinkedHashMap<String, String> products = new LinkedHashMap<String, String>();
- Map<String, LinkedHashMap<String, String>> companies = new TreeMap();
- for (int i = 0; i < inputArrayList.size(); i++) {
- String[] splittedLine = inputArrayList.get(i).split("\\s+");
- if (!companies.containsKey(splittedLine[0])) {
- companies.put(splittedLine[0],
- new LinkedHashMap<String, String>());
- companies.get(splittedLine[0]).put(splittedLine[1],
- splittedLine[2]);
- } else {
- if (!companies.get(splittedLine[0])
- .containsKey(splittedLine[1])) {
- companies.get(splittedLine[0]).put(splittedLine[1],
- splittedLine[2]);
- } else {
- String currentValue = companies.get(splittedLine[0]).get(
- splittedLine[1]);
- int currentValueAsInt = extractIntegerFromString(currentValue);
- int nextValueAsInt = extractIntegerFromString(splittedLine[2]);
- int sumOfCurrentAndNextValue = currentValueAsInt + nextValueAsInt;
- String toPutString = concatenateSymbols(sumOfCurrentAndNextValue, "kg");
- companies.get(splittedLine[0]).put(splittedLine[1], toPutString);
- }
- }
- }
- for (String string : companies.keySet()) {
- System.out.print(string + ": ");
- printProducts(companies.get(string));
- System.out.println();
- }
- }
- private static String extractCompanyFromInput(String companyWithProducts) {
- String[] companyWithProductsToArray = companyWithProducts.split(" ");
- String company = companyWithProductsToArray[0];
- return company;
- }
- private static String extractProductFromInput(String companyWithProducts) {
- String[] companyWithProductsToArray = companyWithProducts.split(" ");
- String product = companyWithProductsToArray[1];
- return product;
- }
- private static String extractQuantityFromInput(String companyWithProducts) {
- String[] companyWithProductsToArray = companyWithProducts.split(" ");
- String quantity = companyWithProductsToArray[2];
- return quantity;
- }
- private static void printProducts(LinkedHashMap<String, String> products) {
- String productsToString = products.toString();
- productsToString = productsToString.replaceAll("[\\{\\}]+", "");
- productsToString = productsToString.replaceAll("=", "-");
- System.out.print(productsToString);
- }
- public static LinkedHashMap<String, String> getNutsQuantity(String nut,
- String quantity, LinkedHashMap<String, String> products) {
- if (products.containsKey(nut)) {
- String oldQuantity = products.get(nut);
- int oldQuantityInt = extractIntegerFromString(oldQuantity);
- int quantityInt = extractIntegerFromString(quantity);
- int newQuantityInt = oldQuantityInt + quantityInt;
- String newQuantityString = concatenateSymbols(newQuantityInt, "kg");
- products.put(nut, newQuantityString);
- } else {
- products.put(nut, quantity);
- }
- return products;
- }
- public static int extractIntegerFromString(String stringWithInteger) {
- String theIntString = stringWithInteger.substring(0,
- stringWithInteger.length() - 2);
- int theInt = Integer.parseInt(theIntString);
- return theInt;
- }
- public static String concatenateSymbols(int theInt, String theString) {
- String result = "";
- result = Integer.toString(theInt) + theString;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment