"Fundamentals of Java language Programming"--event-driven programming--loan calculator

Source: Internet
Author: User


Enter the total amount of the year interest rate to calculate the monthly repayment number of the total repayment


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 ();p rivate JTextField numberofyears = new JTextField ();p rivate JTextField L Oanamount = new JTextField ();p rivate JTextField monthlypayment = new JTextField ();p rivate JTextField TotalPayment = new JT Extfield ();p ublic loancalculator () {JPanel P1 = new JPanel ();p 1.setLayout (New GridLayout (5,2));p 1.add (New JLabel (" Annual interest rate "));p 1.add (annualinterestrate);p 1.add (New JLabel (" Number of years ");p 1.add (numberofyears); P1.add (New JLabel ("loan Amount"));p 1.add (loanamount);p 1.add (New JLabel ("Monthly Payment"));p 1.add (Monthlypayment);p 1.add (New JLabel ("Totalpayment"));p 1.add (totalpayment);p 1.setBorder (New Titledborder ("Enter loan amount, Interest Rate,and Year ")); JPanel P2 = new JPanel ();p 2.setLayout (New FlowLayout (flowlayout.right)); JButton button = new JButton ("Computer payment");p 2.add (Button,borderlayout.south); add (p1,borderlayout.center);// Need to add an orientation 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{@Overridepublic void actionperformed (ActionEvent e) {Double interest = Double.parsedouble (Annualinterestrate.gettext ());d ouble 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 on 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 (Doub    Le 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.annualinter  Estrate = 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/1    200; Double monthlypayment = Loanamount * Monthlyinterestrate/(1-(Math.pow (1/(1 + monthlyinterestrate), numberofyear    S * 12)));      return monthlypayment; }/** Find Total payment */public double gettotalpayment () {Double totalpayment = getmonthlypayment () * Numberofyea    RS * 12;      return totalpayment;  }/** return Loan date */public java.util.Date getloandate () {return loandate; }}





Click Computer payment to generate the data




"Fundamentals of Java language Programming"--event-driven programming--loan calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.