Java--类和对象 --实例学习

贷款类:loan

1.创建2个class。TestLoanClass 和 Loan

 

2.在TestLoanClass类 中,写入代码:

 1 package com.algorithm.java.blueBirdge;
 2 
 3 import javax.swing.*;
 4 
 5 //JOptionPane消息提示框
 6 
 7 public class TestLoanClass {
 8     public static void main(String args[]) {
 9         //JOptionPane消息提示框
10         //enter yearly interest rate
11         String annualInterestRateString = JOptionPane.showInputDialog(
12                 // JOptionPane.showInputDialog方法返回用户输入的字符串。
13                 "Enter yearly interestrate,for example 8.25:"
14         );
15 
16         //Convert string to double
17 
18         //Double.parseDouble方法是把数字类型的字符串,转换成double类型
19         //Double.valueOf方法是把数字类型的字符串,转换成Double类型
20         double annualInterestRate = Double.parseDouble(annualInterestRateString);
21 
22         //enter number of years
23 
24         String numberOfYearString = JOptionPane.showInputDialog(
25                 // JOptionPane.showInputDialog方法返回用户输入的字符串。
26                 "Enter number of years as an integer,\nfor example 5:"
27         );
28 
29         //Convert string to int
30         int numberOfyears = Integer.parseInt(numberOfYearString);
31 
32         //enter loan amout
33         String loanString = JOptionPane.showInputDialog(
34                 "Enter loan amout,for example 120000.95:"
35         );
36 
37         //convert string to double
38         double loanAmount = Double.parseDouble(loanString);
39 
40         //Create Loan object
41         Loan loan = new Loan(annualInterestRate, numberOfyears, loanAmount);
42 
43         //Format to keep two digits after the decimal point
44         double monthlyPayment = (int) (loan.getMonthlyPayment() * 100) / 100.0;
45         double totalPayment = (int) (loan.getTotalPayment() * 100) / 100.0;
46 
47         //diaplay results
48         String output = "The loan was create on " + loan.getLoanDate().toString() + "\nThe monthly payment is" + monthlyPayment + "\nThe total Payment is" + totalPayment;
49         JOptionPane.showMessageDialog(null, output);
50     }
51 }

3.在Loan类中插入代码:

 1 package com.algorithm.java.blueBirdge;
 2 
 3 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
 4 
 5 public class Loan {
 6     private double annualInterestRate;
 7     private int numberOfYears;
 8     private double loanAmount;
 9     private java.util.Date loanDate;//为什么这句话要写在类里面?????????????
10 
11     /*Default constructor*/
12     public Loan() {
13         this(7.5, 30, 100000);
14     }
15 
16     /*Construct a loan with specified annual interest rate,
17      *  number of years and loan amout*/
18     public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
19         this.annualInterestRate = annualInterestRate;
20         this.numberOfYears = numberOfYears;
21         this.loanAmount = loanAmount;
22         loanDate = new java.util.Date();//????????????????????????????????
23     }
24 
25     //*************封装后的获取和修改********************
26     /*return annualInteresRate*/
27     public double getAnnualInterestRate() {
28         return annualInterestRate;
29     }
30 
31     /*Set a new annualInteresRate*/
32     public void setAnnualInterestRate(double annualInterestRate) {
33         this.annualInterestRate = annualInterestRate;
34     }
35 
36     /*return numberOfYears*/
37     public int getNumberOfYears() {
38         return numberOfYears;
39     }
40 
41     /*Set a new numberOfYears*/
42     public void setNumberOfYears(int numberOfYears) {
43         this.numberOfYears = numberOfYears;
44     }
45 
46     /*return loanAmout*/
47     public double getLoanAmount() {
48         return loanAmount;
49     }
50 
51     /*Set a new loanAmount*/
52     public void setLoanAmount(double loanAmount) {
53         this.loanAmount = loanAmount;
54     }
55     //*************************************************
56 
57     /*find Monthly payment*/
58     public double getMonthlyPayment() {
59         double mothlyInterestRate = annualInterestRate / 1200;
60         return loanAmount * mothlyInterestRate / (1 - (Math.pow(1 / (1 + mothlyInterestRate), numberOfYears * 12)));
61 
62     }
63 
64     /*find total payment*/
65     public double getTotalPayment() {
66         return getMonthlyPayment() * numberOfYears * 12;
67     }
68 
69     /*return loan date*/
70     //?????????????????????????????
71     public java.util.Date getLoanDate() {
72         return loanDate;
73     }
74 }

 

4.代码分析:
(1)代码部分出现了:private java.util.Date loanDate;写在类里面,是为了引入loanDate变量。同时,在构造方法中也出现:loanDate = new java.util.Date();

(2)Loan的数据域有 :annualInterestRate,numberOfYears,loanAmount和loanDate

  Loan的数据域有:

getAnnualInterestRate(返回这笔贷款的年利率),getNumberOfYears(返回这笔贷款的年限),getLoanAmount(返回这笔贷款额),getLoanDate(返回这笔贷款发生日期),

setAnnualInterstRate(设置这笔贷款新的年利率),setNumberOfYears(设置这笔贷款新的贷款年限),setLoanAmount(设置这笔贷款新的贷款额),

getMonthlyPayment(返回这笔贷款的月支付额)和getTotalPayment(返回这笔贷款的总支付额)

 

5.测试结果:

 

6.测试成功!

 

 

package com.algorithm.java.blueBirdge;

import javax.swing.*;

//JOptionPane消息提示框

public class TestLoanClass {
public static void main(String args[]) {
//JOptionPane消息提示框
//enter yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog(
// JOptionPane.showInputDialog方法返回用户输入的字符串。
"Enter yearly interestrate,for example 8.25:"
);

//Convert string to double

//Double.parseDouble方法是把数字类型的字符串,转换成double类型
//Double.valueOf方法是把数字类型的字符串,转换成Double类型
double annualInterestRate = Double.parseDouble(annualInterestRateString);

//enter number of years

String numberOfYearString = JOptionPane.showInputDialog(
// JOptionPane.showInputDialog方法返回用户输入的字符串。
"Enter number of years as an integer,\nfor example 5:"
);

//Convert string to int
int numberOfyears = Integer.parseInt(numberOfYearString);

//enter loan amout
String loanString = JOptionPane.showInputDialog(
"Enter loan amout,for example 120000.95:"
);

//convert string to double
double loanAmount = Double.parseDouble(loanString);

//Create Loan object
Loan loan = new Loan(annualInterestRate, numberOfyears, loanAmount);

//Format to keep two digits after the decimal point
double monthlyPayment = (int) (loan.getMonthlyPayment() * 100) / 100.0;
double totalPayment = (int) (loan.getTotalPayment() * 100) / 100.0;

//diaplay results
String output = "The loan was create on " + loan.getLoanDate().toString() + "\nThe monthly payment is" + monthlyPayment + "\nThe total Payment is" + totalPayment;
JOptionPane.showMessageDialog(null, output);
}
}


转载于:https://www.cnblogs.com/Catherinezhilin/p/8666978.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值