StefanTodorovski

[НП] Discount

Feb 6th, 2019
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.96 KB | None | 0 0
  1. /*
  2. Discounts (50 поени) Problem 7 (0 / 0)
  3. Да се имплементира класа Discounts за обработка на информации за цени и цени на попуст на одредени производи во неколку продавници (објекти од класа Store). Потребно е да се имплементираат следните методи:
  4.  
  5. public int readStores(InputStream inputStream) - метод за вчитување на податоците за продавниците и цените на производите. Податоците за секоја продавница се во посебен ред во формат [ime] [cena_na_popust1:cena1] [cena_na_popust2:cena2] ... (погледнете пример). Методот враќа колку продавници се вчитани.
  6.  
  7. public List<Store> byAverageDiscount() - метод кој враќа листа од 3-те продавници со најголем просечен попуст (просечна вредност на попустот за секој производ од таа продавница). Попустот (намалувањето на цената) е изразен во цел број (проценти) и треба да се пресмета од намалената цена и оригиналната цена. Ако две продавници имаат ист попуст, се подредуваат според името лексикографски.
  8.  
  9. public List<Store> byTotalDiscount() - метод кој враќа листа од 3-те продавници со намал вкупен попуст (сума на апсолутен попуст од сите производи). Апсолутен попуст е разликата од цената и цената на попуст. Ако две продавници имаат ист попуст, се подредуваат според името лексикографски.
  10.  
  11. Дополнително за класата Store да се имплементира стринг репрезентација, односно методот:
  12. public String toString() кој ќе враќа репрезентација во следниот формат:
  13. [Store_name]
  14. Average discount: [заокружена вредност со едно децимално место]%
  15. Total discount: [вкупен апсолутен попуст]
  16. [процент во две места]% [цена на попуст]/[цена]
  17. ...
  18. при што продуктите се подредени според процентот на попуст (ако е ист, според апсолутниот попуст) во опаѓачки редослед.
  19.  
  20.  
  21. Input:
  22. GAP 501:593  6135:7868  1668:2582  3369:4330  9702:15999  8252:13674  3944:5707  2896:4392  9169:17391  
  23. Desigual 5967:9115  5519:9378  3978:5563  7319:13092  8558:10541  
  24. Stradivarius 7833:15538  7407:10477  9768:13688  9797:15637  2466:2812  7606:13665  580:1024  4792:7212  1864:2090  623:1241  1116:2056  1839:3465  4520:5846  8724:14790  9636:16158  6885:8021  3014:5378  1442:2096  7939:7975  4084:5292  4301:7688  1389:2447  1541:1874  7436:8836  8520:10214  3495:5263  4953:9306  1578:2970  3798:6904  1592:2620  5872:6308  4085:5857  8321:12453  3866:5965  5549:10073  8198:12214  
  25. ZARA 9518:12033  6790:12201  5220:8389  6822:7886  6625:8053  2330:3538  7579:14655  7586:12274  6358:11155  7687:7803  9470:18652  
  26. Levis 6385:9497  9988:19165  7121:11287  1501:2316  2579:4985  6853:8314  
  27. MassimoDutti 5875:7320  4789:9414  1538:2216  5045:8925  4974:8513  2461:3961  2360:4041  4193:8238  3863:5754  4590:6654  1403:1585  4999:9384  1737:2439  3214:4750  5302:7477  5793:10278  3983:6372  6102:12201  6796:11675  9310:15946  5396:9341  5958:9112  1486:2824  1147:1382  2418:3739  3349:6643  6206:8961  6268:6524  8613:12536  9916:13140  6772:10423  5283:5840  6444:10106  1470:2586  3490:6018  2218:2808  5303:9398  7462:13680  496:716  8105:10148  2749:5244  7236:8028  4949:8370  4180:4589  7204:12798  5380:10760  4566:8941  3251:4626  9114:11094  1618:1896  
  28. Diesel 6911:11246  6373:8142  5469:10746  8925:10579  3159:5944  9439:16987  6076:7702  5513:10041  436:717  9956:11360  7310:7786  2368:2513  8589:11212  1215:1938  8441:8880  3897:7328  3332:6124  9273:10409  7164:12025  1426:1566  1181:1678  6429:10230  2932:4400  1264:1659  4014:7472  2072:3575  2269:2446  498:888  5156:6306  7401:8986  8217:15414  8210:10862  2932:3266  2413:4194  7455:13196  4279:4450  4103:7840  8477:12871  6413:10470  9100:9598  6449:8291  7370:12116  1054:1500  4831:5558  4438:5086  3167:5873  8518:10872  2390:2893  
  29. H&M 3848:7374  8797:12477  1240:1719  577:935  2620:4112  716:781  2896:4685  824:1565  8119:8676  8965:17503  7769:11291  8432:13984  6532:10276  2304:4408  9596:16915  993:1808  8503:16782  2273:3323  7109:9664  5660:11049  708:1070  3602:4133  8152:8359  1641:1677  4562:5488  5219:8159  7752:9557  8558:12196  1064:1963  4010:5531  6506:7745  1557:2661  9926:17100  4396:5788  6640:12243  7297:9141  7547:13233  9488:12316  5830:8733  8727:8799  9618:14282  
  30. P&B 7577:10157  474:555  7234:14022  9951:17736  7774:13741  2816:4429  384:735  5824:8043  6022:8681  3764:6387  8647:16231  3262:5741  4714:8433  5490:8897  3737:5052  4932:8355  7429:10196  1522:1558  8488:16514  5599:7254  5557:6347  9916:17501  8913:16125  775:1267  7189:11996  7044:11387  3238:3852  7780:13183  1686:3278  7523:14707  3422:5782  8974:16318  8050:10189  4350:5821  8460:13622  9121:16086  6025:6678  7960:10383  8805:16415  9435:10086  3160:3550  8310:8523  6551:10056  7142:8746  9948:13982  3881:4845  2318:4400  2352:4228  9883:17025  
  31. Bershka 5076:6917  709:1120  5664:10973  2174:3899  8816:14510  558:887  9752:10401  7107:9222  
  32. Holister 1471:1908  4358:5850  3544:3619  3463:3774  3799:3973  3307:4279  4641:8691  4025:6667  5798:8432  2072:2882  6308:9647  1367:1967  1474:2113  7640:15070  8772:9704  2388:3629  6226:7222  5115:9375  5125:6084  3240:5626  5703:10270  1011:1495  4072:5462  9933:15013  4559:7457  1858:2892  4572:7414
  33.  
  34. Output:
  35. Stores read: 11
  36. === By average discount ===
  37. Levis
  38. Average discount: 35.8%
  39. Total discount: 21137
  40. 48% 2579/4985
  41. 47% 9988/19165
  42. 36% 7121/11287
  43. 35% 1501/2316
  44. 32% 6385/9497
  45. 17% 6853/8314
  46. MassimoDutti
  47. Average discount: 33.4%
  48. Total discount: 133040
  49. 50% 5380/10760
  50. 49% 6102/12201
  51. 49% 4789/9414
  52. 49% 4193/8238
  53. 49% 3349/6643
  54. 48% 4566/8941
  55. 47% 2749/5244
  56. 47% 1486/2824
  57. 46% 4999/9384
  58. 45% 7462/13680
  59. 43% 7204/12798
  60. 43% 5793/10278
  61. 43% 5303/9398
  62. 43% 5045/8925
  63. 43% 1470/2586
  64. 42% 5396/9341
  65. 42% 3490/6018
  66. 41% 9310/15946
  67. 41% 6796/11675
  68. 41% 4974/8513
  69. 41% 2360/4041
  70. 40% 4949/8370
  71. 37% 3983/6372
  72. 37% 2461/3961
  73. 36% 6444/10106
  74. 35% 6772/10423
  75. 35% 2418/3739
  76. 34% 5958/9112
  77. 32% 3863/5754
  78. 32% 3214/4750
  79. 31% 8613/12536
  80. 31% 4590/6654
  81. 30% 6206/8961
  82. 30% 1538/2216
  83. 30% 496/716
  84. 29% 5302/7477
  85. 29% 3251/4626
  86. 28% 1737/2439
  87. 24% 9916/13140
  88. 21% 2218/2808
  89. 20% 8105/10148
  90. 19% 5875/7320
  91. 17% 9114/11094
  92. 17% 1147/1382
  93. 14% 1618/1896
  94. 11% 1403/1585
  95.  9% 7236/8028
  96.  9% 5283/5840
  97.  8% 4180/4589
  98.  3% 6268/6524
  99. Desigual
  100. Average discount: 33.0%
  101. Total discount: 16348
  102. 44% 7319/13092
  103. 41% 5519/9378
  104. 34% 5967/9115
  105. 28% 3978/5563
  106. 18% 8558/10541
  107. === By total discount ===
  108. Desigual
  109. Average discount: 33.0%
  110. Total discount: 16348
  111. 44% 7319/13092
  112. 41% 5519/9378
  113. 34% 5967/9115
  114. 28% 3978/5563
  115. 18% 8558/10541
  116. Bershka
  117. Average discount: 32.3%
  118. Total discount: 18073
  119. 48% 5664/10973
  120. 44% 2174/3899
  121. 39% 8816/14510
  122. 37% 558/887
  123. 36% 709/1120
  124. 26% 5076/6917
  125. 22% 7107/9222
  126.  6% 9752/10401
  127. Levis
  128. Average discount: 35.8%
  129. Total discount: 21137
  130. 48% 2579/4985
  131. 47% 9988/19165
  132. 36% 7121/11287
  133. 35% 1501/2316
  134. 32% 6385/9497
  135. 17% 6853/8314
  136. */
  137.  
  138. import java.io.BufferedReader;
  139. import java.io.IOException;
  140. import java.io.InputStream;
  141. import java.io.InputStreamReader;
  142. import java.util.*;
  143. import java.util.stream.Collectors;
  144. import java.util.stream.IntStream;
  145.  
  146. public class DiscountsTest {
  147.     public static void main(String[] args) {
  148.         Discounts discounts = new Discounts();
  149.         int stores = discounts.readStores(System.in);
  150.         System.out.println("Stores read: " + stores);
  151.         System.out.println("=== By average discount ===");
  152.         discounts.byAverageDiscount().forEach(System.out::println);
  153.         System.out.println("=== By total discount ===");
  154.         discounts.byTotalDiscount().forEach(System.out::println);
  155.     }
  156. }
  157.  
  158. class Product {
  159.     private Integer realPrice, discountedPrice;
  160.  
  161.     public Product(Integer realPrice, Integer discountedPrice) {
  162.         this.realPrice = realPrice;
  163.         this.discountedPrice = discountedPrice;
  164.     }
  165.  
  166.     public Integer getDiscountPercent() {
  167.         if(discountedPrice > realPrice) {
  168.             System.out.println("S: " + discountedPrice + " " + realPrice);
  169.         }
  170.  
  171.         double priceInPercent = 1-((double)discountedPrice / realPrice);
  172.         return (int) (priceInPercent * 100);
  173.     }
  174.  
  175.     public Integer getDiscountedPrice() {
  176.         return realPrice - discountedPrice;
  177.     }
  178.  
  179.     public Integer getRealPrice() {
  180.         return realPrice;
  181.     }
  182.  
  183.     @Override
  184.     public String toString() {
  185.         return String.format("%2d%% %d/%d", getDiscountPercent(), discountedPrice, realPrice);
  186.     }
  187. }
  188.  
  189. class Store {
  190.     private String name;
  191.     private Set<Product> products;
  192.  
  193.     public Store(String name, List<Product> products) {
  194.         this.name = name;
  195.         this.products = new TreeSet<>(Comparator.comparing(Product::getDiscountPercent).reversed().thenComparing(Comparator.comparing(Product::getRealPrice).reversed()));
  196.         this.products.addAll(products);
  197.     }
  198.  
  199.     public Integer totalDiscount() {
  200.         return products.stream()
  201.                 .mapToInt(Product::getDiscountedPrice)
  202.                 .sum();
  203.     }
  204.  
  205.     public Double averageDiscount() {
  206.         double sum = products.stream()
  207.                 .mapToDouble(Product::getDiscountPercent)
  208.                 .sum();
  209.         return sum / products.size();
  210.     }
  211.  
  212.     public String getName() {
  213.         return name;
  214.     }
  215.  
  216.     @Override
  217.     public String toString() {
  218.         StringBuilder sb = new StringBuilder();
  219.         Character percent = '%';
  220.         sb.append(String.format("%s\nAverage discount: %.1f%c\nTotal discount: %d\n", name, averageDiscount(), percent, totalDiscount()));
  221.         products.forEach((p) -> sb.append(p.toString()).append("\n"));
  222.         sb.setLength(sb.length()-1);
  223.         return sb.toString();
  224.     }
  225. }
  226.  
  227. class Discounts {
  228.     private Set<Store> stores;
  229.  
  230.     public Discounts() {
  231.         stores = new TreeSet<>(Comparator.comparingDouble(Store::averageDiscount).reversed().thenComparing(Store::getName));
  232.     }
  233.  
  234.     public int readStores(InputStream inputStream) {
  235.         BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  236.         String line;
  237.         int storesRead = 0;
  238.         try {
  239.             while ((line = br.readLine()) != null) {
  240.                 if(line.equals("KRAJ")) break;
  241.  
  242.                 String[] parts = line.split("\\s+");
  243.                 List<Product> products = IntStream.range(1, parts.length)
  244.                         .mapToObj((ind) -> {
  245.                             String[] prices = parts[ind].split(":");
  246.                             return new Product(Integer.parseInt(prices[1]), Integer.parseInt(prices[0]));
  247.                         })
  248.                         .collect(Collectors.toList());
  249.                 stores.add(new Store(parts[0], products));
  250.  
  251.                 storesRead++;
  252.             }
  253.         } catch(IOException e) {
  254.             System.out.println("ERROR fml");
  255.         }
  256.         return storesRead;
  257.     }
  258.  
  259.     public List<Store> byAverageDiscount() {
  260.         return stores.stream().limit(3).collect(Collectors.toList());
  261.     }
  262.  
  263.     public List<Store> byTotalDiscount() {
  264.         return stores.stream()
  265.                 .sorted(Comparator.comparingInt(Store::totalDiscount))
  266.                 .limit(3)
  267.                 .collect(Collectors.toList());
  268.     }
  269. }
  270.  
  271. // Vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment