Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.util.*;
- import java.util.stream.Collectors;
- class DiscountPercentSorter implements Comparator<Store>{
- @Override
- public int compare(Store o1, Store o2) {
- return o1.getAverageDiscount()>o2.getAverageDiscount()? -1 : o1.getAverageDiscount()<o2.getAverageDiscount()? 1:0;
- }
- }
- class NameSorter implements Comparator<Store>{
- @Override
- public int compare(Store o1, Store o2) {
- return o1.getName().compareTo(o2.getName());
- }
- }
- class TotalDiscount implements Comparator<Store>{
- @Override
- public int compare(Store o1, Store o2) {
- return o1.getTotalDiscount()>o2.getTotalDiscount()? 1 : o1.getTotalDiscount()<o2.getTotalDiscount()? -1:0;
- }
- }
- class PercentageSorter implements Comparator<StoreItem>{
- @Override
- public int compare(StoreItem o1, StoreItem o2) {
- return o1.getDiscountPercentage()>o2.getDiscountPercentage()? -1 : o1.getDiscountPercentage()<o2.getDiscountPercentage()? 1:0;
- }
- }
- class ItemPrice implements Comparator<StoreItem>{
- @Override
- public int compare(StoreItem o1, StoreItem o2) {
- return o1.getDiscountPrice()>o2.getDiscountPrice()? -1 : o1.getDiscountPrice()<o2.getDiscountPrice()? 1:0;
- }
- }
- class StoreItem{
- private int price;
- private int discountPrice;
- public StoreItem(int discountPrice, int price) {
- this.price = price;
- this.discountPrice = discountPrice;
- }
- public int getPrice() {
- return price;
- }
- public int getDiscountPrice() {
- return discountPrice;
- }
- public int getDiff(){
- return price-discountPrice;
- }
- public int getDiscountPercentage(){
- return (int) (100-(100/((float)price/discountPrice)));
- }
- @Override
- public String toString() {
- StringBuilder sb=new StringBuilder();
- sb.append(String.format("\n%2d%% %d/%d",getDiscountPercentage(),discountPrice,price));
- // sb.append("\n"+this.getDiscountPercentage()+"% "+discountPrice+"/"+price);
- return sb.toString();
- }
- }
- class Store{
- private String name;
- private List<StoreItem> items;
- public Store(String name, List<StoreItem> items) {
- this.name = name;
- this.items = items;
- }
- public float getAverageDiscount(){
- float sum=0;
- for(StoreItem i:items){
- sum+=i.getDiscountPercentage();
- }
- return sum/items.size();
- }
- public int getTotalDiscount(){
- int sum=0;
- for(StoreItem i:items){
- sum+=i.getDiff();
- }
- return sum;
- }
- public String getName() {
- return name;
- }
- public String toString(){
- StringBuilder sb=new StringBuilder();
- sb.append(String.format("%s\nAverage discount: %.1f%%\nTotal discount: %d", name,(float)getAverageDiscount(),getTotalDiscount()));
- for(StoreItem si:items) {
- sb.append(si);
- }
- return sb.toString();
- }
- }
- class Discounts{
- private List<Store> stores;
- public Discounts() {
- stores=new ArrayList<>();
- }
- public int readStores(InputStream inputStream){
- Scanner in= new Scanner(inputStream);
- while(in.hasNextLine()){
- String line=in.nextLine();
- String []parts=line.split("\\s+");
- String name=parts[0];
- List<StoreItem> items=new ArrayList<>();
- for(int i=1;i<parts.length;i++){
- String [] priceParts=parts[i].split(":");
- items.add(new StoreItem(Integer.parseInt(priceParts[0]),Integer.parseInt(priceParts[1])));
- }
- items.sort(new PercentageSorter().thenComparing(new ItemPrice()));
- stores.add(new Store(name,items));
- }
- return stores.size();
- }
- public List<Store> byAverageDiscount(){
- Collections.sort(stores,new DiscountPercentSorter().thenComparing(new NameSorter()));
- return stores.stream().limit(3).collect(Collectors.toList());
- }
- public List<Store> byTotalDiscount(){
- Collections.sort(stores, new TotalDiscount().thenComparing(new NameSorter()));
- return stores.stream().limit(3).collect(Collectors.toList());
- }
- public String toString() {
- StringBuilder sb=new StringBuilder();
- for(Store s:stores){
- sb.append(s);
- }
- return sb.toString();
- }
- }
- 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);
- }
- }
- // Vashiot kod ovde
Add Comment
Please, Sign In to add comment