dzocesrce

[NP] MojDDV

Apr 12th, 2025
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.05 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.DoubleSummaryStatistics;
  4. import java.util.List;
  5. import java.util.Objects;
  6. import java.util.stream.Collectors;
  7. import java.util.Arrays;
  8. import java.util.Comparator;
  9.  
  10.  
  11. public class MojDDVTest {
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         MojDDV mojDDV = new MojDDV();
  16.  
  17.         //In IntelliJ we must use CTRL+D in order to break the INPUT STREAM.
  18.         System.out.println("===READING RECORDS FROM INPUT STREAM===");
  19.         mojDDV.readRecords(System.in);
  20.  
  21.         System.out.println("===PRINTING TAX RETURNS RECORDS TO OUTPUT STREAM ===");
  22.         mojDDV.printTaxReturns(System.out);
  23.  
  24.         System.out.println("===PRINTING SUMMARY STATISTICS FOR TAX RETURNS TO OUTPUT STREAM===");
  25.         mojDDV.printStatistics(System.out);
  26.  
  27.     }
  28. }
  29.  
  30. class MojDDV {
  31.  
  32.     private List<Receipt> receipts;
  33.  
  34.     public MojDDV() {
  35.         receipts= new ArrayList<Receipt>();
  36.     }
  37.  
  38.     //for every line we try to make a receipt, if it throws an exception we simply mark it as null.
  39.     //then we filter out the null objects and collect them into a list, which represents the receipts of the actual MojDDV object.
  40.     public void readRecords(InputStream in) {
  41.         this.receipts = new BufferedReader(new InputStreamReader(in))
  42.                 .lines()
  43.                 .map(i -> {
  44.                     try {
  45.                         return Receipt.createReceipt(i);
  46.                     } catch (AmountNotAllowedException e) {
  47.                         System.out.println(e.getMessage());
  48.                         return null;
  49.                     }
  50.                 })
  51.                 .filter(Objects::nonNull)
  52.                 .collect(Collectors.toList());
  53.  
  54.  
  55.     }
  56.  
  57.     //First we sort the receipts and then we print every single one.
  58.     public void printTaxReturns(OutputStream out) {
  59.  
  60.         PrintWriter printWriter = new PrintWriter(out);
  61.  
  62.         this.receipts.stream().
  63.                // sorted().
  64.                 forEach(i -> printWriter.println(i));
  65.  
  66.         printWriter.flush();
  67.     }
  68.  
  69.     //if we need statistics such as min,max,avg... we can use the
  70.     // DoubleSummaryStatistcs class that generates them for our data.
  71.     public void printStatistics(OutputStream out) {
  72.         PrintWriter printWriter = new PrintWriter(out);
  73.         DoubleSummaryStatistics summaryStatistics = receipts.stream()
  74.                         .mapToDouble(i-> i.taxReturns())
  75.                                 .summaryStatistics();
  76.  
  77.         printWriter.format("min:\t%.3f\nmax:\t%.3f\nsum:\t%.3f\ncount:\t%d\navg:\t%.3f\n",
  78.                 summaryStatistics.getMin(),
  79.                 summaryStatistics.getMax(),
  80.                 summaryStatistics.getSum(),
  81.                 summaryStatistics.getCount(),
  82.                 summaryStatistics.getAverage());
  83.  
  84.         printWriter.flush();
  85.     }
  86. }
  87.  
  88. class Receipt implements Comparable<Receipt> {
  89.     private long id;
  90.     private List<Item> items;
  91.  
  92.     public Receipt(long id) {
  93.         this.id = id;
  94.         this.items = new ArrayList<>();
  95.     }
  96.  
  97.     public Receipt(long id, List<Item> items) {
  98.         this.id = id;
  99.         this.items = items;
  100.     }
  101.  
  102.     //We create objects through this method where we cut down the input line.
  103.     //We through an exception which we try and match in the readData method.
  104.     public static Receipt createReceipt(String line) throws AmountNotAllowedException {
  105.     String[] parts= line.split("\\s+");
  106.     long id= Long.parseLong(parts[0]);
  107.  
  108.     List<Item> items = new ArrayList<>();
  109.  
  110.         Arrays.stream(parts)
  111.                 .skip(1)
  112.                 .forEach(i -> {
  113.                     if(Character.isDigit(i.charAt(0))){
  114.                         items.add(new Item(Integer.parseInt(i)));
  115.                     }
  116.                     else{
  117.                         items.get(items.size()-1).setTaxType(TaxType.valueOf(i));
  118.                     }
  119.                 });
  120.         if(totalAmount(items)>30000)
  121.             throw new AmountNotAllowedException(totalAmount(items));
  122.  
  123.         return new Receipt(id,items);
  124.     }
  125.  
  126.     //We get all the items into a stream and map to Int or Double their adequate amount, and then we sum them all.
  127.     public static int totalAmount(List<Item> items){
  128.         return items.stream().mapToInt(i -> i.getAmount()).sum();
  129.     }
  130.     public int totalAmount(){
  131.         return items.stream().mapToInt(i -> i.getAmount()).sum();
  132.     }
  133.  
  134.     public double taxReturns(){
  135.         return items.stream().mapToDouble(i -> i.getTaxReturn()).sum();
  136.     }
  137.  
  138.     //We compare the order of listing/sorting for 2 receipts, first through their taxReturn,
  139.     // and then through their totalAmount attribute.
  140.     @Override
  141.     public int compareTo(Receipt other) {
  142.         return Comparator.comparing(Receipt::taxReturns)
  143.                 .thenComparing(Receipt::totalAmount)
  144.                 .compare(this,other);
  145.     }
  146.  
  147.     @Override
  148.     public String toString() {
  149.         return String.format("%10d\t%10d\t%10.5f",id,totalAmount(),taxReturns());
  150.     }
  151. }
  152.  
  153. class Item {
  154.     private int amount;
  155.     private TaxType taxType;
  156.     private static double TAX_V=0.0;
  157.     private static double TAX_A=0.18;
  158.     private static double TAX_B=0.05;
  159.  
  160.     public Item(int amount) {
  161.         this.amount = amount;
  162.     }
  163.  
  164.     public double getTaxReturn(){
  165.         if(taxType==TaxType.V)
  166.             return 0.15*amount*TAX_V;
  167.         else if(taxType==TaxType.B)
  168.             return 0.15*amount*TAX_B;
  169.         else
  170.             return 0.15*amount*TAX_A;
  171.     }
  172.  
  173.     public int getAmount() {
  174.         return amount;
  175.     }
  176.  
  177.     public void setAmount(int amount) {
  178.         this.amount = amount;
  179.     }
  180.  
  181.     public TaxType getTaxType() {
  182.         return taxType;
  183.     }
  184.  
  185.     public void setTaxType(TaxType taxType) {
  186.         this.taxType = taxType;
  187.     }
  188. }
  189.  
  190. enum TaxType {
  191.     A,
  192.     B,
  193.     V
  194. }
  195.  
  196. class AmountNotAllowedException extends Exception{
  197.     public AmountNotAllowedException(int totalAmount) {
  198.         super(String.format("Receipt with amount %d is not allowed to be scanned",totalAmount));
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment