Advertisement
dzocesrce

[NP] Store Discounts

Apr 24th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.io.BufferedReader;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.Comparator;
  10. import java.util.stream.IntStream;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.stream.Collectors;
  14.  
  15. class Discounts {
  16.     List<Store> stores;
  17.  
  18.     public Discounts() {
  19.         this.stores = new ArrayList<>();
  20.     }
  21.  
  22.     public int readStores(InputStream in) {
  23.         BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(in));
  24.         stores= bufferedReader.lines().map(Store::create).collect(Collectors.toList());
  25.         return stores.toArray().length;
  26.     }
  27.  
  28.     public List<Store> byAverageDiscount(){
  29.         Comparator<Store> comparatorAverage= Comparator.comparing(Store::getAverageDiscount).reversed().thenComparing(Store::getName);
  30.         return stores.stream().sorted(comparatorAverage).limit(3).collect(Collectors.toList());
  31.     }
  32.  
  33.     public List<Store> byTotalDiscount(){
  34.         Comparator<Store> comparatorTotal= Comparator.comparing(Store::getTotalDiscount).thenComparing(Store::getName);
  35.         return stores.stream().sorted(comparatorTotal).limit(3).collect(Collectors.toList());
  36.     }
  37. }
  38. class Store{
  39.     String name;
  40.     List<Integer> discountPrices;
  41.     List<Integer> originalPrices;
  42.  
  43.     public Store(String name, List<Integer> discountPrices, List<Integer> originalPrices) {
  44.         this.name = name;
  45.         this.discountPrices = discountPrices;
  46.         this.originalPrices = originalPrices;
  47.     }
  48.  
  49.     public void sortByDiscountDescending() {
  50.         List<Integer> indices = IntStream.range(0, discountPrices.size())
  51.                 .boxed()
  52.                 .sorted(Comparator.comparingInt(i ->
  53.                                 (int)getDiscountInPercent(discountPrices.get((int)i), originalPrices.get((int)i)))
  54.                         .thenComparingInt(i ->
  55.                                 discountPrices.get((int)i))
  56.                                 //getDiscountInPrice(discountPrices.get((int)i), originalPrices.get((int)i)))
  57.                         .reversed())
  58.                 .collect(Collectors.toList());
  59.  
  60.         List<Integer> sortedDiscountPrices = indices.stream()
  61.                 .map(discountPrices::get)
  62.                 .collect(Collectors.toList());
  63.  
  64.         List<Integer> sortedOriginalPrices = indices.stream()
  65.                 .map(originalPrices::get)
  66.                 .collect(Collectors.toList());
  67.  
  68.         discountPrices = sortedDiscountPrices;
  69.         originalPrices = sortedOriginalPrices;
  70.     }
  71.  
  72.     public String getName() {
  73.         return name;
  74.     }
  75.  
  76.     public double getDiscountInPercent(int a, int b){
  77.         return (100.00*((b-a)/(double)b));
  78.     }
  79.  
  80.     public double getAverageDiscount(){
  81.         double discountSum=0;
  82.         for(int i=0;i<discountPrices.size();i++){
  83.             discountSum += (int) getDiscountInPercent(discountPrices.get(i),originalPrices.get(i));
  84.         }
  85.         return discountSum/discountPrices.size();
  86.     }
  87.  
  88.     public int getTotalDiscount(){
  89.         return (originalPrices.stream().mapToInt(i->i).sum()-discountPrices.stream().mapToInt(i->i).sum());
  90.     }
  91.  
  92.     public static Store create(String string) {
  93.         String[] parts= string.split("\\s+");
  94.         String name= parts[0];
  95.         List<Integer> discountPrices = new ArrayList<>();
  96.         List<Integer> originalPrices = new ArrayList<>();
  97.  
  98.         for(int i=1;i<parts.length;i++){
  99.             String[] prices= parts[i].split(":");
  100.             discountPrices.add(Integer.parseInt(prices[0]));
  101.             originalPrices.add(Integer.parseInt(prices[1]));
  102.         }
  103.         return new Store(name,discountPrices,originalPrices);
  104.     }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         StringBuilder stringBuilder= new StringBuilder();
  109.  
  110.         stringBuilder.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d\n",getName(),getAverageDiscount(),getTotalDiscount()));
  111.         sortByDiscountDescending();
  112.         for(int i=0;i<discountPrices.size();i++){
  113.             stringBuilder.append(String.format("%2d%% %d/%d\n",(int) getDiscountInPercent(discountPrices.get(i),originalPrices.get(i)),
  114.                                                 discountPrices.get(i),originalPrices.get(i)));
  115.         }
  116.         return stringBuilder.toString().trim();
  117.     }
  118.    
  119. }
  120.  
  121. public class DiscountsTest {
  122.     public static void main(String[] args) {
  123.         Discounts discounts = new Discounts();
  124.         int stores = discounts.readStores(System.in);
  125.         System.out.println("Stores read: " + stores);
  126.         System.out.println("=== By average discount ===");
  127.         discounts.byAverageDiscount().forEach(System.out::println);
  128.         System.out.println("=== By total discount ===");
  129.         discounts.byTotalDiscount().forEach(System.out::println);
  130.     }
  131. }
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement