Advertisement
dzocesrce

[NP] Mobile Operator

Apr 13th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. /**
  2. INPUT:
  3. 300 100000 M 210 20 10 2000000 S 400 15 15
  4. 31 1002000 S 200 20 12 2000000 S 330 10 15
  5. 303 1040000 M 500 24 10 2000000 S 600 25 7
  6. 340 1053000 M 200 20 10 2000000 M 320 45 8
  7. OUTPUT:
  8. 100000 is not a valid customer ID.
  9. 31 is not a valid rep ID.
  10. ---- PRINTING FINAL REPORTS FOR SALES REPRESENTATIVES----
  11. 303 2 2600.00 2150.00 3050.00 272.50
  12. 340 2 1190.00 950.00 1430.00 166.60
  13. */
  14.  
  15. import java.util.ArrayList;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. import java.io.*;
  19. import java.util.ArrayList;
  20. import java.util.Comparator;
  21. import java.util.List;
  22. import java.util.Objects;
  23. import java.util.stream.Collectors;
  24.  
  25. abstract class Customer implements Comparable<Customer>{
  26.     private long customerID;
  27.     private double minutes;
  28.     private int SMSs;
  29.     private double GBs;
  30.  
  31.     public Customer(long customerID, double minutes, int SMSs, double GBs) {
  32.         this.customerID= customerID;
  33.         this.minutes = minutes;
  34.         this.SMSs = SMSs;
  35.         this.GBs = GBs;
  36.     }
  37.  
  38.     public long getCustomerID() {
  39.         return customerID;
  40.     }
  41.  
  42.     public double getMinutes() {
  43.         return minutes;
  44.     }
  45.  
  46.     public int getSMSs() {
  47.         return SMSs;
  48.     }
  49.  
  50.     public double getGBs() {
  51.         return GBs;
  52.     }
  53.  
  54.     public abstract double totalComission();
  55.  
  56.     public abstract double totalPrice();
  57.  
  58.     @Override
  59.     public String toString() {
  60.         return "Customer{" +
  61.                 "customerID=" + customerID +
  62.                 ", minutes=" + minutes +
  63.                 ", SMSs=" + SMSs +
  64.                 ", GBs=" + GBs +
  65.                 '}';
  66.     }
  67. }
  68.  
  69. class SCustomer extends Customer{
  70.     public static double COMISSION_RATE_S=0.04;
  71.  
  72.     public static int FREE_MINUTES_S=100;
  73.     public static int FREE_SMSs_S=50;
  74.     public static int FREE_GBs_S=5;
  75.     public static int BASE_PRICE_S=500;
  76.  
  77.     public static int MINUTE_PENALTY_S=5;
  78.     public static int SMS_PENALTY_S=6;
  79.     public static int GB_PENALTY_S=25;
  80.  
  81.     public SCustomer(long customerID, double minutes, int SMSs, double GBs) {
  82.         super(customerID, minutes, SMSs, GBs);
  83.     }
  84.  
  85.     @Override
  86.     public double totalComission() {
  87.         return totalPrice()*COMISSION_RATE_S;
  88.     }
  89.  
  90.     @Override
  91.     public double totalPrice() {
  92.         return BASE_PRICE_S+Math.max(0,super.getMinutes()-FREE_MINUTES_S)*MINUTE_PENALTY_S+
  93.                 Math.max(0,super.getSMSs()-FREE_SMSs_S)*SMS_PENALTY_S+
  94.                 Math.max(0,super.getGBs()-FREE_GBs_S)*GB_PENALTY_S;
  95.     }
  96.  
  97.     @Override
  98.     public int compareTo(Customer o) {
  99.         return Double.compare(this.totalPrice(),o.totalPrice());
  100.     }
  101. }
  102.  
  103. class MCustomer extends Customer{
  104.     public static double COMISSION_RATE_M=0.07;
  105.  
  106.     public static int FREE_MINUTES_M=150;
  107.     public static int FREE_SMSs_M=60;
  108.     public static int FREE_GBs_M=10;
  109.     public static int BASE_PRICE_M=750;
  110.  
  111.     public static int MINUTE_PENALTY_M=4;
  112.     public static int SMS_PENALTY_M=4;
  113.     public static int GB_PENALTY_M=20;
  114.  
  115.     public MCustomer(long customerID, double minutes, int SMSs, double GBs) {
  116.         super(customerID, minutes, SMSs, GBs);
  117.     }
  118.  
  119.     @Override
  120.     public double totalComission() {
  121.         return totalPrice()*COMISSION_RATE_M;
  122.     }
  123.  
  124.     @Override
  125.     public double totalPrice() {
  126.         return BASE_PRICE_M+Math.max(0,super.getMinutes()-FREE_MINUTES_M)*MINUTE_PENALTY_M+
  127.                 Math.max(0,super.getSMSs()-FREE_SMSs_M)*SMS_PENALTY_M+
  128.                 Math.max(0,super.getGBs()-FREE_GBs_M)*GB_PENALTY_M;
  129.     }
  130.  
  131.     @Override
  132.     public int compareTo(Customer o) {
  133.         return Double.compare(this.totalPrice(),o.totalPrice());
  134.     }
  135. }
  136.  
  137. class MobileOperator {
  138.     List<SalesRep> salesReps;
  139.  
  140.     public MobileOperator() {
  141.     }
  142.  
  143.     public void readSalesRepData(InputStream in) {
  144.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
  145.  
  146.         salesReps = bufferedReader.lines()
  147.                 .map(i-> {
  148.                     try {
  149.                         return SalesRep.create(i);
  150.                     } catch (InvalidIdException e) {
  151.                         System.out.println(e.getMessage());
  152.                         return null;
  153.                     }
  154.                 })
  155.                 .filter(Objects::nonNull)
  156.                 .collect(Collectors.toList());
  157.     }
  158.  
  159.     public void printSalesReport(PrintStream out) {
  160.  
  161.         PrintWriter printWriter = new PrintWriter(out);
  162.         salesReps.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
  163.         printWriter.flush();
  164.     }
  165. }
  166.  
  167. class InvalidIdException extends Exception{
  168.     public InvalidIdException(String message) {
  169.         super(message);
  170.     }
  171. }
  172.  
  173. class SalesRep implements Comparable<SalesRep>{
  174.     private String salesID;
  175.     private List<Customer> customers;
  176.  
  177.     public SalesRep(String salesID,List<Customer> customers) {
  178.         this.salesID = salesID;
  179.         this.customers= customers;
  180.     }
  181.  
  182.     public static boolean checkValidSalesId(String s) throws InvalidIdException {
  183.         if(s.length()!=3)
  184.             throw new InvalidIdException(String.format("%s is not a valid rep ID.",s));
  185.         for(char c : s.toCharArray()){
  186.             if(!Character.isDigit(c))
  187.                 throw new InvalidIdException(String.format("%s is not a valid rep ID.",s));
  188.         }
  189.         return true;
  190.     }
  191.  
  192.     public static boolean checkValidCustomerId(Long l) throws InvalidIdException {
  193.         if(l<1000000 || l>9999999)
  194.             throw new InvalidIdException(String.format("%d is not a valid customer ID.",l));
  195.         return true;
  196.     }
  197.  
  198.     public static SalesRep create(String s) throws InvalidIdException {
  199.  
  200.         String[] parts = s.split("\\s+");
  201.         String salesID= parts[0];
  202.         List<Customer> customers = new ArrayList<>();
  203.         if(!checkValidSalesId(salesID))
  204.             throw new InvalidIdException(String.format("%s is not a valid rep ID.",salesID));
  205.         for(int i=1;i< parts.length;i+=5){
  206.  
  207.             Long customerID= Long.parseLong(parts[i]);
  208.             String type = parts[i+1];
  209.             double minutes = Double.parseDouble(parts[i+2]);
  210.             int SMSs = Integer.parseInt(parts[i+3]);
  211.             double GBs = Double.parseDouble(parts[i+4]);
  212.             if(!checkValidCustomerId(customerID))
  213.                 throw new InvalidIdException(String.format("%d is not a valid customer ID.",customerID));
  214.             if(type.equals("M"))
  215.                 customers.add(new MCustomer(customerID,minutes,SMSs,GBs));
  216.             else
  217.                 customers.add(new SCustomer(customerID,minutes,SMSs,GBs));
  218.         }
  219.         return new SalesRep(salesID,customers);
  220.     }
  221.  
  222.     public int numberOfBills(){
  223.         return customers.size();
  224.     }
  225.  
  226.     public double minBill(){
  227.         return customers.stream().max(Comparator.reverseOrder()).get().totalPrice();
  228.     }
  229.  
  230.     public double maxBill(){
  231.         return customers.stream().min(Comparator.reverseOrder()).get().totalPrice();
  232.     }
  233.  
  234.     public double averageBill(){
  235.         return customers.stream().mapToDouble(i->i.totalPrice()).sum()/numberOfBills();
  236.     }
  237.  
  238.     public double totalComissionOfCustomers(){
  239.         return customers.stream().mapToDouble(i->i.totalComission()).sum();
  240.     }
  241.  
  242.     @Override
  243.     public String toString() {
  244.         return String.format("%s %d %.2f %.2f %.2f %.2f",salesID,numberOfBills(),averageBill(),minBill(),maxBill(),totalComissionOfCustomers());
  245.  
  246.     }
  247.  
  248.  
  249.     @Override
  250.     public int compareTo(SalesRep o) {
  251.         return Double.compare(this.totalComissionOfCustomers(),o.totalComissionOfCustomers());
  252.     }
  253. }
  254.  
  255. public class MobileOperatorTest {
  256.     public static void main(String[] args) {
  257.  
  258.         MobileOperator mobileOperator = new MobileOperator();
  259.         System.out.println("--- READINGOF THE SALES REPORTS ----");
  260.         mobileOperator.readSalesRepData(System.in);
  261.         System.out.println("---- PRINTING FINAL REPORTS FOR SALES REPRESENTATIVES---- ");
  262.         mobileOperator.printSalesReport(System.out);
  263.     }
  264. }
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement