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