Advertisement
NotATowel

Untitled

Sep 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. /**
  2. Name: Brandon Altuchow
  3. ID: 0207256
  4. Date: 9/23/17
  5. Description: Assignment 16 - JButtons & TextFields
  6.  */
  7. package howmuch;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.text.NumberFormat;
  12.  
  13. public class HowMuch extends JFrame implements ActionListener{
  14.     Container content = this.getContentPane();
  15.     JTextField tfAmount = new JTextField();
  16.     JTextField tfInterest = new JTextField();
  17.     JTextField tfYears = new JTextField();
  18.     JLabel lblPayment = new JLabel();
  19.     JLabel lblTotCost = new JLabel();
  20.     JButton bCalc = new JButton("Calculate");
  21.    
  22.     public HowMuch(){
  23.         this.setVisible(true);
  24.         this.setSize(350, 300);
  25.         this.setTitle("How Much?");
  26.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.        
  28.         content.setLayout(new GridLayout(6,2));
  29.         content.add(new JLabel("Amount of Purchase"));
  30.         content.add(tfAmount);
  31.         content.add(new JLabel("Interest Rate [like 7.5]"));
  32.         content.add(tfInterest);
  33.         content.add(new JLabel("Years To Pay"));
  34.         content.add(tfYears);
  35.         content.add(new JLabel("Monthly Payment"));
  36.         content.add(lblPayment);
  37.         content.add(new JLabel("Total Purchase Cost"));
  38.         content.add(lblTotCost);
  39.         content.add(bCalc);
  40.         bCalc.addActionListener(this);        
  41.     }
  42.     public void actionPerformed(ActionEvent ae){
  43.         String strAmount = tfAmount.getText();
  44.         String strInterest = tfInterest.getText();
  45.         String strYears = tfYears.getText();
  46.        
  47.         double dblAmount = Double.parseDouble(strAmount);
  48.         double dblInterest = Double.parseDouble(strInterest);
  49.         double dblYears = Double.parseDouble(strYears);
  50.        
  51.         dblInterest /= 100;
  52.         dblInterest /= 12;
  53.         double payment = (dblAmount*dblInterest)/(1 - Math.pow(1/(1+dblInterest),dblYears*12));
  54.         double total = payment*12*dblYears;
  55.        
  56.         NumberFormat fmt = NumberFormat.getCurrencyInstance();
  57.         lblPayment.setText("" + fmt.format(payment));
  58.         lblTotCost.setText("" + fmt.format((total)));
  59.     }    
  60.     public static void main(String[] args) {
  61.         HowMuch gui = new HowMuch();
  62.     }  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement