Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Discounts (50 поени) Problem 7 (0 / 0)
- Да се имплементира класа Discounts за обработка на информации за цени и цени на попуст на одредени производи во неколку продавници (објекти од класа Store). Потребно е да се имплементираат следните методи:
- public int readStores(InputStream inputStream) - метод за вчитување на податоците за продавниците и цените на производите. Податоците за секоја продавница се во посебен ред во формат [ime] [cena_na_popust1:cena1] [cena_na_popust2:cena2] ... (погледнете пример). Методот враќа колку продавници се вчитани.
- public List<Store> byAverageDiscount() - метод кој враќа листа од 3-те продавници со најголем просечен попуст (просечна вредност на попустот за секој производ од таа продавница). Попустот (намалувањето на цената) е изразен во цел број (проценти) и треба да се пресмета од намалената цена и оригиналната цена. Ако две продавници имаат ист попуст, се подредуваат според името лексикографски.
- public List<Store> byTotalDiscount() - метод кој враќа листа од 3-те продавници со намал вкупен попуст (сума на апсолутен попуст од сите производи). Апсолутен попуст е разликата од цената и цената на попуст. Ако две продавници имаат ист попуст, се подредуваат според името лексикографски.
- Дополнително за класата Store да се имплементира стринг репрезентација, односно методот:
- public String toString() кој ќе враќа репрезентација во следниот формат:
- [Store_name]
- Average discount: [заокружена вредност со едно децимално место]%
- Total discount: [вкупен апсолутен попуст]
- [процент во две места]% [цена на попуст]/[цена]
- ...
- при што продуктите се подредени според процентот на попуст (ако е ист, според апсолутниот попуст) во опаѓачки редослед.
- Input:
- GAP 501:593 6135:7868 1668:2582 3369:4330 9702:15999 8252:13674 3944:5707 2896:4392 9169:17391
- Desigual 5967:9115 5519:9378 3978:5563 7319:13092 8558:10541
- 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
- ZARA 9518:12033 6790:12201 5220:8389 6822:7886 6625:8053 2330:3538 7579:14655 7586:12274 6358:11155 7687:7803 9470:18652
- Levis 6385:9497 9988:19165 7121:11287 1501:2316 2579:4985 6853:8314
- 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
- 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
- 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
- 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
- Bershka 5076:6917 709:1120 5664:10973 2174:3899 8816:14510 558:887 9752:10401 7107:9222
- 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
- Output:
- Stores read: 11
- === By average discount ===
- Levis
- Average discount: 35.8%
- Total discount: 21137
- 48% 2579/4985
- 47% 9988/19165
- 36% 7121/11287
- 35% 1501/2316
- 32% 6385/9497
- 17% 6853/8314
- MassimoDutti
- Average discount: 33.4%
- Total discount: 133040
- 50% 5380/10760
- 49% 6102/12201
- 49% 4789/9414
- 49% 4193/8238
- 49% 3349/6643
- 48% 4566/8941
- 47% 2749/5244
- 47% 1486/2824
- 46% 4999/9384
- 45% 7462/13680
- 43% 7204/12798
- 43% 5793/10278
- 43% 5303/9398
- 43% 5045/8925
- 43% 1470/2586
- 42% 5396/9341
- 42% 3490/6018
- 41% 9310/15946
- 41% 6796/11675
- 41% 4974/8513
- 41% 2360/4041
- 40% 4949/8370
- 37% 3983/6372
- 37% 2461/3961
- 36% 6444/10106
- 35% 6772/10423
- 35% 2418/3739
- 34% 5958/9112
- 32% 3863/5754
- 32% 3214/4750
- 31% 8613/12536
- 31% 4590/6654
- 30% 6206/8961
- 30% 1538/2216
- 30% 496/716
- 29% 5302/7477
- 29% 3251/4626
- 28% 1737/2439
- 24% 9916/13140
- 21% 2218/2808
- 20% 8105/10148
- 19% 5875/7320
- 17% 9114/11094
- 17% 1147/1382
- 14% 1618/1896
- 11% 1403/1585
- 9% 7236/8028
- 9% 5283/5840
- 8% 4180/4589
- 3% 6268/6524
- Desigual
- Average discount: 33.0%
- Total discount: 16348
- 44% 7319/13092
- 41% 5519/9378
- 34% 5967/9115
- 28% 3978/5563
- 18% 8558/10541
- === By total discount ===
- Desigual
- Average discount: 33.0%
- Total discount: 16348
- 44% 7319/13092
- 41% 5519/9378
- 34% 5967/9115
- 28% 3978/5563
- 18% 8558/10541
- Bershka
- Average discount: 32.3%
- Total discount: 18073
- 48% 5664/10973
- 44% 2174/3899
- 39% 8816/14510
- 37% 558/887
- 36% 709/1120
- 26% 5076/6917
- 22% 7107/9222
- 6% 9752/10401
- Levis
- Average discount: 35.8%
- Total discount: 21137
- 48% 2579/4985
- 47% 9988/19165
- 36% 7121/11287
- 35% 1501/2316
- 32% 6385/9497
- 17% 6853/8314
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class DiscountsTest {
- public static void main(String[] args) {
- Discounts discounts = new Discounts();
- int stores = discounts.readStores(System.in);
- System.out.println("Stores read: " + stores);
- System.out.println("=== By average discount ===");
- discounts.byAverageDiscount().forEach(System.out::println);
- System.out.println("=== By total discount ===");
- discounts.byTotalDiscount().forEach(System.out::println);
- }
- }
- class Product {
- private Integer realPrice, discountedPrice;
- public Product(Integer realPrice, Integer discountedPrice) {
- this.realPrice = realPrice;
- this.discountedPrice = discountedPrice;
- }
- public Integer getDiscountPercent() {
- if(discountedPrice > realPrice) {
- System.out.println("S: " + discountedPrice + " " + realPrice);
- }
- double priceInPercent = 1-((double)discountedPrice / realPrice);
- return (int) (priceInPercent * 100);
- }
- public Integer getDiscountedPrice() {
- return realPrice - discountedPrice;
- }
- public Integer getRealPrice() {
- return realPrice;
- }
- @Override
- public String toString() {
- return String.format("%2d%% %d/%d", getDiscountPercent(), discountedPrice, realPrice);
- }
- }
- class Store {
- private String name;
- private Set<Product> products;
- public Store(String name, List<Product> products) {
- this.name = name;
- this.products = new TreeSet<>(Comparator.comparing(Product::getDiscountPercent).reversed().thenComparing(Comparator.comparing(Product::getRealPrice).reversed()));
- this.products.addAll(products);
- }
- public Integer totalDiscount() {
- return products.stream()
- .mapToInt(Product::getDiscountedPrice)
- .sum();
- }
- public Double averageDiscount() {
- double sum = products.stream()
- .mapToDouble(Product::getDiscountPercent)
- .sum();
- return sum / products.size();
- }
- public String getName() {
- return name;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- Character percent = '%';
- sb.append(String.format("%s\nAverage discount: %.1f%c\nTotal discount: %d\n", name, averageDiscount(), percent, totalDiscount()));
- products.forEach((p) -> sb.append(p.toString()).append("\n"));
- sb.setLength(sb.length()-1);
- return sb.toString();
- }
- }
- class Discounts {
- private Set<Store> stores;
- public Discounts() {
- stores = new TreeSet<>(Comparator.comparingDouble(Store::averageDiscount).reversed().thenComparing(Store::getName));
- }
- public int readStores(InputStream inputStream) {
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
- String line;
- int storesRead = 0;
- try {
- while ((line = br.readLine()) != null) {
- if(line.equals("KRAJ")) break;
- String[] parts = line.split("\\s+");
- List<Product> products = IntStream.range(1, parts.length)
- .mapToObj((ind) -> {
- String[] prices = parts[ind].split(":");
- return new Product(Integer.parseInt(prices[1]), Integer.parseInt(prices[0]));
- })
- .collect(Collectors.toList());
- stores.add(new Store(parts[0], products));
- storesRead++;
- }
- } catch(IOException e) {
- System.out.println("ERROR fml");
- }
- return storesRead;
- }
- public List<Store> byAverageDiscount() {
- return stores.stream().limit(3).collect(Collectors.toList());
- }
- public List<Store> byTotalDiscount() {
- return stores.stream()
- .sorted(Comparator.comparingInt(Store::totalDiscount))
- .limit(3)
- .collect(Collectors.toList());
- }
- }
- // Vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment