输入年份 利率 总金额 ,计算出总还款数 每月还款数
package chapter16; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;// import javax.swing.border.TitledBorder; @SuppressWarnings("serial") public class LoanCalculator extends JFrame{ private JTextField AnnualInterestRate = new JTextField(); private JTextField numberOfYears = new JTextField(); private JTextField LoanAmount = new JTextField(); private JTextField MonthlyPayment = new JTextField(); private JTextField TotalPayment = new JTextField(); public LoanCalculator(){ JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(5,2)); p1.add(new JLabel("annual interest rate")); p1.add(AnnualInterestRate); p1.add(new JLabel("number of years")); p1.add(numberOfYears); p1.add(new JLabel("loan amount")); p1.add(LoanAmount); p1.add(new JLabel("monthly payment")); p1.add(MonthlyPayment); p1.add(new JLabel("totalpayment")); p1.add(TotalPayment); p1.setBorder(new TitledBorder("enter loan amount,interest rate,and year")); JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton button = new JButton("computer payment"); p2.add(button,BorderLayout.SOUTH); add(p1,BorderLayout.CENTER);//需要在这里加入方位 add(p2,BorderLayout.SOUTH); button.addActionListener(new buttonListener()); } public static void main(String[] args) { JFrame frame = new LoanCalculator(); frame.setTitle("loancalculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.pack(); frame.setSize(300, 300); } class buttonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { double interest = Double.parseDouble(AnnualInterestRate.getText()); double loanamount = Double.parseDouble(LoanAmount.getText()); int year = Integer.parseInt(numberOfYears.getText()); Loan loan = new Loan(interest,year,loanamount); MonthlyPayment.setText(String.format("%.2f", loan.getMonthlyPayment()));//why is no useful in Line 61?? TotalPayment.setText(String.format("%.2f", loan.getTotalPayment())); } } } class Loan { private double annualInterestRate; private int numberOfYears; private double loanAmount; private java.util.Date loanDate; /** Default constructor */ public Loan() { this(2.5, 1, 1000); } /** Construct a loan with specified annual interest rate, number of years and loan amount */ public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; this.loanAmount = loanAmount; loanDate = new java.util.Date(); } /** Return annualInterestRate */ public double getAnnualInterestRate() { return annualInterestRate; } /** Set a new annualInterestRate */ public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } /** Return numberOfYears */ public int getNumberOfYears() { return numberOfYears; } /** Set a new numberOfYears */ public void setNumberOfYears(int numberOfYears) { this.numberOfYears = numberOfYears; } /** Return loanAmount */ public double getLoanAmount() { return loanAmount; } /** Set a newloanAmount */ public void setLoanAmount(double loanAmount) { this.loanAmount = loanAmount; } /** Find monthly payment */ public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); return monthlyPayment; } /** Find total payment */ public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment; } /** Return loan date */ public java.util.Date getLoanDate() { return loanDate; } }
点击computer payment生成数据
时间: 2024-11-04 10:26:53