Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Semester1;
- /*
- Program: GUImortgage.Java
- Programmer: Yonatan Mishan
- Date: 10-22-18
- version: 5.0
- This program uses a GUI to take a user-input term rate and principal and
- calculate monthly payment. in addition it print your Interest Paid, Princ Paid, and principal in a table.
- */
- //imports
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.text.DecimalFormat;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- public class GUImortgage6 extends JFrame implements ActionListener {
- // creating the labels and buttons
- FlowLayout flow = new FlowLayout();
- JFrame frame = new JFrame("Menu");
- //creating the labels and menu bar
- JMenuBar jmb = new JMenuBar();
- JMenu menu = new JMenu("Menu");
- JMenuItem m7 = new JMenuItem("7 year at 5.35%");
- JMenuItem m15 = new JMenuItem("15 year at 5.50%");
- JMenuItem m30 = new JMenuItem("30 year at 5.75%");
- JLabel labelPrincipal = new JLabel("Principal: ");
- JTextField textPrincipal = new JTextField(5);
- JLabel space1 = new JLabel(" ");
- JLabel labelRate = new JLabel("Rate: ");
- JTextField textRate = new JTextField(5);
- JLabel space2 = new JLabel(" ");
- JLabel labelTerm = new JLabel("Enter your term: ");
- JTextField textTerm = new JTextField(5);
- JLabel space3 = new JLabel(" ");
- JMenu action = new JMenu("Actions");
- JMenuItem Calculate = new JMenuItem("Calculate!");
- JMenuItem Reset = new JMenuItem("Reset");
- JMenuItem Exit = new JMenuItem("Exit");
- JLabel MonthlyPaymentLabel = new JLabel("Your monthly payment is: ");
- JTextArea Payment = new JTextArea("");
- String text = "";
- public GUImortgage6() { // constructor
- super("Mortgage Calculator");
- Container con = getContentPane();
- con.setLayout(flow);
- frame.setJMenuBar(jmb);
- con.add(jmb);
- jmb.add(menu);
- jmb.add(action);
- con.add(labelPrincipal);
- con.add(textPrincipal);
- con.add(space1);
- con.add(labelRate);
- con.add(textRate);
- con.add(space2);
- con.add(labelTerm);
- con.add(textTerm);
- con.add(space3);
- con.add(MonthlyPaymentLabel);
- con.add(Payment);
- con.add(Calculate);
- con.add(Reset);
- con.add(Exit);
- menu.add(m7);
- menu.add(m15);
- menu.add(m30);
- action.add(Calculate);
- action.add(Reset);
- action.add(Exit);
- //adding action listeners.
- m7.addActionListener(this);
- m15.addActionListener(this);
- m30.addActionListener(this);
- Calculate.addActionListener(this);
- Reset.addActionListener(this);
- Exit.addActionListener(this);
- }// end constructor
- public static void main(String[] args) {// main method
- GUImortgage6 temp = new GUImortgage6();
- temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// close on x
- temp.setSize(1000, 600);// frame size
- temp.setVisible(true);// make GUI visible
- }// end main
- public void actionPerformed(ActionEvent e) {
- String source = e.getActionCommand();
- //setting Calculate button and calculating monthly payment from the user inputs
- if(source == "7 year at 5.35%") {
- textRate.setText("7");
- textTerm.setText("5.35");
- }else if(source == "15 year at 5.50%") {
- textRate.setText("15");
- textTerm.setText("5.50");
- }else if(source == "30 year at 5.75%") {
- textRate.setText("30");
- textTerm.setText("5.75");
- }else if (source == "Calculate!") {
- String Principal = textPrincipal.getText();
- String Rate = textRate.getText();
- String Term = textTerm.getText();
- double p = Double.parseDouble(Principal);
- double r = Double.parseDouble(Rate);
- double t = Double.parseDouble(Term);
- double MonthlyPayment = 0;
- try {
- if (t >= 10 && t <=40){
- t = t * 12;
- r = r / 1200.0;
- MonthlyPayment = p * r / (1.0 - Math.pow(r + 1, -t)); // calculating the principal.
- }else {
- throw new termException("TERM IS OUT OF BOUNDS! ");
- }
- } catch(termException e1) {
- text += String.format(e1.getMessage());
- }
- for (int i=1;i<=360;i++){ //start loop for the number of payments
- double interest_paid = p * r;
- double princPaid = MonthlyPayment - interest_paid;
- p = p - princPaid;
- //add info to string
- text += String.format("Month " + i + " " + "MonthlyPayment: $%.2f Interest Paid: $%.2f Princ Paid: $%.2f principal: $%.2f\n ",
- MonthlyPayment, interest_paid, princPaid, p);
- }
- Payment.setText(text); //print string
- } else if (source == "Reset") { //reseting all the text
- textRate.setText("");
- textPrincipal.setText("");
- textTerm.setText("");
- Payment.setText("");
- } else { //exiting the program
- System.exit(0);
- }//end else
- }//end actionPerformed
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement