【JAVA语言程序设计基础篇】--事件驱动程序设计--贷款计算器

输入年份 利率 总金额 ,计算出总还款数 每月还款数

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

【JAVA语言程序设计基础篇】--事件驱动程序设计--贷款计算器的相关文章

Java语言程序设计基础篇 循环(四)

①打印:***** **** *** ** * for(int x=1; x<=5; x++) { for(int y=x; y<=5; y++) { System.out.print("*"); //向下一般的格式for(int y=x; y<=5; y++) } System.out.println(); } ②打印:* ** *** **** ***** for (int x=1; x<=5 ;x++ ) { for (int y=1;y<=x ;y

Java语言程序设计基础篇 方法(五)

生成随机字符 生成随机字符就是生成0到65535之间的一个随机整数,因为0<=Math.random()<1.0,必须在65535+1 (int) (Math.random() * (65535+1)) 随机生成小写字母 public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2){ return (char)(ch1 +Math.random() * (ch2 - ch1 + 1

Java语言程序设计基础篇 循环(四)练习

*4.21(计算不同利率下的贷款)编写程序,让用户输入贷款总额及以年为单位的贷款期限,以1/8为递增量,显示从5%到8%的利率下每月支付额和总偿还额.假设输入贷款总量为10 000,还贷期限为5年,所显示的输出如下: 贷款总额:to 000 年数:5 利率月支付额总偿还额 5%188 .71   11322.74 5 .125%189.28   11357.13 5 .25%189.85   11391.59 ... //Exercise3_26.java: displays the month

Java语言程序设计基础篇 数组(六)

Java语法之数组 数组的定义 数组是对象. 如:int [ ]  x = new int[100];或 :int x [ ]  = new int[100];(这种方式主要是为了适应C/C++程序员) 声明一个数组变量:int [ ] x;并不会在内存中给数组分配任何空间,仅创建一个引用数组的存储地址. 数组创建后,其元素赋予默认值,数值型基本数据类型默认值为0,char类型为'\u0000',boolean类型为false. 数组的静态初始化 如:int [ ] x = new int [

【JAVA语言程序设计基础篇】--事件驱动程序设计--匿名类监听器

监听器类是特意为创建一个GUI组件而设计的监听器对象.监听器不被其他应用程序所共享,因此,正确的做法是将他作为一个内部类定义在框架类中. 当然,可以使用匿名内部类简化内部类监听器. 匿名内部类是没有名字的内部类. 他一步完成定义内部类和创建一个该类的实例. 由于匿名内部类是一种特殊的内部类,所以,可以将他看作有以下特征的内部类: 1.匿名内部类必须总是扩展父类或者实现接口,但他不能有显示的extends 和 implements子句. 2.匿名捏不累必须实现父类或者接口中的所有的抽象方法. 3.

【JAVA语言程序设计基础篇】--事件驱动程序设计--定义监听器的另一种方式

有很多种方式定义监听类,哪种方式比较好呢? 使用内部类或者匿名内部类定义监听类已经成了事件处理程序设计的标准. 因为他通常都能提供清晰.干净和简洁的代码. package chapter16; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFram

【JAVA语言程序设计基础篇】--事件驱动程序设计--键盘事件

java] view plain copy print? package chapter16; import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") p

【JAVA语言程序设计基础篇】--事件驱动程序设计--鼠标事件

点击文字 鼠标拖动 文字会随鼠标拖动 package chapter16; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial

【JAVA语言程序设计基础篇】--事件驱动程序设计--窗口事件

对窗口进行操作,将操作步骤显示在控制台 package chapter16; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; @SuppressWarnings("serial") public class TestWindowEvent extends JFrame { public TestWindowEvent (){ addWi