Advertisement
Martina312

[НП] - Мој ДДВ 1

Aug 22nd, 2020
2,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. class AmountNotAllowedException extends Exception{
  8.     public AmountNotAllowedException(String message) {
  9.         super(message);
  10.     }
  11. }
  12.  
  13. class Product{
  14.     private int price;
  15.     private char taxType;
  16.  
  17.     public Product(int price, char taxType) {
  18.         this.price = price;
  19.         this.taxType = taxType;
  20.     }
  21.  
  22.     public double tax(){
  23.         if (taxType == 'A')
  24.             return price*0.18*0.15;
  25.         else if (taxType == 'B')
  26.             return price*0.05*0.15;
  27.         else return 0;
  28.     }
  29.  
  30.     public int getPrice() {
  31.         return price;
  32.     }
  33.  
  34.     public char getTaxType() {
  35.         return taxType;
  36.     }
  37. }
  38.  
  39. class Receipt{
  40.     private String id;
  41.     private List<Product> products;
  42.  
  43.     public Receipt(String id, List<Product> products) throws AmountNotAllowedException {
  44.         this.id = id;
  45.         this.products = products;
  46.         if (sum() > 30000)
  47.             throw new AmountNotAllowedException("Receipt with amount "+sum()+" is not allowed to be scanned");
  48.     }
  49.  
  50.     public int sum (){
  51.         return products.stream().mapToInt(Product::getPrice).sum();
  52.     }
  53.  
  54.     public double taxReturn(){
  55.         return products.stream().mapToDouble(Product::tax).sum();
  56.     }
  57.  
  58.     @Override
  59.     public String toString() {
  60.         return String.format("%s %d %.2f", id, sum(), taxReturn());
  61.     }
  62. }
  63.  
  64. class MojDDV{
  65.     private List<Receipt> fiskalni;
  66.  
  67.     public MojDDV() {
  68.         this.fiskalni = new ArrayList<>();
  69.     }
  70.  
  71.     public void readRecords (InputStream inputStream){
  72.         Scanner in = new Scanner(inputStream);
  73.  
  74.         while (in.hasNextLine()){
  75.             String line = in.nextLine();
  76.             String [] parts = line.split("\\s+");
  77.  
  78.             String id = parts[0];
  79.             ArrayList<Product> products = new ArrayList<>();
  80.             for (int i=1; i<parts.length; i+=2){
  81.                 int price = Integer.parseInt(parts[i]);
  82.                 char taxType = parts[i+1].charAt(0);
  83.                 products.add(new Product(price,taxType));
  84.             }
  85.             try {
  86.                 Receipt r = new Receipt(id, products);
  87.                 fiskalni.add(r);
  88.             } catch (AmountNotAllowedException e) {
  89.                 System.out.println(e.getMessage());
  90.             }
  91.         }
  92.     }
  93.  
  94.     public void printTaxReturns(OutputStream outputStream){
  95.         fiskalni.forEach(System.out::println);
  96.     }
  97. }
  98. public class MojDDVTest {
  99.  
  100.     public static void main(String[] args) throws AmountNotAllowedException {
  101.  
  102.         MojDDV mojDDV = new MojDDV();
  103.  
  104.         System.out.println("===READING RECORDS FROM INPUT STREAM===");
  105.         mojDDV.readRecords(System.in);
  106.  
  107.         System.out.println("===PRINTING TAX RETURNS RECORDS TO OUTPUT STREAM ===");
  108.         mojDDV.printTaxReturns(System.out);
  109.  
  110.         //System.out.println("===PRINTING SUMMARY STATISTICS FOR TAX RETURNS TO OUTPUT STREAM===");
  111.         //mojDDV.printStatistics(System.out);
  112.  
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement