Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Name: Brandon Altuchow
- ID: 0207256
- Date: 9/23/17
- Description: Assignment 16 - JButtons & TextFields
- */
- package howmuch;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.text.NumberFormat;
- public class HowMuch extends JFrame implements ActionListener{
- Container content = this.getContentPane();
- JTextField tfAmount = new JTextField();
- JTextField tfInterest = new JTextField();
- JTextField tfYears = new JTextField();
- JLabel lblPayment = new JLabel();
- JLabel lblTotCost = new JLabel();
- JButton bCalc = new JButton("Calculate");
- public HowMuch(){
- this.setVisible(true);
- this.setSize(350, 300);
- this.setTitle("How Much?");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- content.setLayout(new GridLayout(6,2));
- content.add(new JLabel("Amount of Purchase"));
- content.add(tfAmount);
- content.add(new JLabel("Interest Rate [like 7.5]"));
- content.add(tfInterest);
- content.add(new JLabel("Years To Pay"));
- content.add(tfYears);
- content.add(new JLabel("Monthly Payment"));
- content.add(lblPayment);
- content.add(new JLabel("Total Purchase Cost"));
- content.add(lblTotCost);
- content.add(bCalc);
- bCalc.addActionListener(this);
- }
- public void actionPerformed(ActionEvent ae){
- String strAmount = tfAmount.getText();
- String strInterest = tfInterest.getText();
- String strYears = tfYears.getText();
- double dblAmount = Double.parseDouble(strAmount);
- double dblInterest = Double.parseDouble(strInterest);
- double dblYears = Double.parseDouble(strYears);
- dblInterest /= 100;
- dblInterest /= 12;
- double payment = (dblAmount*dblInterest)/(1 - Math.pow(1/(1+dblInterest),dblYears*12));
- double total = payment*12*dblYears;
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- lblPayment.setText("" + fmt.format(payment));
- lblTotCost.setText("" + fmt.format((total)));
- }
- public static void main(String[] args) {
- HowMuch gui = new HowMuch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement