Advertisement
Guest User

Lottory2.0

a guest
Nov 19th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package Semester1;
  2. /*
  3.      Program: GUImortgage.Java
  4.      Programmer: Yonatan Mishan
  5.      Date: 10-22-18
  6.      version: 5.0
  7.      This program uses a GUI to take a user-input term rate and principal and
  8.      calculate monthly payment. in addition it print your Interest Paid, Princ Paid, and  principal in a table.
  9.      */
  10.     //imports
  11.     import javax.swing.*;
  12.     import java.awt.*;
  13.     import java.awt.event.*;
  14.     import java.text.DecimalFormat;
  15.     import java.awt.event.ActionListener;
  16.     import java.awt.event.ActionEvent;
  17.     import java.awt.event.ItemEvent;
  18.     import java.awt.event.ItemListener;
  19.    
  20.  
  21. public class GUImortgage6 extends JFrame implements ActionListener {
  22.         // creating the labels and buttons
  23.         FlowLayout flow = new FlowLayout();
  24.         JFrame frame = new JFrame("Menu");
  25.         //creating the labels and menu bar
  26.        
  27.         JMenuBar jmb = new JMenuBar();
  28.         JMenu menu = new JMenu("Menu");
  29.         JMenuItem m7 = new JMenuItem("7 year at 5.35%");
  30.         JMenuItem m15 = new JMenuItem("15 year at 5.50%");
  31.         JMenuItem m30 = new JMenuItem("30 year at 5.75%");
  32.        
  33.         JLabel labelPrincipal = new JLabel("Principal: ");
  34.         JTextField textPrincipal = new JTextField(5);
  35.         JLabel space1 = new JLabel("                     ");
  36.        
  37.         JLabel labelRate = new JLabel("Rate: ");
  38.         JTextField textRate = new JTextField(5);
  39.         JLabel space2 = new JLabel("                     ");
  40.        
  41.         JLabel labelTerm = new JLabel("Enter your term: ");
  42.         JTextField textTerm = new JTextField(5);
  43.         JLabel space3 = new JLabel("                     ");
  44.  
  45.         JMenu action = new JMenu("Actions");
  46.         JMenuItem Calculate = new JMenuItem("Calculate!");
  47.         JMenuItem Reset = new JMenuItem("Reset");
  48.         JMenuItem Exit = new JMenuItem("Exit");
  49.  
  50.         JLabel MonthlyPaymentLabel = new JLabel("Your monthly payment is: ");
  51.         JTextArea Payment = new JTextArea("");
  52.        
  53.         String text = "";
  54.  
  55.         public GUImortgage6() { // constructor
  56.             super("Mortgage Calculator");
  57.             Container con = getContentPane();
  58.             con.setLayout(flow);
  59.             frame.setJMenuBar(jmb);
  60.             con.add(jmb);
  61.             jmb.add(menu);
  62.             jmb.add(action);
  63.             con.add(labelPrincipal);
  64.             con.add(textPrincipal);
  65.             con.add(space1);
  66.             con.add(labelRate);
  67.             con.add(textRate);
  68.             con.add(space2);
  69.             con.add(labelTerm);
  70.             con.add(textTerm);
  71.             con.add(space3);
  72.             con.add(MonthlyPaymentLabel);
  73.             con.add(Payment);
  74.             con.add(Calculate);
  75.             con.add(Reset);
  76.             con.add(Exit);
  77.             menu.add(m7);
  78.             menu.add(m15);
  79.             menu.add(m30);
  80.             action.add(Calculate);
  81.             action.add(Reset);
  82.             action.add(Exit);
  83.             //adding action listeners.
  84.             m7.addActionListener(this);
  85.             m15.addActionListener(this);
  86.             m30.addActionListener(this);
  87.             Calculate.addActionListener(this);
  88.             Reset.addActionListener(this);
  89.             Exit.addActionListener(this);
  90.         }// end constructor
  91.  
  92.         public static void main(String[] args) {// main method
  93.             GUImortgage6 temp = new GUImortgage6();
  94.             temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// close on x
  95.             temp.setSize(1000, 600);// frame size
  96.             temp.setVisible(true);// make GUI visible
  97.         }// end main
  98.  
  99.         public void actionPerformed(ActionEvent e) {
  100.             String source = e.getActionCommand();
  101.             //setting Calculate button and calculating monthly payment from the user inputs
  102.             if(source == "7 year at 5.35%") {
  103.                 textRate.setText("7");
  104.                 textTerm.setText("5.35");
  105.             }else if(source == "15 year at 5.50%") {
  106.                 textRate.setText("15");
  107.                 textTerm.setText("5.50");
  108.             }else if(source == "30 year at 5.75%") {
  109.                 textRate.setText("30");
  110.                 textTerm.setText("5.75");
  111.             }else if (source == "Calculate!") {
  112.                 String Principal = textPrincipal.getText();
  113.                 String Rate = textRate.getText();
  114.                 String Term = textTerm.getText();
  115.                 double p = Double.parseDouble(Principal);
  116.                 double r = Double.parseDouble(Rate);
  117.                 double t = Double.parseDouble(Term);
  118.                 double MonthlyPayment = 0;
  119.                 try {
  120.                 if (t >= 10 && t <=40){
  121.                     t = t * 12;
  122.                     r = r / 1200.0;
  123.                     MonthlyPayment = p * r / (1.0 - Math.pow(r + 1, -t)); // calculating the principal.
  124.                 }else {
  125.                     throw new termException("TERM IS OUT OF BOUNDS! ");
  126.                     }
  127.                 } catch(termException e1) {
  128.                         text += String.format(e1.getMessage());
  129.                     }
  130.                
  131.                 for (int i=1;i<=360;i++){ //start loop for the number of payments
  132.                    
  133.                     double interest_paid = p * r;
  134.                     double princPaid =  MonthlyPayment - interest_paid;
  135.                     p =  p - princPaid;
  136.                     //add info to string
  137.                     text += String.format("Month " + i + " " + "MonthlyPayment: $%.2f Interest Paid: $%.2f Princ Paid: $%.2f principal: $%.2f\n ",
  138.                             MonthlyPayment, interest_paid, princPaid, p);    
  139.                 }
  140.                     Payment.setText(text); //print string
  141.                    
  142.             } else if (source == "Reset") { //reseting all the text
  143.                 textRate.setText("");
  144.                 textPrincipal.setText("");
  145.                 textTerm.setText("");
  146.                 Payment.setText("");
  147.             } else { //exiting the program
  148.                 System.exit(0);
  149.             }//end else
  150.         }//end actionPerformed
  151.        
  152.     }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement