Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.*;
- import java.io.*;
- 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);
- }
- }
- class Price {
- private int discount;
- private int price;
- public Price (String discount, String price) {
- this.discount = Integer.parseInt(discount);
- this.price = Integer.parseInt(price);
- }
- public int getAbsolute () {
- return price - discount;
- }
- public double getAverage () {
- return ((price - discount) * 100) / price;
- }
- public String toString () {
- return String.format("%2d%% %d/%d\n", (int)getAverage(), discount, price);
- }
- }
- class Store {
- private String name;
- private List <Price> prices;
- public Store (String name, List <Price> prices) {
- this.name = name;
- this.prices = prices;
- }
- public int Absolute () {
- return prices.stream().mapToInt(price -> price.getAbsolute()).sum();
- }
- public double Average () {
- return prices.stream().mapToDouble(price -> price.getAverage()).average().orElse(0);
- }
- static Store fromString (String s) {
- List <Price> list = new ArrayList<> ();
- String [] parts = s.split("\\s++");
- IntStream.range(1, parts.length)
- .forEach(i -> {
- String [] newPart = parts[i].split(":");
- Price price = new Price (newPart[0], newPart[1]);
- list.add(price);
- });
- return new Store (parts[0], list);
- }
- public String getName () {
- return name;
- }
- public String toString () {
- StringBuilder builder = new StringBuilder ();
- builder.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d\n",
- name, Average(), Absolute()));
- prices.stream()
- .sorted(Comparator.comparing(Price::getAverage)
- .thenComparing(Price::getAbsolute).reversed())
- .forEach(each -> builder.append(each));
- builder.setLength(builder.length() - 1);
- return builder.toString();
- }
- }
- class Discounts {
- private List <Store> list;
- public Discounts () {
- list = new ArrayList <> ();
- }
- public int readStores (InputStream inputStream) {
- BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream));
- list = reader.lines()
- .map(Store::fromString)
- .collect(Collectors.toList());
- return list.size();
- }
- public List<Store> byAverageDiscount () {
- return list.stream()
- .sorted(Comparator.comparing(Store::Average).reversed()
- .thenComparing(Store::getName))
- .limit(3)
- .collect(Collectors.toList());
- }
- public List <Store> byTotalDiscount () {
- return list.stream()
- .sorted(Comparator.comparing(Store::Absolute)
- .thenComparing(Store::getName))
- .limit(3)
- .collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment