MartinPaunov

Untitled

Jun 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class test {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. int n = Integer.parseInt(scanner.nextLine());
  13. TreeMap<String, Double> shop = new TreeMap<>();
  14.  
  15. for (int i = 0; i < n; i++) {
  16. String[] input = scanner.nextLine().split("-");
  17. shop.put(input[0], Double.parseDouble(input[1]));
  18. }
  19.  
  20. List<Customer2> allCustomers = new ArrayList<>();
  21.  
  22. while (true) {
  23. String input = scanner.nextLine();
  24. if (input.equals("end of clients")) {
  25. break;
  26. }
  27.  
  28. String[] data = input.split("[-,]");
  29. String name = data[0];
  30. String product = data[1];
  31. int quantity = Integer.parseInt(data[2]);
  32.  
  33. if (!shop.containsKey(product)) {
  34. continue;
  35. }
  36.  
  37. Customer2 client = allCustomers.stream().filter(s -> s.getName().equals(name)).findFirst()
  38. .orElse(Customer2.DEFAULT);
  39.  
  40. if (client == null) {
  41. Customer2 newClient = new Customer2(name);
  42. newClient.getShopList().put(product, quantity);
  43. allCustomers.add(newClient);
  44. } else {
  45. if (client.getShopList().containsKey(product) == false) {
  46. client.getShopList().put(product, quantity);
  47. } else {
  48. int temp = client.getShopList().get(product);
  49. client.getShopList().put(product, quantity + temp);
  50. }
  51. }
  52. }
  53.  
  54. final double[] total = {0};
  55.  
  56. allCustomers
  57. .stream()
  58. .sorted((a, b) -> a.getName().compareTo(b.getName()))
  59. .forEach(client -> {
  60. System.out.println(client.getName());
  61. double bill = 0;
  62.  
  63. for (Map.Entry<String, Integer> order : client.getShopList().entrySet()) {
  64. String productName = order.getKey();
  65. int quantity = order.getValue();
  66. double price = shop.get(productName);
  67.  
  68. System.out.printf("-- %s - %d%n", productName, quantity);
  69. bill += quantity * price;
  70. }
  71. total[0] += bill;
  72. System.out.printf("Bill: %.2f%n", bill);
  73. });
  74. System.out.printf("Total bill: %.2f%n", total[0]);
  75. }
  76. }
  77.  
  78. class Customer2 {
  79. public static final Customer2 DEFAULT = null;
  80. private String name;
  81. private TreeMap<String, Integer> shopList;
  82.  
  83. public Customer2(String name) {
  84. this.name = name;
  85. this.shopList = new TreeMap<String, Integer>();
  86. }
  87.  
  88. public String getName() {
  89. return name;
  90. }
  91.  
  92. public void setName(String name) {
  93. this.name = name;
  94. }
  95.  
  96. public TreeMap<String, Integer> getShopList() {
  97. return shopList;
  98. }
  99.  
  100. public void setShopList(TreeMap<String, Integer> shopList) {
  101. this.shopList = shopList;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment