• 用Java写PTA 7-11 设计一个能处理异常的Loan类


    用Java写PTA 7-11 设计一个能处理异常的Loan类
    定义一个贷款类Loan,其中有属性:
    annualInterestRate:double,表示贷款的年利率(默认值:2.5)
    numberOfYears:int,表示贷款的年数(默认值:1)
    loanAmount:double,表示贷款额(默认值:100)
    loanDate:java.util.Date,表示创建贷款的日期
    定义方法:
    (1)默认的无参构造方法
    (2)带指定利率、年数和贷款额的构造方法
    (3)所有属性的get/set方法
    (4)返回这笔贷款的月支付额getMonthlyPayment()
    月支付额 = (贷款额度月利率)/(1-(1/Math.pow(1+月利率,年数12)))
    (5)返回这笔贷款的总支付额getTotalPayment()
    总支付额度 = 月支付额度年数12

    附上如下的测试类。

    public class Main{
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          while (input.hasNext()) {
              double AIR = input.nextDouble();
              int NOY = input.nextInt();
              double LA = input.nextDouble();
              try {
                  Loan m = new Loan(AIR, NOY, LA);
                  System.out.printf("%.3f\n",m.getTotalPayment());
              } catch (Exception ex) {
                  System.out.println(ex);
              }
          }
        
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输入格式:
    输入有多组数据,一个实数表示年利率,一个整数表示年数,一个实数表示贷款总额。

    输出格式:
    若任意一项小于或等于零,抛出IllegalArgumentException异常及相应描述(Number of years must be positive或Annual interest rate must be positive或Loan amount must be positive);有多项不符合,以不符合最前项为准;

    若均符合要求,按照格式输出总额。

    输入样例:
    在这里给出一组输入。例如:

    1 1 1000
    2.0 0 2000
    0 0 0
    输出样例:
    在这里给出相应的输出。例如:

    1005.425
    java.lang.IllegalArgumentException: Number of years must be positive
    java.lang.IllegalArgumentException: Annual interest rate must be positive

    import java.util.Scanner;
    class Loan{
      double annualInterestRate;
       int numberOfYears;
     double loanAmount;
    java.util.Date loanDate;
        Loan(){
            this.annualInterestRate=2.5;
            this.numberOfYears=1;
            this.loanAmount=100;
            this.loanDate=loanDate;
        }
         Loan(double annualInterestRate,int numberOfYears,double loanAmount){
            this.annualInterestRate=annualInterestRate;
            this.numberOfYears=numberOfYears;
            this.loanAmount=loanAmount;
            
        }
        public double getAnnualInterestRate() {
    	return annualInterestRate;
    }
    public void setAnnualInterestRate(double annualInterestRate) {
    	this.annualInterestRate = annualInterestRate;
    }
    public int getNumberOfYears() {
    	return numberOfYears;
    }
    public void setNumberOfYears(int numberOfYears) {
    	this.numberOfYears = numberOfYears;
    }
    public double getLoanAmount() {
    	return loanAmount;
    }
    public void setLoanAmount(double loanAmount) {
    	this.loanAmount = loanAmount;
    }
    public java.util.Date getLoanDate() {
    	return loanDate;
    }
    public void setLoanDate(java.util.Date loanDate) {
    	this.loanDate = loanDate;
    }
      public double getMonthlyPayment(){
          double monthRate=annualInterestRate/1200;
          double monthlyPayment=(monthRate)/(1-(1/Math.pow(1+monthRate,numberOfYears*12)));  
          return monthlyPayment;
      }
     public double getTotalPayment(){
    	 double totalPayment=getMonthlyPayment()*12*numberOfYears;
         return totalPayment;
     }
    
    }
    public class Main{
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          while (input.hasNext()) {
              double AIR = input.nextDouble();
              int NOY = input.nextInt();
              double LA = input.nextDouble();
              
              try {
                  Loan m = new Loan(AIR, NOY, LA);
                  if (AIR<=0)throw new IllegalArgumentException("Annual interest rate must be positive");
    else if (NOY<=0)throw new IllegalArgumentException("Number of years must be positive");
    else if (LA<=0)throw new IllegalArgumentException("Loan amount must be positive");
    
                  System.out.printf("%.3f\n",m.getTotalPayment()*1000);
              } catch (Exception ex) {
                  System.out.println(ex);
              }
          }
        
      }
    }
            
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    Primavera Unifier 自定义报表制作及打印分享
    SpringSecurity6 | 默认登录页
    【3D 图像分割】基于 Pytorch 的 VNet 3D 图像分割2(基础数据流篇)
    中医实训室:在传统针灸教学中的应用与创新
    ChatGPT 从零到一打造私人智能英语学习助手
    洛必达法则
    【社区人物志】专访马龙伟:轮子不好用,那就自己造!
    107.(前端)分类管理增加值实现——使用elementui中的动态编辑标签发送请求
    技术 | 终端安全 | 服务器并不像您想象的那么安全
    Linux17 jdk tomcat idea mysql在linux上安装
  • 原文地址:https://blog.csdn.net/m0_61216070/article/details/125462480