Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Semester1;
- /*
- Program: mortgage7.Java
- Programmer: Yonatan Mishan
- Date: 12-08-18
- version: 7.0
- This program uses a GUI to read term rate and principal from a file. And
- calculate monthly payment. add a menu option to calculate preset term and rate.
- */
- //imports
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.text.DecimalFormat;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Formatter;
- import java.util.Scanner;
- import java.io.*;
- public class Mortgage7 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("");
- Scanner s;
- String text = "";
- public Mortgage7() { // constructor
- super("Mortgage Calculator");
- try {
- s = new Scanner(new File("rates.txt"));
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- 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);
- monthlyFromFile();
- }// end constructor
- public static void main(String[] args) {// main method
- Mortgage7 temp = new Mortgage7();
- temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// close on x
- temp.setSize(1000, 600);// frame size
- temp.setVisible(true);// make GUI visible
- }// end main
- public static double getMonthlyPayment(double time, double rate, double principle) {
- time = time* 12;
- rate = rate / 1200.0;
- double monthlyPayment = principle * rate / (1.0 - Math.pow(rate + 1, -time)); // calculating the principal.
- return monthlyPayment;
- }
- //gets all monthly payments from file
- public void monthlyFromFile() {
- //read all lines
- text += "FILE DATA: ";
- while(s.hasNext()) {
- String line[] = new String[4];
- for(int i = 0; i < line.length; i++) {
- line[i] = s.next();
- }
- text += getMonthlyPayment(Double.parseDouble(line[0]),Double.parseDouble(line[3].substring(0, line[3].length() - 2)), 2) + "\n";
- }
- Payment.setText(text);
- }
- 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);
- try { //try exception
- if (t >= 10 && t <= 40 && p >= 5000 && p <= 1000000) { //boundries
- double MonthlyPayment = getMonthlyPayment(t,r,p); // calculating the principal.
- 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);
- }
- } else {
- //throw new ("TERM IS OUT OF BOUNDS! "); //set exception error massage
- }
- } catch (Exception e1) {
- text += String.format(e1.getMessage());
- }
- 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