luoni

AndreyAndBilliard2

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