Welcome to Dream.In.Code
Become a Java Expert!

Join 149,835 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,510 people online right now. Registration is fast and FREE... Join Now!




Reading from a file

 
Reply to this topicStart new topic

Reading from a file, Mortgage Program

retmsg21
13 Feb, 2007 - 06:55 PM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 16


My Contributions
How do I read in from a file the mortgage rates and periods?

5.35, 7
5.50, 15
5.75, 30
CODE

//Imports packages
import java.awt.*; //Used create to buttons
import java.awt.event.*; //Used create an event
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

import java.text.DecimalFormat; //Used to import decimal format for money

//Action listener
public class MortgageCalculator extends JPanel implements ActionListener, ItemListener
{

   //Variables
   protected JComboBox MortIntRate;
   protected JLabel lblprincipal, lblinterestRate, lblTerm, lblmonthlyPayment;
   protected JButton calculate, Reset, exit;//New button exit


   protected JTextField principal, interestRate, Term, monthlyPayment, mortgagecalculater;
   protected TextArea MortPayResults;

   public MortgageCalculator()

   {

      String[] comboItems = {"5.35", "5.50", "5.75", "Custom"};//Interest rates used in program creating a combo box


      //Label are defined here,used to creates labels and fields

      lblprincipal = new JLabel("Principal Amount:");
      principal = new JTextField("", 4);
      lblinterestRate = new JLabel("Interest Rate:");
      interestRate = new JTextField("", 3);
      interestRate.setEditable(false);
      lblTerm = new JLabel("Term:");
      Term = new JTextField("", 3);
      lblmonthlyPayment = new JLabel("Monthly Payment:");
      monthlyPayment = new JTextField("", 5);
      MortIntRate = new JComboBox(comboItems);
      mortIntPrevSel = (String) MortIntRate.getSelectedItem();
      interestRate.setText((String) MortIntRate.getSelectedItem());
      MortIntRate.addItemListener(this);

      MortPayResults = new TextArea(20, 65);

      //Create action buttons
      calculate = new JButton("Calculate"); //Create button used to calculate mortgage payment
      calculate.setActionCommand("GO");

      Reset = new JButton("Reset"); //Create button used to reset data from the fields
      Reset.setActionCommand("Reset");

      exit = new JButton("Exit");//new
      exit.setActionCommand("Exit");//new



      //Activate buttons
      calculate.addActionListener(this);
      Reset.addActionListener(this);
      exit.addActionListener(this);//new

      //Add labels and fields

      add(lblprincipal);
      add(principal);
      add(lblinterestRate);
      add(interestRate);
      add(MortIntRate);
      add(lblTerm);
      add(Term);
      add(lblmonthlyPayment);
      add(monthlyPayment);
      add(MortPayResults);
      add(calculate);
      add(Reset);
      add(exit);//new

   }
   public void actionPerformed(ActionEvent e)// Action event occurs here
   {
      if ("GO".equals(e.getActionCommand()))
      {
         CalculateMortgage();
      }
      else
      {
         principal.setText("");
         interestRate.setText("");
         Term.setText("");
         monthlyPayment.setText("");
         MortPayResults.setText("");

      }
   }

   public void CalculateMortgage()
   {
//      interestRate.setText("" + (String) MortIntRate.getSelectedItem());
      double dblprincipal = Double.parseDouble(principal.getText());
      double dblInterestRate = Double.parseDouble(interestRate.getText());

      //Handle the condition.
      if (dblInterestRate == 5.35)
         Term.setText("7");
      else if (dblInterestRate == 5.50)
         Term.setText("15");
      else if (dblInterestRate == 5.75)
         Term.setText("30");

      //Currency is set here
      int intTerm = Integer.parseInt(Term.getText());
      DecimalFormat money = new DecimalFormat("$0.00");

      double MonthlyPayment;
      double InterestPaid;
      double PrincipalBal;
      double temp;
      int Months;
      double MIntRate = dblInterestRate / 100;
      int MTerms = intTerm * 12;
      Months = MTerms;

      //Formulas
      MonthlyPayment = (dblprincipal * (MIntRate / 12)) / (1 - 1 / Math.pow((1 + MIntRate / 12), MTerms));

      //Converts Monthly Payment to decimal format
      monthlyPayment.setText("" + (money.format(MonthlyPayment)));

      MortPayResults.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");

      for (int counter = 0; counter < MTerms; counter++)
      {
         temp = (1 - 1 / Math.pow((1 + MIntRate / 12), Months));
         InterestPaid = MonthlyPayment * temp;
         dblprincipal = (dblprincipal - MonthlyPayment + InterestPaid);
         MTerms = MTerms--;
         Months--;

         MortPayResults.append((counter + 1) + "\t\t" + (money.format(MonthlyPayment) + "\t\t" + (money.format(dblprincipal) + "\t\t" + (money.format(InterestPaid) + "\n"))));
         MortPayResults.setCaretPosition(0);
      }

   }
   //GUI is created in this section
   private static void createAndShowGui()
   {
      JFrame frame = new JFrame("Multiple Mortgage Calculator");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //Frame is setup

      MortgageCalculator myCalculator = new MortgageCalculator();
      myCalculator.setOpaque(true);
      frame.setContentPane(myCalculator);

      frame.pack();
      frame.setVisible(true);


   }

   public static void main(String[] args)
   {
      javax.swing.SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
            createAndShowGui();
         }
      }

      );

   }

//Get interest rates here
   public void itemStateChanged(ItemEvent e)
   {
      if(e.getSource() instanceof JComboBox)
      {
         JComboBox cb = (JComboBox) e.getSource();
         String str = (String) cb.getSelectedItem();
         if(str.equals("Custom"))
         {
            interestRate.setEditable(true);
            interestRate.setText("");
         }
         else
         {
            interestRate.setEditable(false);
            interestRate.setText(str);
         }

      }
   }

private String mortIntPrevSel;

}

User is offlineProfile CardPM
+Quote Post

horace
RE: Reading From A File
14 Feb, 2007 - 12:26 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
have a look at Scanner
http://java.sun.com/j2se/1.5.0/docs/api/ja...il/Scanner.html
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:40AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month