Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. package Semester1;
  2.  
  3. /*
  4.  Program: mortgage7.Java
  5.  Programmer: Yonatan Mishan
  6.  Date: 12-08-18
  7.  version: 7.0
  8.  This program uses a GUI to read term rate and principal from a file. And
  9.  calculate monthly payment. add a menu option to calculate preset term and rate.
  10.  */
  11. //imports
  12. import javax.swing.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.text.DecimalFormat;
  16.  
  17. import java.awt.Container;
  18. import java.awt.FlowLayout;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.util.Formatter;
  22. import java.util.Scanner;
  23. import java.io.*;
  24.  
  25.  
  26. public class Mortgage7 extends JFrame implements ActionListener {
  27.     // creating the labels and buttons
  28.     FlowLayout flow = new FlowLayout();
  29.     JFrame frame = new JFrame("Menu");
  30.     //creating the labels and menu bar
  31.    
  32.     JMenuBar jmb = new JMenuBar();
  33.     JMenu menu = new JMenu("Menu");
  34.     JMenuItem m7 = new JMenuItem("7 year at 5.35%");
  35.     JMenuItem m15 = new JMenuItem("15 year at 5.50%");
  36.     JMenuItem m30 = new JMenuItem("30 year at 5.75%");
  37.    
  38.     JLabel labelPrincipal = new JLabel("Principal: ");
  39.     JTextField textPrincipal = new JTextField(5);
  40.     JLabel space1 = new JLabel("                     ");
  41.    
  42.     JLabel labelRate = new JLabel("Rate: ");
  43.     JTextField textRate = new JTextField(5);
  44.     JLabel space2 = new JLabel("                     ");
  45.    
  46.     JLabel labelTerm = new JLabel("Enter your term: ");
  47.     JTextField textTerm = new JTextField(5);
  48.     JLabel space3 = new JLabel("                     ");
  49.  
  50.     JMenu action = new JMenu("Actions");
  51.     JMenuItem Calculate = new JMenuItem("Calculate!");
  52.     JMenuItem Reset = new JMenuItem("Reset");
  53.     JMenuItem Exit = new JMenuItem("Exit");
  54.  
  55.     JLabel MonthlyPaymentLabel = new JLabel("Your monthly payment is: ");
  56.     JTextArea Payment = new JTextArea("");
  57.    
  58.     Scanner s;
  59.    
  60.     String text = "";
  61.    
  62.     public Mortgage7() { // constructor
  63.         super("Mortgage Calculator");
  64.         try {
  65.             s = new Scanner(new File("rates.txt"));
  66.         } catch (FileNotFoundException e) {
  67.             // TODO Auto-generated catch block
  68.             e.printStackTrace();
  69.         }
  70.        
  71.         Container con = getContentPane();
  72.         con.setLayout(flow);
  73.         frame.setJMenuBar(jmb);
  74.         con.add(jmb);
  75.         jmb.add(menu);
  76.         jmb.add(action);
  77.         con.add(labelPrincipal);
  78.         con.add(textPrincipal);
  79.         con.add(space1);
  80.         con.add(labelRate);
  81.         con.add(textRate);
  82.         con.add(space2);
  83.         con.add(labelTerm);
  84.         con.add(textTerm);
  85.         con.add(space3);
  86.         con.add(MonthlyPaymentLabel);
  87.         con.add(Payment);
  88.         con.add(Calculate);
  89.         con.add(Reset);
  90.         con.add(Exit);
  91.         menu.add(m7);
  92.         menu.add(m15);
  93.         menu.add(m30);
  94.         action.add(Calculate);
  95.         action.add(Reset);
  96.         action.add(Exit);
  97.         //adding action listeners.
  98.         m7.addActionListener(this);
  99.         m15.addActionListener(this);
  100.         m30.addActionListener(this);
  101.         Calculate.addActionListener(this);
  102.         Reset.addActionListener(this);
  103.         Exit.addActionListener(this);
  104.        
  105.         monthlyFromFile();
  106.     }// end constructor
  107.  
  108.     public static void main(String[] args) {// main method
  109.         Mortgage7 temp = new Mortgage7();
  110.         temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// close on x
  111.         temp.setSize(1000, 600);// frame size
  112.         temp.setVisible(true);// make GUI visible
  113.     }// end main
  114.  
  115.    
  116. public static double getMonthlyPayment(double time, double rate, double principle) {
  117.     time = time* 12;
  118.     rate = rate / 1200.0;
  119.     double monthlyPayment = principle * rate / (1.0 - Math.pow(rate + 1, -time)); // calculating the principal.
  120.     return monthlyPayment;
  121. }
  122.  
  123. //gets all monthly payments from file
  124. public void monthlyFromFile() {
  125.     //read all lines
  126.         text += "FILE DATA:  ";
  127.         while(s.hasNext()) {
  128.             String line[] = new String[4];
  129.             for(int i = 0; i < line.length; i++) {
  130.                 line[i] = s.next();
  131.             }
  132.             text += getMonthlyPayment(Double.parseDouble(line[0]),Double.parseDouble(line[3].substring(0, line[3].length() - 2)), 2) + "\n";
  133.         }
  134.         Payment.setText(text);
  135. }
  136.    
  137. public void actionPerformed(ActionEvent e) {
  138.     String source = e.getActionCommand();
  139.  
  140.     // setting Calculate button and calculating monthly payment from the user inputs
  141.     if (source == "7 year at 5.35%") {
  142.         textRate.setText("7");
  143.         textTerm.setText("5.35");
  144.     } else if (source == "15 year at 5.50%") {
  145.         textRate.setText("15");
  146.         textTerm.setText("5.50");
  147.     } else if (source == "30 year at 5.75%") {
  148.         textRate.setText("30");
  149.         textTerm.setText("5.75");
  150.     } else if (source == "Calculate!") {
  151.         String Principal = textPrincipal.getText();
  152.         String Rate = textRate.getText();
  153.         String Term = textTerm.getText();
  154.         double p = Double.parseDouble(Principal);
  155.         double r = Double.parseDouble(Rate);
  156.         double t = Double.parseDouble(Term);
  157.         try { //try exception
  158.             if (t >= 10 && t <= 40 && p >= 5000 && p <= 1000000) { //boundries
  159.                 double MonthlyPayment = getMonthlyPayment(t,r,p); // calculating the principal.
  160.                 for (int i = 1; i <= 360; i++) { // start loop for the number of payments
  161.  
  162.                     double interest_paid = p * r;
  163.                     double princPaid = MonthlyPayment - interest_paid;
  164.                     p = p - princPaid;
  165.                     // add info to string
  166.                     text += String.format("Month " + i + " "
  167.                             + "MonthlyPayment: $%.2f Interest Paid: $%.2f Princ Paid: $%.2f principal: $%.2f\n ",
  168.                             MonthlyPayment, interest_paid, princPaid, p);
  169.                 }
  170.  
  171.             } else {
  172.                 //throw new ("TERM IS OUT OF BOUNDS! "); //set exception error massage
  173.             }
  174.         } catch (Exception e1) {
  175.             text += String.format(e1.getMessage());
  176.         }
  177.         Payment.setText(text); // print string
  178.  
  179.     } else if (source == "Reset") { // reseting all the text
  180.         textRate.setText("");
  181.         textPrincipal.setText("");
  182.         textTerm.setText("");
  183.         Payment.setText("");
  184.     } else { // exiting the program
  185.         System.exit(0);
  186.     } // end else
  187. }// end actionPerformed
  188.    
  189. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement