Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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...
- public class BankTest {
- public static void main(String[] args) {
- Bank bank = new Bank();
- System.out.println("----- READING LOAN APPLICATIONS -----");
- bank.readApplication(System.in);
- System.out.println("----- PRINTING APPROVED APPLICATIONS REPORTS FOR BANK CLERKS -----");
- bank.printApplicationsReport(System.out);
- }
- }
- public class Clerk {
- String clerkId;
- List<Loan> loans;
- public Clerk(String clerkId) {
- this.clerkId = clerkId;
- this.loans = new ArrayList<>();
- }
- public double computeTotalCommission(){
- return loans.stream().mapToDouble(i->i.returnCommissionToClerk()).sum();
- }
- public static Clerk create(String line) throws InvalidLoanApplicationException {
- String[] parts = line.split(" ");
- String clerkid = parts[0];
- Clerk clerk = new Clerk(clerkid);
- for (int i = 1; i < parts.length; i += 6) {
- String clientId = parts[i];
- int loanAmount = Integer.parseInt(parts[i + 1]);
- int yearsOfPayment = Integer.parseInt(parts[i+2]);
- int clientBalance = Integer.parseInt(parts[i+3]);
- double interestRate = Double.parseDouble(parts[i+4]);
- String loanType = parts[i+5];
- if (loanType.equals("H")) {
- if (loanAmount > clientBalance * 15)
- throw new InvalidLoanApplicationException(clientId, loanAmount);
- clerk.loans.add(new HousingLoan(clientId, loanAmount, yearsOfPayment, clientBalance, interestRate));
- } else {
- if (loanAmount > clientBalance * 5)
- throw new InvalidLoanApplicationException(clientId, loanAmount);
- clerk.loans.add(new SpendingLoan(clientId, loanAmount, yearsOfPayment, clientBalance, interestRate));
- }
- }
- return clerk;
- }
- @Override
- public String toString() {
- return String.format(" %s %d %d %.2f %.2f",
- clerkId, loans.size(),
- loans.stream().mapToInt(i->i.getLoanAmount()).min().getAsInt(),
- loans.stream().mapToDouble(i->i.returnTotalAmount()).max().getAsDouble(),
- computeTotalCommission());
- }
- }
- public class HousingLoan extends Loan{
- public HousingLoan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) throws InvalidLoanApplicationException {
- super(clientId,loanAmount,yearsOfPayment,clientBalance,interestRate);
- }
- @Override
- public double returnCommissionToClerk() {
- return returnTotalAmount()*0.06;
- }
- @Override
- public double returnTotalAmount() {
- return loanAmount * interestRate / 100 * yearsOfPayment + loanAmount;
- }
- }
- public class InvalidLoanApplicationException extends Exception {
- public InvalidLoanApplicationException(String id,int amount) {
- super(String.format("The client with id %s can not take a loan of amount %d.",id,amount));
- }
- }
- public abstract class Loan {
- String clientId;
- int loanAmount;
- int yearsOfPayment;
- int clientBalance;
- double interestRate;
- public Loan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) {
- this.clientId = clientId;
- this.loanAmount = loanAmount;
- this.yearsOfPayment = yearsOfPayment;
- this.clientBalance = clientBalance;
- this.interestRate = interestRate;
- }
- public abstract double returnCommissionToClerk();
- public abstract double returnTotalAmount();
- public String getClientId() {
- return clientId;
- }
- public int getLoanAmount() {
- return loanAmount;
- }
- public int getYearsOfPayment() {
- return yearsOfPayment;
- }
- public int getClientBalance() {
- return clientBalance;
- }
- public double getInterestRate() {
- return interestRate;
- }
- }
- public class SpendingLoan extends Loan{
- public SpendingLoan(String clientId, int loanAmount, int yearsOfPayment, int clientBalance, double interestRate) {
- super(clientId,loanAmount,yearsOfPayment,clientBalance,interestRate);
- }
- @Override
- public double returnCommissionToClerk() {
- return returnTotalAmount()*0.03;
- }
- @Override
- public double returnTotalAmount() {
- return loanAmount * interestRate / 100 * yearsOfPayment + loanAmount;
- }
- }
- public class Bank {
- Map<Clerk,List<Loan>> clerks;
- public Bank() {
- this.clerks = new HashMap<>();
- }
- public void readApplication(InputStream in) {
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- clerks= br.lines().map(line -> {
- try {
- return Clerk.create(line);
- } catch (InvalidLoanApplicationException e) {
- System.out.println(e.getMessage());
- return null;
- }
- })
- .filter(Objects::nonNull)
- .collect(Collectors.toMap(
- i->i,
- i->i.loans
- ));
- }
- public void printApplicationsReport(PrintStream out) {
- PrintWriter pw = new PrintWriter(out);
- clerks.entrySet().stream()
- .sorted(Comparator.comparing((Map.Entry<Clerk, List<Loan>> entry) ->
- entry.getValue().size())
- .thenComparing(entry->entry.getKey().clerkId))
- .forEach(i-> System.out.println(i.getKey()));
- pw.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement