fork(1) download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Main {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. // Integer count of number of items in the store
  8. int count = scan.nextInt();
  9. // Create an array to store names and prices of each item
  10. String[] itemName = new String[count];
  11. double[] itemPrice = new double[count];
  12. for (int i = 0; i < count; i++) {
  13. // Scan name of each item and price
  14. itemName[i] = scan.next();
  15. itemPrice[i] = scan.nextDouble();
  16. }
  17. // Integer count for the number of customers
  18. int numCustomers = scan.nextInt();
  19. String[] nameF = new String[numCustomers];
  20. String[] nameL = new String[numCustomers];
  21. double[] costs = new double[numCustomers];
  22. final Map<String, Integer> numBought = new HashMap<>();
  23. final Map<String, Integer> peopleBought = new HashMap<>();
  24. for (int j = 0; j < numCustomers; j++) {
  25. // First and last name of each customer
  26. nameF[j] = scan.next();
  27. nameL[j] = scan.next();
  28.  
  29. // Number of items bought
  30. int numItems = scan.nextInt();
  31. for (int k = 0; k < numItems; k++) {
  32. // For each number of items bought, name and quantity
  33. int numItemBought = scan.nextInt();
  34. String nameOfItem = scan.next();
  35. numBought.merge(nameOfItem, numItemBought, Integer::sum);// increase quantity of the item
  36. peopleBought.merge(nameOfItem, 1, Integer::sum);// increment number of people who bought this item
  37. }
  38. }
  39. for (final String item : itemName) {
  40. final Integer num = numBought.get(item);
  41. if (num == null) {
  42. System.out.println("No one bought " + item);
  43. } else {
  44. System.out.println(peopleBought.get(item) + " bought " + num + " " + item);
  45. }
  46. }
  47. }
  48. }
Success #stdin #stdout 0.19s 39860KB
stdin
6

Apple 0.25

Banana 0.75

Milk 3.15

Orange 1.25

Salami 2.50

Sponge 1.15

3

Callie Brown 3 2 Banana 1 Orange 2 Milk

Chris Tucker 2 3 Banana 2 Sponge

Jane Weiss 1 5 Salami
stdout
No one bought Apple
2 bought 5 Banana
1 bought 2 Milk
1 bought 1 Orange
1 bought 5 Salami
1 bought 2 Sponge