Advertisement
dzocesrce

[NP] Online Payments

Apr 24th, 2025
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.util.stream.IntStream;
  2. import java.io.InputStream;
  3. import java.io.PrintStream;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9. import java.util.concurrent.atomic.AtomicInteger;
  10. import java.util.stream.Collectors;
  11. import java.util.Comparator;
  12.  
  13. class Item implements Comparable<Item> {
  14.     String index;
  15.     String name;
  16.     int price;
  17.  
  18.     public Item(String index,String name, int price) {
  19.         this.index = index;
  20.         this.name = name;
  21.         this.price = price;
  22.     }
  23.  
  24.     public String getIndex() {
  25.         return index;
  26.     }
  27.  
  28.     public String getName() {
  29.         return name;
  30.     }
  31.  
  32.     public int getPrice() {
  33.         return price;
  34.     }
  35.  
  36.     public static Item createItem(String string) {
  37.         String[] parts= string.split(";");
  38.         String index = parts[0];
  39.         String name = parts[1];
  40.         int price = Integer.parseInt(parts[2]);
  41.  
  42.         return new Item(index,name,price);
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public String toString() {
  48.         return String.format("%s %d",name,price);
  49.     }
  50.  
  51.  
  52.     @Override
  53.     public int compareTo(Item o) {
  54.         return Comparator.comparing(Item::getPrice).reversed().compare(this,o);
  55.     }
  56. }
  57.  
  58. class OnlinePayments {
  59.     List<Item> items;
  60.     Map<String, List<Item>> itemsByStudent;
  61.     public static final Double PROVISION_RATE= 1.14;
  62.  
  63.     public OnlinePayments() {
  64.         this.items = new ArrayList<>();
  65.         this.itemsByStudent = new TreeMap<>();
  66.     }
  67.  
  68.     public void readItems(InputStream in) {
  69.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
  70.         items= bufferedReader.lines().map(Item::createItem).collect(Collectors.toList());
  71.         for(Item i : items){
  72.             itemsByStudent.putIfAbsent(i.getIndex(),new ArrayList<>());
  73.             itemsByStudent.get(i.getIndex()).add(i);
  74.         }
  75.     }
  76.  
  77.     public void printStudentReport(String id, PrintStream out) {
  78.         PrintWriter printWriter = new PrintWriter(out);
  79.         if (!itemsByStudent.containsKey(id)) {
  80.             printWriter.println(String.format("Student %s not found!", id));
  81.             printWriter.flush();
  82.             return;
  83.         }
  84.         AtomicInteger atomicInteger = new AtomicInteger(1);
  85.         int netPrice = getNetPrice(itemsByStudent.get(id));
  86.         int provision = getProvision(itemsByStudent.get(id));
  87.         printWriter.println(String.format("Student: %s Net: %d Fee: %d Total: %d",id,netPrice,provision,netPrice+provision));
  88.         printWriter.println("Items:");
  89. //        for(Item i : itemsByStudent.get(id)){
  90. //            printWriter.println(String.format("%d. %s",atomicInteger.getAndIncrement(),i.toString()));
  91. //        }
  92.         itemsByStudent.get(id).stream().sorted().forEach(i->printWriter.println(String.format("%d. %s",atomicInteger.getAndIncrement(),i.toString())));
  93.         printWriter.flush();
  94.     }
  95.  
  96.     private int getProvision(List<Item> items) {
  97.         int provision= (int)Math.round(getNetPrice(items)*PROVISION_RATE/100.0);
  98.         if(provision<3)
  99.             return 3;
  100.         if(provision>300)
  101.             return 300;
  102.         return provision;
  103.     }
  104.  
  105.     private int getNetPrice(List<Item> items) {
  106.         return items.stream().mapToInt(i->i.getPrice()).sum();
  107.     }
  108. }
  109.  
  110.  
  111. public class OnlinePaymentsTest {
  112.     public static void main(String[] args) {
  113.         OnlinePayments onlinePayments = new OnlinePayments();
  114.  
  115.         onlinePayments.readItems(System.in);
  116.  
  117.         IntStream.range(151020, 151025).mapToObj(String::valueOf).forEach(id -> onlinePayments.printStudentReport(id, System.out));
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement