Advertisement
Martina312

[НП] - Discounts

Aug 23rd, 2020
1,976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. class StoreItem{
  9.     int discount;
  10.     int regular;
  11.     int percentage;
  12.  
  13.     public StoreItem(int discount, int regular, int percentage) {
  14.         this.discount = discount;
  15.         this.regular = regular;
  16.         this.percentage = percentage;
  17.     }
  18.  
  19.     @Override
  20.     public String toString() {
  21.         return String.format("%2d%% %d/%d", percentage, discount, regular);
  22.     }
  23.  
  24.     public int getDiscount() {
  25.         return discount;
  26.     }
  27.  
  28.     public int getRegular() {
  29.         return regular;
  30.     }
  31.  
  32.     public int getPercentage() {
  33.         return percentage;
  34.     }
  35. }
  36.  
  37. class Store{
  38.     private String name;
  39.     private List<Integer> discountPrices;
  40.     private List<Integer> regularPrices;
  41.  
  42.     public Store(String name, List<Integer> discountPrices, List<Integer> regularPrices) {
  43.         this.name = name;
  44.         this.discountPrices = discountPrices;
  45.         this.regularPrices = regularPrices;
  46.     }
  47.  
  48.     public String getName() {
  49.         return name;
  50.     }
  51.  
  52.     public int totalDiscount(){
  53.         int sum = 0;
  54.         for(int i=0; i<discountPrices.size(); i++){
  55.             sum = sum + (regularPrices.get(i) - discountPrices.get(i));
  56.         }
  57.         return sum;
  58.     }
  59.  
  60.     public float averageDiscount(){
  61.         int sum = 0;
  62.         for (int i=0; i<discountPrices.size(); i++){
  63.             sum += getPercentage(discountPrices.get(i), regularPrices.get(i));
  64.         }
  65.         return (float) (sum*1.0 / discountPrices.size());
  66.     }
  67.  
  68.     public int getPercentage(int discount, int regular){
  69.         return (int) (100 - (((float) discount / regular) * 100));
  70.     }
  71.  
  72.     @Override
  73.     public String toString() {
  74.         StringBuilder sb = new StringBuilder();
  75.         sb.append(name+"\n");
  76.         sb.append(String.format("Average discount: %.1f%%\n", averageDiscount()));
  77.         sb.append("Total discount: "+totalDiscount()+"\n");
  78.         List<StoreItem> items = new ArrayList<>();
  79.         for (int i=0; i<discountPrices.size(); i++){
  80.             items.add(new StoreItem(discountPrices.get(i), regularPrices.get(i), getPercentage(discountPrices.get(i), regularPrices.get(i))));
  81.         }
  82.         items.sort(Comparator.comparing(StoreItem::getPercentage).thenComparing(StoreItem::getDiscount).reversed());
  83.         for (int i = 0; i< items.size(); i++){
  84.            if (i==items.size()-1)
  85.                sb.append(items.get(i));
  86.            else
  87.                sb.append(items.get(i)+"\n");
  88.         }
  89.         return sb.toString();
  90.     }
  91. }
  92.  
  93. class Discounts{
  94.     List<Store> stores;
  95.  
  96.     public Discounts() {
  97.         this.stores = new ArrayList<>();
  98.     }
  99.  
  100.     public int readStores(InputStream inputStream){
  101.         Scanner in = new Scanner(inputStream);
  102.  
  103.         while (in.hasNextLine()){
  104.             String line = in.nextLine();
  105.             String [] parts = line.split("\\s+");
  106.  
  107.             String name = parts[0];
  108.             List<Integer> discountPrices = new ArrayList<>();
  109.             List<Integer> regularPrices = new ArrayList<>();
  110.  
  111.             for(int i=1; i<parts.length; i++){
  112.                 String [] priceParts = parts[i].split(":");
  113.                 discountPrices.add(Integer.parseInt(priceParts[0]));
  114.                 regularPrices.add(Integer.parseInt(priceParts[1]));
  115.             }
  116.             stores.add(new Store(name, discountPrices, regularPrices));
  117.         }
  118.         return stores.size();
  119.     }
  120.  
  121.     public List<Store> byTotalDiscount(){
  122.         stores.sort(Comparator.comparing(Store::totalDiscount).thenComparing(Store::getName));
  123.         return stores.stream().limit(3).collect(Collectors.toList());
  124.     }
  125.  
  126.     public List<Store> byAverageDiscount(){
  127.         stores.sort(Comparator.comparing(Store::averageDiscount).reversed().thenComparing(Store::getName));
  128.         return stores.stream().limit(3).collect(Collectors.toList());
  129.     }
  130.  
  131.     @Override
  132.     public String toString() {
  133.         StringBuilder sb = new StringBuilder();
  134.         stores.forEach(sb::append);
  135.         return sb.toString();
  136.     }
  137. }
  138. public class DiscountsTest {
  139.     public static void main(String[] args) {
  140.         Discounts discounts = new Discounts();
  141.         int stores = discounts.readStores(System.in);
  142.         System.out.println("Stores read: " + stores);
  143.         System.out.println("=== By average discount ===");
  144.         discounts.byAverageDiscount().forEach(System.out::println);
  145.         System.out.println("=== By total discount ===");
  146.         discounts.byTotalDiscount().forEach(System.out::println);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement