Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.stream.IntStream;
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- import java.util.Comparator;
- class Item implements Comparable<Item> {
- String index;
- String name;
- int price;
- public Item(String index,String name, int price) {
- this.index = index;
- this.name = name;
- this.price = price;
- }
- public String getIndex() {
- return index;
- }
- public String getName() {
- return name;
- }
- public int getPrice() {
- return price;
- }
- public static Item createItem(String string) {
- String[] parts= string.split(";");
- String index = parts[0];
- String name = parts[1];
- int price = Integer.parseInt(parts[2]);
- return new Item(index,name,price);
- }
- @Override
- public String toString() {
- return String.format("%s %d",name,price);
- }
- @Override
- public int compareTo(Item o) {
- return Comparator.comparing(Item::getPrice).reversed().compare(this,o);
- }
- }
- class OnlinePayments {
- List<Item> items;
- Map<String, List<Item>> itemsByStudent;
- public static final Double PROVISION_RATE= 1.14;
- public OnlinePayments() {
- this.items = new ArrayList<>();
- this.itemsByStudent = new TreeMap<>();
- }
- public void readItems(InputStream in) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
- items= bufferedReader.lines().map(Item::createItem).collect(Collectors.toList());
- for(Item i : items){
- itemsByStudent.putIfAbsent(i.getIndex(),new ArrayList<>());
- itemsByStudent.get(i.getIndex()).add(i);
- }
- }
- public void printStudentReport(String id, PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- if (!itemsByStudent.containsKey(id)) {
- printWriter.println(String.format("Student %s not found!", id));
- printWriter.flush();
- return;
- }
- AtomicInteger atomicInteger = new AtomicInteger(1);
- int netPrice = getNetPrice(itemsByStudent.get(id));
- int provision = getProvision(itemsByStudent.get(id));
- printWriter.println(String.format("Student: %s Net: %d Fee: %d Total: %d",id,netPrice,provision,netPrice+provision));
- printWriter.println("Items:");
- // for(Item i : itemsByStudent.get(id)){
- // printWriter.println(String.format("%d. %s",atomicInteger.getAndIncrement(),i.toString()));
- // }
- itemsByStudent.get(id).stream().sorted().forEach(i->printWriter.println(String.format("%d. %s",atomicInteger.getAndIncrement(),i.toString())));
- printWriter.flush();
- }
- private int getProvision(List<Item> items) {
- int provision= (int)Math.round(getNetPrice(items)*PROVISION_RATE/100.0);
- if(provision<3)
- return 3;
- if(provision>300)
- return 300;
- return provision;
- }
- private int getNetPrice(List<Item> items) {
- return items.stream().mapToInt(i->i.getPrice()).sum();
- }
- }
- public class OnlinePaymentsTest {
- public static void main(String[] args) {
- OnlinePayments onlinePayments = new OnlinePayments();
- onlinePayments.readItems(System.in);
- IntStream.range(151020, 151025).mapToObj(String::valueOf).forEach(id -> onlinePayments.printStudentReport(id, System.out));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement