Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.stream.IntStream;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- class Discounts {
- List<Store> stores;
- public Discounts() {
- this.stores = new ArrayList<>();
- }
- public int readStores(InputStream in) {
- BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(in));
- stores= bufferedReader.lines().map(Store::create).collect(Collectors.toList());
- return stores.toArray().length;
- }
- public List<Store> byAverageDiscount(){
- Comparator<Store> comparatorAverage= Comparator.comparing(Store::getAverageDiscount).reversed().thenComparing(Store::getName);
- return stores.stream().sorted(comparatorAverage).limit(3).collect(Collectors.toList());
- }
- public List<Store> byTotalDiscount(){
- Comparator<Store> comparatorTotal= Comparator.comparing(Store::getTotalDiscount).thenComparing(Store::getName);
- return stores.stream().sorted(comparatorTotal).limit(3).collect(Collectors.toList());
- }
- }
- class Store{
- String name;
- List<Integer> discountPrices;
- List<Integer> originalPrices;
- public Store(String name, List<Integer> discountPrices, List<Integer> originalPrices) {
- this.name = name;
- this.discountPrices = discountPrices;
- this.originalPrices = originalPrices;
- }
- public void sortByDiscountDescending() {
- List<Integer> indices = IntStream.range(0, discountPrices.size())
- .boxed()
- .sorted(Comparator.comparingInt(i ->
- (int)getDiscountInPercent(discountPrices.get((int)i), originalPrices.get((int)i)))
- .thenComparingInt(i ->
- discountPrices.get((int)i))
- //getDiscountInPrice(discountPrices.get((int)i), originalPrices.get((int)i)))
- .reversed())
- .collect(Collectors.toList());
- List<Integer> sortedDiscountPrices = indices.stream()
- .map(discountPrices::get)
- .collect(Collectors.toList());
- List<Integer> sortedOriginalPrices = indices.stream()
- .map(originalPrices::get)
- .collect(Collectors.toList());
- discountPrices = sortedDiscountPrices;
- originalPrices = sortedOriginalPrices;
- }
- public String getName() {
- return name;
- }
- public double getDiscountInPercent(int a, int b){
- return (100.00*((b-a)/(double)b));
- }
- public double getAverageDiscount(){
- double discountSum=0;
- for(int i=0;i<discountPrices.size();i++){
- discountSum += (int) getDiscountInPercent(discountPrices.get(i),originalPrices.get(i));
- }
- return discountSum/discountPrices.size();
- }
- public int getTotalDiscount(){
- return (originalPrices.stream().mapToInt(i->i).sum()-discountPrices.stream().mapToInt(i->i).sum());
- }
- public static Store create(String string) {
- String[] parts= string.split("\\s+");
- String name= parts[0];
- List<Integer> discountPrices = new ArrayList<>();
- List<Integer> originalPrices = new ArrayList<>();
- for(int i=1;i<parts.length;i++){
- String[] prices= parts[i].split(":");
- discountPrices.add(Integer.parseInt(prices[0]));
- originalPrices.add(Integer.parseInt(prices[1]));
- }
- return new Store(name,discountPrices,originalPrices);
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder= new StringBuilder();
- stringBuilder.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d\n",getName(),getAverageDiscount(),getTotalDiscount()));
- sortByDiscountDescending();
- for(int i=0;i<discountPrices.size();i++){
- stringBuilder.append(String.format("%2d%% %d/%d\n",(int) getDiscountInPercent(discountPrices.get(i),originalPrices.get(i)),
- discountPrices.get(i),originalPrices.get(i)));
- }
- return stringBuilder.toString().trim();
- }
- }
- public class DiscountsTest {
- public static void main(String[] args) {
- Discounts discounts = new Discounts();
- int stores = discounts.readStores(System.in);
- System.out.println("Stores read: " + stores);
- System.out.println("=== By average discount ===");
- discounts.byAverageDiscount().forEach(System.out::println);
- System.out.println("=== By total discount ===");
- discounts.byTotalDiscount().forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement