1 year ago

#379364

test-img

alright212

Specific Analysers Have Only Minimal Code java

How do i make my code more compact? I know i should use inheritance, but i am not sure how to use it in this case...most methods are pretty similar, but not exactly...

package inheritance.analyser;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

    public non-sealed class FlatTaxSalesAnalyser extends SalesAnalyser {
    protected FlatTaxSalesAnalyser(List<SalesRecord> records) {
        super(records);
    }

    @Override
    public Double getTotalSales() {
        return records.stream().mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2).sum();
    }

    @Override
    public Double getTotalSalesByProductId(String id) {
        return records.stream().filter(record -> record.getProductId().equals(id)).mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2).sum();
    }

    @Override
    public String getIdOfMostPopularItem() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingInt(SalesRecord::getItemsSold)))
                .entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).get().getKey();
    }

    @Override
    public String getIdOfItemWithLargestTotalSales() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2)))
                .entrySet().stream().max(Comparator.comparingDouble(Map.Entry::getValue)).get().getKey();
    }
}
package inheritance.analyser;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

    public non-sealed class TaxFreeSalesAnalyser extends SalesAnalyser {
    protected TaxFreeSalesAnalyser(List<SalesRecord> records) {
        super(records);
    }

    @Override
    public Double getTotalSales() {
        return records.stream().mapToDouble(record -> record.getProductPrice() * record.getItemsSold()).sum();
    }

    @Override
    public Double getTotalSalesByProductId(String id) {
        return records.stream().filter(record -> record.getProductId().equals(id)).mapToDouble(record -> record.getProductPrice() * record.getItemsSold()).sum();
    }

    @Override
    public String getIdOfMostPopularItem() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingInt(SalesRecord::getItemsSold)))
                .entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).get().getKey();
    }

    @Override
    public String getIdOfItemWithLargestTotalSales() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingDouble(record -> record.getProductPrice() * record.getItemsSold())))
                .entrySet().stream().max(Comparator.comparingDouble(Map.Entry::getValue)).get().getKey();
    }
}
package inheritance.analyser;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


    public non-sealed class DifferentiatedTaxSalesAnalyser extends SalesAnalyser {
    protected DifferentiatedTaxSalesAnalyser(List<SalesRecord> records) {
        super(records);
    }

    @Override
    public Double getTotalSales() {
          return records.stream().mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / (record.hasReducedRate() ? 1.1 : 1.2)).sum();
    }

    @Override
    public Double getTotalSalesByProductId(String id) {

        return records.stream().filter(record -> record.getProductId().equals(id)).mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / (record.hasReducedRate() ? 1.1 : 1.2)).sum();
    }

    @Override
    public String getIdOfMostPopularItem() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingInt(SalesRecord::getItemsSold)))
                .entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).get().getKey();
    }

    @Override
    public String getIdOfItemWithLargestTotalSales() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingDouble(record -> record.getProductPrice() * record.getItemsSold() / (record.hasReducedRate() ? 1.1 : 1.2))))
                .entrySet().stream().max(Comparator.comparingDouble(Map.Entry::getValue)).get().getKey();
    }
}

This should be my superclass:

package inheritance.analyser;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract sealed class SalesAnalyser implements AbstractSalesAnalyser permits DifferentiatedTaxSalesAnalyser, FlatTaxSalesAnalyser, TaxFreeSalesAnalyser {

    protected List<SalesRecord> records;

    public SalesAnalyser(List<SalesRecord> records) {
        this.records = records;
    }

    @Override
    public Double getTotalSales() {
        return records.stream().mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2).sum();
    }

    @Override
    public Double getTotalSalesByProductId(String id) {
        return records.stream().filter(record -> record.getProductId().equals(id)).mapToDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2).sum();
    }


    @Override
    public String getIdOfMostPopularItem() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingInt(SalesRecord::getItemsSold)))
                .entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).get().getKey();
    }

    @Override
    public String getIdOfItemWithLargestTotalSales() {
        return records.stream().collect(Collectors.groupingBy(SalesRecord::getProductId, Collectors.summingDouble(record -> record.getProductPrice() * record.getItemsSold() / 1.2)))
                .entrySet().stream().max(Comparator.comparingDouble(Map.Entry::getValue)).get().getKey();
    }
}

Since my calculator will have a large number of common parts, make them a common abstract superclass into which this common part goes. There should be only "protected" methods with minimal code in specific classes. The abstract superclass must be closed (sealed).

java

inheritance

abstract-class

superclass

0 Answers

Your Answer

Accepted video resources