Advertisement
dzocesrce

[NP] Bank Loan System

May 1st, 2025
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. //ne treba da gi ponishtuva clerks kaj sho mu frla exception zaradi nesoodveten loan ama me mrzi da go pravam create metodot na nivo na loan. Sepak e 1-vi maj...
  2.  
  3. public class BankTest {
  4.     public static void main(String[] args) {
  5.         Bank bank = new Bank();
  6.         System.out.println("----- READING LOAN APPLICATIONS -----");
  7.         bank.readApplication(System.in);
  8.         System.out.println("----- PRINTING APPROVED APPLICATIONS REPORTS FOR BANK CLERKS -----");
  9.         bank.printApplicationsReport(System.out);
  10.  
  11.     }
  12. }
  13. public class Clerk {
  14.     String clerkId;
  15.     List<Loan> loans;
  16.  
  17.     public Clerk(String clerkId) {
  18.         this.clerkId = clerkId;
  19.         this.loans = new ArrayList<>();
  20.     }
  21.  
  22.     public double computeTotalCommission(){
  23.         return loans.stream().mapToDouble(i->i.returnCommissionToClerk()).sum();
  24.     }
  25.  
  26.  
  27.     public static Clerk create(String line) throws InvalidLoanApplicationException {
  28.  
  29.         String[] parts = line.split(" ");
  30.         String clerkid = parts[0];
  31.         Clerk clerk = new Clerk(clerkid);
  32.         for (int i = 1; i < parts.length; i += 6) {
  33.             String clientId = parts[i];
  34.             int loanAmount = Integer.parseInt(parts[i + 1]);
  35.             int yearsOfPayment = Integer.parseInt(parts[i+2]);
  36.             int clientBalance = Integer.parseInt(parts[i+3]);
  37.             double interestRate = Double.parseDouble(parts[i+4]);
  38.             String loanType = parts[i+5];
  39.             if (loanType.equals("H")) {
  40.                 if (loanAmount > clientBalance * 15)
  41.                     throw new InvalidLoanApplicationException(clientId, loanAmount);
  42.                 clerk.loans.add(new HousingLoan(clientId, loanAmount, yearsOfPayment, clientBalance, interestRate));
  43.             } else {
  44.                 if (loanAmount > clientBalance * 5)
  45.                     throw new InvalidLoanApplicationException(clientId, loanAmount);
  46.                 clerk.loans.add(new SpendingLoan(clientId, loanAmount, yearsOfPayment, clientBalance, interestRate));
  47.             }
  48.         }
  49.         return clerk;
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         return String.format(" %s %d %d %.2f %.2f",
  55.                 clerkId, loans.size(),
  56.                 loans.stream().mapToInt(i->i.getLoanAmount()).min().getAsInt(),
  57.                 loans.stream().mapToDouble(i->i.returnTotalAmount()).max().getAsDouble(),
  58.                 computeTotalCommission());
  59.     }
  60. }
  61.  
  62. public class HousingLoan extends Loan{
  63.  
  64.     public HousingLoan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) throws InvalidLoanApplicationException {
  65.         super(clientId,loanAmount,yearsOfPayment,clientBalance,interestRate);
  66.     }
  67.  
  68.     @Override
  69.     public double returnCommissionToClerk() {
  70.         return returnTotalAmount()*0.06;
  71.     }
  72.  
  73.     @Override
  74.     public double returnTotalAmount() {
  75.         return  loanAmount * interestRate / 100 * yearsOfPayment + loanAmount;
  76.     }
  77. }
  78.  
  79. public class InvalidLoanApplicationException extends Exception {
  80.     public InvalidLoanApplicationException(String id,int amount) {
  81.         super(String.format("The client with id %s can not take a loan of amount %d.",id,amount));
  82.     }
  83. }
  84.  
  85. public abstract class Loan {
  86.     String clientId;
  87.     int loanAmount;
  88.     int yearsOfPayment;
  89.     int clientBalance;
  90.     double interestRate;
  91.  
  92.     public Loan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) {
  93.  
  94.         this.clientId = clientId;
  95.         this.loanAmount = loanAmount;
  96.         this.yearsOfPayment = yearsOfPayment;
  97.         this.clientBalance = clientBalance;
  98.         this.interestRate = interestRate;
  99.     }
  100.  
  101.     public abstract double returnCommissionToClerk();
  102.  
  103.     public abstract double returnTotalAmount();
  104.  
  105.     public String getClientId() {
  106.         return clientId;
  107.     }
  108.  
  109.     public int getLoanAmount() {
  110.         return loanAmount;
  111.     }
  112.  
  113.     public int getYearsOfPayment() {
  114.         return yearsOfPayment;
  115.     }
  116.  
  117.     public int getClientBalance() {
  118.         return clientBalance;
  119.     }
  120.  
  121.     public double getInterestRate() {
  122.         return interestRate;
  123.     }
  124.  
  125. }
  126.  
  127. public class SpendingLoan extends Loan{
  128.  
  129.     public SpendingLoan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) {
  130.         super(clientId,loanAmount,yearsOfPayment,clientBalance,interestRate);
  131.     }
  132.  
  133.     @Override
  134.     public double returnCommissionToClerk() {
  135.         return returnTotalAmount()*0.03;
  136.     }
  137.  
  138.     @Override
  139.     public double returnTotalAmount() {
  140.         return  loanAmount * interestRate / 100 * yearsOfPayment + loanAmount;
  141.     }
  142.  
  143. }
  144.  
  145. public class Bank {
  146.  
  147.     Map<Clerk,List<Loan>> clerks;
  148.  
  149.     public Bank() {
  150.         this.clerks = new HashMap<>();
  151.     }
  152.  
  153.     public void readApplication(InputStream in) {
  154.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  155.         clerks= br.lines().map(line -> {
  156.                     try {
  157.                         return Clerk.create(line);
  158.                     } catch (InvalidLoanApplicationException e) {
  159.                         System.out.println(e.getMessage());
  160.                         return null;
  161.                     }
  162.                 })
  163.                 .filter(Objects::nonNull)
  164.                 .collect(Collectors.toMap(
  165.                         i->i,
  166.                         i->i.loans
  167.                 ));
  168.  
  169.     }
  170.  
  171.     public void printApplicationsReport(PrintStream out) {
  172.         PrintWriter pw = new PrintWriter(out);
  173.         clerks.entrySet().stream()
  174.                 .sorted(Comparator.comparing((Map.Entry<Clerk, List<Loan>> entry) ->
  175.                         entry.getValue().size())
  176.                         .thenComparing(entry->entry.getKey().clerkId))
  177.                         .forEach(i-> System.out.println(i.getKey()));
  178.         pw.flush();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement