Guest User

Untitled

a guest
Feb 11th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class Nuts {
  8.  
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. int number = Integer.parseInt(sc.nextLine());
  12.  
  13. ArrayList<String> inputArrayList = new ArrayList<String>();
  14. for (int i = 0; i < number; i++) {
  15. inputArrayList.add(sc.nextLine());
  16. }
  17.  
  18. LinkedHashMap<String, String> products = new LinkedHashMap<String, String>();
  19. Map<String, LinkedHashMap<String, String>> companies = new TreeMap();
  20.  
  21. for (int i = 0; i < inputArrayList.size(); i++) {
  22.  
  23. String[] splittedLine = inputArrayList.get(i).split("\\s+");
  24. if (!companies.containsKey(splittedLine[0])) {
  25. companies.put(splittedLine[0],
  26. new LinkedHashMap<String, String>());
  27. companies.get(splittedLine[0]).put(splittedLine[1],
  28. splittedLine[2]);
  29. } else {
  30. if (!companies.get(splittedLine[0])
  31. .containsKey(splittedLine[1])) {
  32. companies.get(splittedLine[0]).put(splittedLine[1],
  33. splittedLine[2]);
  34. } else {
  35. String currentValue = companies.get(splittedLine[0]).get(
  36. splittedLine[1]);
  37. int currentValueAsInt = extractIntegerFromString(currentValue);
  38. int nextValueAsInt = extractIntegerFromString(splittedLine[2]);
  39. int sumOfCurrentAndNextValue = currentValueAsInt + nextValueAsInt;
  40. String toPutString = concatenateSymbols(sumOfCurrentAndNextValue, "kg");
  41. companies.get(splittedLine[0]).put(splittedLine[1], toPutString);
  42.  
  43. }
  44. }
  45. }
  46.  
  47. for (String string : companies.keySet()) {
  48. System.out.print(string + ": ");
  49. printProducts(companies.get(string));
  50. System.out.println();
  51. }
  52.  
  53. }
  54.  
  55. private static String extractCompanyFromInput(String companyWithProducts) {
  56. String[] companyWithProductsToArray = companyWithProducts.split(" ");
  57. String company = companyWithProductsToArray[0];
  58. return company;
  59. }
  60.  
  61. private static String extractProductFromInput(String companyWithProducts) {
  62. String[] companyWithProductsToArray = companyWithProducts.split(" ");
  63. String product = companyWithProductsToArray[1];
  64. return product;
  65. }
  66.  
  67. private static String extractQuantityFromInput(String companyWithProducts) {
  68. String[] companyWithProductsToArray = companyWithProducts.split(" ");
  69. String quantity = companyWithProductsToArray[2];
  70. return quantity;
  71. }
  72.  
  73. private static void printProducts(LinkedHashMap<String, String> products) {
  74. String productsToString = products.toString();
  75. productsToString = productsToString.replaceAll("[\\{\\}]+", "");
  76. productsToString = productsToString.replaceAll("=", "-");
  77. System.out.print(productsToString);
  78. }
  79.  
  80. public static LinkedHashMap<String, String> getNutsQuantity(String nut,
  81. String quantity, LinkedHashMap<String, String> products) {
  82.  
  83. if (products.containsKey(nut)) {
  84. String oldQuantity = products.get(nut);
  85. int oldQuantityInt = extractIntegerFromString(oldQuantity);
  86. int quantityInt = extractIntegerFromString(quantity);
  87. int newQuantityInt = oldQuantityInt + quantityInt;
  88. String newQuantityString = concatenateSymbols(newQuantityInt, "kg");
  89. products.put(nut, newQuantityString);
  90. } else {
  91. products.put(nut, quantity);
  92. }
  93.  
  94. return products;
  95. }
  96.  
  97. public static int extractIntegerFromString(String stringWithInteger) {
  98.  
  99. String theIntString = stringWithInteger.substring(0,
  100. stringWithInteger.length() - 2);
  101. int theInt = Integer.parseInt(theIntString);
  102. return theInt;
  103. }
  104.  
  105. public static String concatenateSymbols(int theInt, String theString) {
  106. String result = "";
  107. result = Integer.toString(theInt) + theString;
  108. return result;
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment