Martina312

[НП] - Discounts

Jan 30th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. class DiscountPercentSorter implements Comparator<Store>{
  6.  
  7. @Override
  8. public int compare(Store o1, Store o2) {
  9. return o1.getAverageDiscount()>o2.getAverageDiscount()? -1 : o1.getAverageDiscount()<o2.getAverageDiscount()? 1:0;
  10. }
  11. }
  12.  
  13. class NameSorter implements Comparator<Store>{
  14.  
  15. @Override
  16. public int compare(Store o1, Store o2) {
  17. return o1.getName().compareTo(o2.getName());
  18. }
  19. }
  20.  
  21. class TotalDiscount implements Comparator<Store>{
  22.  
  23. @Override
  24. public int compare(Store o1, Store o2) {
  25. return o1.getTotalDiscount()>o2.getTotalDiscount()? 1 : o1.getTotalDiscount()<o2.getTotalDiscount()? -1:0;
  26. }
  27. }
  28.  
  29. class PercentageSorter implements Comparator<StoreItem>{
  30.  
  31. @Override
  32. public int compare(StoreItem o1, StoreItem o2) {
  33. return o1.getDiscountPercentage()>o2.getDiscountPercentage()? -1 : o1.getDiscountPercentage()<o2.getDiscountPercentage()? 1:0;
  34. }
  35. }
  36.  
  37. class ItemPrice implements Comparator<StoreItem>{
  38.  
  39. @Override
  40. public int compare(StoreItem o1, StoreItem o2) {
  41. return o1.getDiscountPrice()>o2.getDiscountPrice()? -1 : o1.getDiscountPrice()<o2.getDiscountPrice()? 1:0;
  42. }
  43.  
  44. }
  45.  
  46. class StoreItem{
  47. private int price;
  48. private int discountPrice;
  49.  
  50. public StoreItem(int discountPrice, int price) {
  51. this.price = price;
  52. this.discountPrice = discountPrice;
  53. }
  54.  
  55. public int getPrice() {
  56. return price;
  57. }
  58.  
  59. public int getDiscountPrice() {
  60. return discountPrice;
  61. }
  62.  
  63. public int getDiff(){
  64. return price-discountPrice;
  65. }
  66.  
  67. public int getDiscountPercentage(){
  68. return (int) (100-(100/((float)price/discountPrice)));
  69. }
  70.  
  71. @Override
  72. public String toString() {
  73. StringBuilder sb=new StringBuilder();
  74. sb.append(String.format("\n%2d%% %d/%d",getDiscountPercentage(),discountPrice,price));
  75. // sb.append("\n"+this.getDiscountPercentage()+"% "+discountPrice+"/"+price);
  76. return sb.toString();
  77. }
  78. }
  79. class Store{
  80. private String name;
  81. private List<StoreItem> items;
  82.  
  83. public Store(String name, List<StoreItem> items) {
  84. this.name = name;
  85. this.items = items;
  86. }
  87.  
  88. public float getAverageDiscount(){
  89. float sum=0;
  90. for(StoreItem i:items){
  91. sum+=i.getDiscountPercentage();
  92. }
  93. return sum/items.size();
  94. }
  95.  
  96. public int getTotalDiscount(){
  97. int sum=0;
  98. for(StoreItem i:items){
  99. sum+=i.getDiff();
  100. }
  101. return sum;
  102. }
  103.  
  104. public String getName() {
  105. return name;
  106. }
  107.  
  108. public String toString(){
  109. StringBuilder sb=new StringBuilder();
  110. sb.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d", name,(float)getAverageDiscount(),getTotalDiscount()));
  111.  
  112. for(StoreItem si:items) {
  113. sb.append(si);
  114. }
  115. return sb.toString();
  116. }
  117. }
  118.  
  119. class Discounts{
  120. private List<Store> stores;
  121.  
  122. public Discounts() {
  123. stores=new ArrayList<>();
  124. }
  125.  
  126. public int readStores(InputStream inputStream){
  127. Scanner in= new Scanner(inputStream);
  128.  
  129. while(in.hasNextLine()){
  130. String line=in.nextLine();
  131. String []parts=line.split("\\s+");
  132. String name=parts[0];
  133.  
  134. List<StoreItem> items=new ArrayList<>();
  135. for(int i=1;i<parts.length;i++){
  136. String [] priceParts=parts[i].split(":");
  137. items.add(new StoreItem(Integer.parseInt(priceParts[0]),Integer.parseInt(priceParts[1])));
  138.  
  139. }
  140. items.sort(new PercentageSorter().thenComparing(new ItemPrice()));
  141. stores.add(new Store(name,items));
  142. }
  143. return stores.size();
  144. }
  145.  
  146. public List<Store> byAverageDiscount(){
  147. Collections.sort(stores,new DiscountPercentSorter().thenComparing(new NameSorter()));
  148.  
  149. return stores.stream().limit(3).collect(Collectors.toList());
  150. }
  151.  
  152. public List<Store> byTotalDiscount(){
  153. Collections.sort(stores, new TotalDiscount().thenComparing(new NameSorter()));
  154.  
  155. return stores.stream().limit(3).collect(Collectors.toList());
  156. }
  157.  
  158. public String toString() {
  159. StringBuilder sb=new StringBuilder();
  160. for(Store s:stores){
  161. sb.append(s);
  162. }
  163. return sb.toString();
  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