[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Need help with formatting Mortgage calc

What I want to do is have a line between each entry. Can anyone please help?

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.math.*;
import java.io.*;
import javax.swing.JOptionPane;
 
 
public class GUIMortgageCalculator4 extends JFrame implements ActionListener
{
 
                  //adding RadioButtons
     JPanel row_a1 = new JPanel();
     JRadioButton week3 = new JRadioButton ("Week 3", true);
     JRadioButton week4 = new JRadioButton ("Week 4", false);
 
     //All J fields
     JPanel row_a = new JPanel();
     JLabel amntLabel = new JLabel();
 
     JTextField loanField = new JTextField(11);
     JLabel loan2Label = new JLabel ();
     JPanel row_b = new JPanel();
 
     JPanel row_c1 = new JPanel();
     JLabel termLabel = new JLabel ();  //term
     JTextField termField = new JTextField(4);
 
     JPanel row_c2 = new JPanel();
     JLabel int_entryLabel = new JLabel ();  //interest
     JTextField int_entryField = new JTextField(3);
 
     JPanel row_c3 = new JPanel();
     JLabel int_downpayLabel = new JLabel (); // downpayment
	 JTextField int_downpayField = new JTextField(10);
 
     JComboBox options = new JComboBox();
     JLabel optionsLabel = new JLabel();
 
     JPanel row_d = new JPanel();
     JButton calButton = new JButton();
     JButton resetButton = new JButton();
     JButton endButton = new JButton();
 
     JPanel row_e = new JPanel();
     JLabel paymtLabel = new JLabel();
     JTextField paymtField = new JTextField(8);
 
 
     JPanel row_f = new JPanel();
     JLabel outputLabel = new JLabel();
 
     JPanel row_g = new JPanel();
     JTextArea opField = new JTextArea(7, 60);
 
 
     JScrollPane scrollPane = new JScrollPane(opField,
               ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
               ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 
          ButtonGroup radioSelect = new ButtonGroup();
 
//data for GUI display
     public GUIMortgageCalculator4() {
 
          //main GUI Dimensions
          super(" Kelly's  GUI Mortgage Calculator");
          setSize(800, 450);
          new GridLayout(6, 1, 10, 10);
          FlowLayout flowCenter = new FlowLayout(FlowLayout.CENTER);
          FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
 
          Container pane = getContentPane();
          pane.setLayout(flow);
 
 
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setVisible(true);
 
          //actionListeners
          loanField.addActionListener(this);
          termField.addActionListener(this);
          int_entryField.addActionListener(this);
          //downpayField.addActionListener(this);
          options.addActionListener(this);
          calButton.addActionListener(this);
          resetButton.addActionListener(this);
          endButton.addActionListener(this);
          week3.addActionListener(this);
          week4.addActionListener(this);
 
 
          //mortgage amount
          row_a1.setLayout(flow);
		  radioSelect.add(week3);
		  radioSelect.add(week4);
		  row_a1.add(week3);
		  row_a1.add(week4);
          pane.add(row_a1);
 
 
          row_a.setLayout(flow);
          amntLabel.setText("Loan Amount:");
          row_a.add(amntLabel);
          row_a.add(loanField);
          pane.add(row_b);
 
 
 
                              //layout for options
          options.addItem("7 years at 5.35%");
          options.addItem("15 years at 5.5%");
          options.addItem("30 years at 5.75%");
          options.setEnabled(false);
 
          row_c1.setLayout(flow);
          row_c1.add(options);
          pane.add(row_b);
 
          row_c1.setLayout(flow);
          termLabel.setText("Term in Years");
          row_c1.add(termLabel);
          row_c1.add(termField);
          pane.add(row_c1);
 
          row_c2.setLayout(flow);
          int_entryLabel.setText("Interest %");
          row_c2.add(int_entryLabel);
          row_c2.add(int_entryField);
          pane.add(row_c2);
 
 
          //layout for buttons to calculate, Reset, and End
          row_d.setLayout(flow);
          calButton.setText("Calculate");
          calButton.setBackground(Color.white);
          row_d.add(calButton);
          resetButton.setText("Reset");
          resetButton.setBackground(Color.white);
          row_d.add(resetButton);
          endButton.setText("End");
          endButton.setBackground(Color.white);
          row_d.add(endButton);
          pane.add(row_d);
 
                              //layout for section that shows the monthly payment amount
          row_e.setLayout(flow);
          paymtLabel.setText("Monthly Payment Amount:");
          row_e.add(paymtLabel);
          row_e.add(paymtField);
          pane.add(row_e);
 
                              //layout for loan amoritzation table
          row_e.setLayout(flow);
          outputLabel.setText("Loan Payment Table");
          row_e.add(outputLabel);
          pane.add(row_e);
 
          row_f.setLayout(flowCenter);
          opField.setLineWrap(true);
          opField.setWrapStyleWord(true);
          opField.setEditable(true);
          row_f.add(scrollPane);
          pane.add(row_f);
 
 
     }
 
 
//adding actionEvents for Buttons
     public void actionPerformed(ActionEvent event) {
 
          Object source = event.getSource();
 
          if (source == calButton) {
               startCalculations();
 
          }
          if (source == resetButton) {
               reset();
 
          }
          if (source == endButton) {
               end();
 
          }
          if (source == week3){
 
              options.setEnabled(false);
                   termField.setEnabled(true);
              termField.setEditable(true);
              int_entryField.setEnabled(true);
              int_entryField.setEditable(true);
              }
 
              if (source == week4){
 
              options.setEnabled(true);
              termField.setEnabled(false);
              termField.setEditable(false);
              int_entryField.setEnabled(false);
              int_entryField.setEditable(false);
              }
 
         }
//formulas for monthly payment
     void startCalculations() {
 
          Thread thisThread = Thread.currentThread();
          NumberFormat currency = NumberFormat.getCurrencyInstance();
 
          double amt = 0;   //amount of loan
          double trm = 0;   //variable for term of loan
          double intrst = 0;  //variable for interest amount of loan
          double moIn = 0;  //variable for monthly payment for formula
          double moTrm = 0; //variable for monthly term for formula
          double prin = 0;  // variable for principle
          double paymt = 0;  //variable for payment
          double newPrin = amt;
          boolean Exception = false;
 
 
          try {
               amt = Double.parseDouble(loanField.getText());
          } catch (NumberFormatException e) {
               JOptionPane.showMessageDialog(null, "Please Do Not Use Commas",
                         "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               loanField.setText(null);
 
          }
 
          if (week3.isSelected())
          {
              if(options.getSelectedIndex() == 0)
              {
          trm=7;
          intrst=5.35;
          }
              else if(options.getSelectedIndex() ==1)
              {
                    trm=15;
                    intrst=5.5;
              }
              else
              {
                    trm=30;
                                    intrst=5.75;
                              }
                              }
                              else
                              {
         try
                    {
                   trm = Double.parseDouble(termField.getText());
                    }
                    catch (NumberFormatException e)
                    {
                    JOptionPane.showMessageDialog(this,"Enter amounts greater than zero","Please enter new amount",JOptionPane.ERROR_MESSAGE);
                    Exception=true;
                    termField.setText(null);
              }
              try
              {
                    intrst= Double.parseDouble(int_entryField.getText());
              }
              catch (NumberFormatException e)
              {
                    JOptionPane.showMessageDialog(this,"Enter amount greater than zero and with no % sign","Please enter new amount",JOptionPane.ERROR_MESSAGE);
                    Exception=true;
                    int_entryField.setText(null);
              }
        }
 
 
//output for Loan Payment Table
          if (amt >0) {
               amt = Double.parseDouble(loanField.getText());
               moIn = (intrst / 12) / 100;
               moTrm = trm * 12;
               paymt = amt * (moIn / (1 - java.lang.Math.pow((1 + moIn), (-moTrm))));
 
               paymtField.setText("" + currency.format(paymt));
 
               opField.append("Payment#");
               opField.append("            ");
               opField.append("Payment Amount");
               opField.append("           ");
               opField.append("Interest");
               opField.append("           ");
               opField.append("Principle Reduction");
               opField.append("           ");
               opField.append("Remaining Balance");
               opField.append("      \n");
 
 
 
 
               for (int i = 1; i <= moTrm; i++) {
                double newIn = moIn * amt;
                double reduct = paymt - newIn;
                newPrin = amt - reduct;
                amt = newPrin;
 
 
 
                    opField.append("    " + i);
                    opField.append("                         " + currency.format(paymt));
                    opField.append("                      " + currency.format(newIn));
                    opField.append("                    " + currency.format(reduct));
                    opField.append("                    " + currency.format(newPrin) + "\n");
 
 
 
 
                      if((amt <= 0 || trm <= 0 || intrst <= 0) && (Exception == false))
                      {
               JOptionPane.showMessageDialog(null, "Please Enter Positive Numbers Only.",
                         "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               if(amt <= 0)
               loanField.setText(null);
          }}}
        }
 
 
// resets GUI for another calculation
 void reset () {
 
          loanField.setText(null);
          paymtField.setText(null);
                              opField.setText(null);
                              termField.setText(null);
                              int_entryField.setText(null);
     }
 
// ends GUI and exits program
void end() {
          System.exit(0);
}
 
 
//main method to close program
public static void main(String args[]) {
          GUIMortgageCalculator4 d1= new GUIMortgageCalculator4();
          d1.setVisible(true);
            }
            ;
      }

Open in new window

0
spoilerbalto
Asked:
spoilerbalto
  • 3
  • 2
1 Solution
 
objectsCommented:
just add an extra newline

                    opField.append("                    " + currency.format(newPrin) + "\n\n");

0
 
spoilerbaltoAuthor Commented:
Where do I put this. I tried two different place and I have alot of errors.
0
 
objectsCommented:
it replaces the following at line 313

                    opField.append("                    " + currency.format(newPrin) + "\n");

0
 
spoilerbaltoAuthor Commented:
It is still not putting everything on its own line.


opField.append("    " + i);
                    opField.append("                    " + currency.format(newPrin) + "\n");
                    opField.append("                         " + currency.format(paymt));
                    opField.append("                    " + currency.format(newPrin) + "\n");
                    opField.append("                      " + currency.format(newIn));
                    opField.append("                    " + currency.format(newPrin) + "\n");
                    opField.append("                    " + currency.format(reduct));
                    opField.append("                    " + currency.format(newPrin) + "\n");
                    opField.append("                    " + currency.format(newPrin) + "\n");

Open in new window

0
 
objectsCommented:
tget rid of all the new lines, except the last one
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now