lameski

Discounts

Aug 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8. import java.util.Set;
  9. import java.util.TreeMap;
  10. import java.util.TreeSet;
  11. import java.util.stream.Collectors;
  12.  
  13. /**
  14. * Discounts
  15. */
  16. class Product {
  17. int price;
  18. int discountedPrice;
  19.  
  20. public Product(int discountedPrice, int price) {
  21. this.discountedPrice = discountedPrice;
  22. this.price = price;
  23. }
  24.  
  25. public static Product ofString(String product) {
  26. String[] parts = product.split(":");
  27. return new Product(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
  28. }
  29.  
  30. public double discount() {
  31. return (1 - (discountedPrice *1.00 / price));
  32. }
  33.  
  34. public int absoluteDiscount() {
  35. return price - discountedPrice;
  36. }
  37.  
  38. @Override
  39. public String toString() {
  40. return String.format("%2d%% %d/%d", discount(), discountedPrice, price);
  41. }
  42. }
  43. class Store //implements Comparable<Store>
  44. {
  45. String name;
  46. ArrayList<Product> products;
  47.  
  48. public Store(String name, ArrayList<Product> products)
  49. {
  50. this.name = name;
  51. this.products = products;
  52. }
  53.  
  54. public String getName()
  55. {
  56. return name;
  57. }
  58. public Integer numProducts()
  59. {
  60. return products.size();
  61. }
  62. public double[] getDiscounts()
  63. {
  64. double dis[] = new double[products.size()];
  65. int i=0;
  66. for(Product m : products)
  67. {
  68. dis[i++] = m.discount();
  69. }
  70. return dis;
  71. }
  72. public double getAvgDis()
  73. {
  74. double sum = 0;
  75.  
  76. for(double d : this.getDiscounts())
  77. sum+=d;
  78. return sum*100/this.numProducts();
  79. }
  80.  
  81. public int totalDiscount()
  82. {
  83. int sum = 0;
  84. for(Product i : products)
  85. {
  86. sum+= i.absoluteDiscount();
  87. }
  88. return sum;
  89. }
  90.  
  91. @Override
  92. public String toString() {
  93. // TODO Auto-generated method stub
  94. StringBuilder sb = new StringBuilder();
  95.  
  96. sb.append(name);
  97. sb.append("\n");
  98.  
  99. sb.append(String.format("Average discount: %.1f%%\n", this.getAvgDis()-1));
  100. sb.append(String.format("Total discount: %d\n", this.totalDiscount()));
  101.  
  102. products.stream().sorted(Comparator.comparing(Product::discount).reversed())
  103. .forEach(i -> {
  104. //double procent = 100*((1- i.discountedPrice*1.00/i.price));
  105. sb.append(String.format("%2.0f%% %d/%d\n", i.discount()*100, i.discountedPrice, i.price));
  106. });;
  107. /*for(Product i : products)
  108. {
  109. double procent = 100*((1- i.discountedPrice*1.00/i.price));
  110. sb.append(String.format("%2.0f%% %d/%d\n", procent, i.discountedPrice, i.price));
  111. }
  112. */
  113. sb.deleteCharAt(sb.toString().length()-1);
  114. return sb.toString();
  115. }
  116.  
  117.  
  118.  
  119.  
  120. }
  121. class Discounts
  122. {
  123. ArrayList<Store> stores;
  124. Discounts()
  125. {
  126. this.stores = new ArrayList<>();
  127. }
  128. public int readStores(InputStream input)
  129. {
  130. Scanner in = new Scanner(input);
  131. int count = 0;
  132. while(in.hasNextLine())
  133. {
  134. String [] line = in.nextLine().split("\\s+");
  135. String name = line[0];
  136. ArrayList<Product> products = new ArrayList<>();
  137. for(int i=1; i<line.length; i++)
  138. {
  139. String[] split = line[i].split(":");
  140. Product p = new Product(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
  141. products.add(p);
  142. }
  143.  
  144. stores.add(new Store(name, products));
  145. count++;
  146. }
  147. return count;
  148.  
  149.  
  150. }
  151.  
  152. public List<Store> byAverageDiscount()
  153. {
  154. return stores.stream().sorted(Comparator.comparing(Store::getAvgDis).reversed()
  155. .thenComparing(Store::getName)).limit(3).collect(Collectors.toList());
  156.  
  157. }
  158.  
  159. public List<Store> byTotalDiscount()
  160. {
  161. return stores.stream().sorted(Comparator.comparing(Store::totalDiscount)
  162. .thenComparing(Store::getName)).limit(3).collect(Collectors.toList());
  163. }
  164. }
  165.  
  166.  
  167. public class DiscountsTest {
  168. public static void main(String[] args) {
  169. Discounts discounts = new Discounts();
  170. int stores = discounts.readStores(System.in);
  171. System.out.println("Stores read: " + stores);
  172. System.out.println("=== By average discount ===");
  173. discounts.byAverageDiscount().forEach(System.out::println);
  174. System.out.println("=== By total discount ===");
  175. discounts.byTotalDiscount().forEach(System.out::println);
  176. }
  177. }
  178.  
  179. // Vashiot kod ovde
Add Comment
Please, Sign In to add comment