Andziev

Discounts

Jun 5th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.*;
  3. import java.io.*;
  4.  
  5. public class DiscountsTest {
  6.     public static void main(String[] args) {
  7.         Discounts discounts = new Discounts();
  8.         int stores = discounts.readStores(System.in);
  9.         System.out.println("Stores read: " + stores);
  10.         System.out.println("=== By average discount ===");
  11.         discounts.byAverageDiscount().forEach(System.out::println);
  12.         System.out.println("=== By total discount ===");
  13.         discounts.byTotalDiscount().forEach(System.out::println);
  14.     }
  15. }
  16.  
  17. class Price {
  18.     private int discount;
  19.     private int price;
  20.    
  21.     public Price (String discount, String price) {
  22.         this.discount = Integer.parseInt(discount);
  23.         this.price = Integer.parseInt(price);
  24.     }
  25.    
  26.     public int getAbsolute () {
  27.         return price - discount;
  28.     }
  29.    
  30.     public double getAverage () {
  31.         return ((price - discount) * 100) / price;
  32.     }
  33.    
  34.     public String toString () {
  35.         return String.format("%2d%% %d/%d\n", (int)getAverage(), discount, price);
  36.     }
  37. }
  38.  
  39. class Store {
  40.     private String name;
  41.     private List <Price> prices;
  42.    
  43.     public Store (String name, List <Price> prices) {
  44.         this.name = name;
  45.         this.prices = prices;
  46.     }
  47.    
  48.     public int Absolute () {
  49.         return prices.stream().mapToInt(price -> price.getAbsolute()).sum();
  50.     }
  51.    
  52.     public double Average () {
  53.         return prices.stream().mapToDouble(price -> price.getAverage()).average().orElse(0);
  54.     }
  55.    
  56.     static Store fromString (String s) {
  57.         List <Price> list = new ArrayList<> ();
  58.         String [] parts = s.split("\\s++");
  59.         IntStream.range(1, parts.length)
  60.             .forEach(i -> {
  61.                 String [] newPart = parts[i].split(":");
  62.                 Price price = new Price (newPart[0], newPart[1]);
  63.                 list.add(price);
  64.             });
  65.         return new Store (parts[0], list);
  66.     }
  67.    
  68.     public String getName () {
  69.         return name;
  70.     }
  71.    
  72.     public String toString () {
  73.         StringBuilder builder = new StringBuilder ();
  74.         builder.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d\n",
  75.                                     name, Average(), Absolute()));
  76.         prices.stream()
  77.             .sorted(Comparator.comparing(Price::getAverage)
  78.                             .thenComparing(Price::getAbsolute).reversed())
  79.             .forEach(each -> builder.append(each));
  80.         builder.setLength(builder.length() - 1);
  81.         return builder.toString();
  82.     }
  83. }
  84.  
  85. class Discounts {
  86.     private List <Store> list;
  87.    
  88.     public Discounts () {
  89.         list = new ArrayList <> ();
  90.     }
  91.    
  92.     public int readStores (InputStream inputStream) {
  93.         BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream));
  94.         list = reader.lines()
  95.                 .map(Store::fromString)
  96.                 .collect(Collectors.toList());
  97.         return list.size();
  98.     }
  99.    
  100.     public List<Store> byAverageDiscount () {
  101.         return list.stream()               
  102.                 .sorted(Comparator.comparing(Store::Average).reversed()
  103.                                   .thenComparing(Store::getName))
  104.                 .limit(3)
  105.                 .collect(Collectors.toList());
  106.     }
  107.    
  108.     public List <Store> byTotalDiscount () {
  109.         return list.stream()               
  110.                 .sorted(Comparator.comparing(Store::Absolute)
  111.                                     .thenComparing(Store::getName))
  112.                 .limit(3)
  113.                 .collect(Collectors.toList());
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment