Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- INPUT:
- 300 100000 M 210 20 10 2000000 S 400 15 15
- 31 1002000 S 200 20 12 2000000 S 330 10 15
- 303 1040000 M 500 24 10 2000000 S 600 25 7
- 340 1053000 M 200 20 10 2000000 M 320 45 8
- OUTPUT:
- 100000 is not a valid customer ID.
- 31 is not a valid rep ID.
- ---- PRINTING FINAL REPORTS FOR SALES REPRESENTATIVES----
- 303 2 2600.00 2150.00 3050.00 272.50
- 340 2 1190.00 950.00 1430.00 166.60
- */
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Objects;
- import java.util.stream.Collectors;
- abstract class Customer implements Comparable<Customer>{
- private long customerID;
- private double minutes;
- private int SMSs;
- private double GBs;
- public Customer(long customerID, double minutes, int SMSs, double GBs) {
- this.customerID= customerID;
- this.minutes = minutes;
- this.SMSs = SMSs;
- this.GBs = GBs;
- }
- public long getCustomerID() {
- return customerID;
- }
- public double getMinutes() {
- return minutes;
- }
- public int getSMSs() {
- return SMSs;
- }
- public double getGBs() {
- return GBs;
- }
- public abstract double totalComission();
- public abstract double totalPrice();
- @Override
- public String toString() {
- return "Customer{" +
- "customerID=" + customerID +
- ", minutes=" + minutes +
- ", SMSs=" + SMSs +
- ", GBs=" + GBs +
- '}';
- }
- }
- class SCustomer extends Customer{
- public static double COMISSION_RATE_S=0.04;
- public static int FREE_MINUTES_S=100;
- public static int FREE_SMSs_S=50;
- public static int FREE_GBs_S=5;
- public static int BASE_PRICE_S=500;
- public static int MINUTE_PENALTY_S=5;
- public static int SMS_PENALTY_S=6;
- public static int GB_PENALTY_S=25;
- public SCustomer(long customerID, double minutes, int SMSs, double GBs) {
- super(customerID, minutes, SMSs, GBs);
- }
- @Override
- public double totalComission() {
- return totalPrice()*COMISSION_RATE_S;
- }
- @Override
- public double totalPrice() {
- return BASE_PRICE_S+Math.max(0,super.getMinutes()-FREE_MINUTES_S)*MINUTE_PENALTY_S+
- Math.max(0,super.getSMSs()-FREE_SMSs_S)*SMS_PENALTY_S+
- Math.max(0,super.getGBs()-FREE_GBs_S)*GB_PENALTY_S;
- }
- @Override
- public int compareTo(Customer o) {
- return Double.compare(this.totalPrice(),o.totalPrice());
- }
- }
- class MCustomer extends Customer{
- public static double COMISSION_RATE_M=0.07;
- public static int FREE_MINUTES_M=150;
- public static int FREE_SMSs_M=60;
- public static int FREE_GBs_M=10;
- public static int BASE_PRICE_M=750;
- public static int MINUTE_PENALTY_M=4;
- public static int SMS_PENALTY_M=4;
- public static int GB_PENALTY_M=20;
- public MCustomer(long customerID, double minutes, int SMSs, double GBs) {
- super(customerID, minutes, SMSs, GBs);
- }
- @Override
- public double totalComission() {
- return totalPrice()*COMISSION_RATE_M;
- }
- @Override
- public double totalPrice() {
- return BASE_PRICE_M+Math.max(0,super.getMinutes()-FREE_MINUTES_M)*MINUTE_PENALTY_M+
- Math.max(0,super.getSMSs()-FREE_SMSs_M)*SMS_PENALTY_M+
- Math.max(0,super.getGBs()-FREE_GBs_M)*GB_PENALTY_M;
- }
- @Override
- public int compareTo(Customer o) {
- return Double.compare(this.totalPrice(),o.totalPrice());
- }
- }
- class MobileOperator {
- List<SalesRep> salesReps;
- public MobileOperator() {
- }
- public void readSalesRepData(InputStream in) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
- salesReps = bufferedReader.lines()
- .map(i-> {
- try {
- return SalesRep.create(i);
- } catch (InvalidIdException e) {
- System.out.println(e.getMessage());
- return null;
- }
- })
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
- }
- public void printSalesReport(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- salesReps.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
- printWriter.flush();
- }
- }
- class InvalidIdException extends Exception{
- public InvalidIdException(String message) {
- super(message);
- }
- }
- class SalesRep implements Comparable<SalesRep>{
- private String salesID;
- private List<Customer> customers;
- public SalesRep(String salesID,List<Customer> customers) {
- this.salesID = salesID;
- this.customers= customers;
- }
- public static boolean checkValidSalesId(String s) throws InvalidIdException {
- if(s.length()!=3)
- throw new InvalidIdException(String.format("%s is not a valid rep ID.",s));
- for(char c : s.toCharArray()){
- if(!Character.isDigit(c))
- throw new InvalidIdException(String.format("%s is not a valid rep ID.",s));
- }
- return true;
- }
- public static boolean checkValidCustomerId(Long l) throws InvalidIdException {
- if(l<1000000 || l>9999999)
- throw new InvalidIdException(String.format("%d is not a valid customer ID.",l));
- return true;
- }
- public static SalesRep create(String s) throws InvalidIdException {
- String[] parts = s.split("\\s+");
- String salesID= parts[0];
- List<Customer> customers = new ArrayList<>();
- if(!checkValidSalesId(salesID))
- throw new InvalidIdException(String.format("%s is not a valid rep ID.",salesID));
- for(int i=1;i< parts.length;i+=5){
- Long customerID= Long.parseLong(parts[i]);
- String type = parts[i+1];
- double minutes = Double.parseDouble(parts[i+2]);
- int SMSs = Integer.parseInt(parts[i+3]);
- double GBs = Double.parseDouble(parts[i+4]);
- if(!checkValidCustomerId(customerID))
- throw new InvalidIdException(String.format("%d is not a valid customer ID.",customerID));
- if(type.equals("M"))
- customers.add(new MCustomer(customerID,minutes,SMSs,GBs));
- else
- customers.add(new SCustomer(customerID,minutes,SMSs,GBs));
- }
- return new SalesRep(salesID,customers);
- }
- public int numberOfBills(){
- return customers.size();
- }
- public double minBill(){
- return customers.stream().max(Comparator.reverseOrder()).get().totalPrice();
- }
- public double maxBill(){
- return customers.stream().min(Comparator.reverseOrder()).get().totalPrice();
- }
- public double averageBill(){
- return customers.stream().mapToDouble(i->i.totalPrice()).sum()/numberOfBills();
- }
- public double totalComissionOfCustomers(){
- return customers.stream().mapToDouble(i->i.totalComission()).sum();
- }
- @Override
- public String toString() {
- return String.format("%s %d %.2f %.2f %.2f %.2f",salesID,numberOfBills(),averageBill(),minBill(),maxBill(),totalComissionOfCustomers());
- }
- @Override
- public int compareTo(SalesRep o) {
- return Double.compare(this.totalComissionOfCustomers(),o.totalComissionOfCustomers());
- }
- }
- public class MobileOperatorTest {
- public static void main(String[] args) {
- MobileOperator mobileOperator = new MobileOperator();
- System.out.println("--- READINGOF THE SALES REPORTS ----");
- mobileOperator.readSalesRepData(System.in);
- System.out.println("---- PRINTING FINAL REPORTS FOR SALES REPRESENTATIVES---- ");
- mobileOperator.printSalesReport(System.out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement