//Amortization6 .java import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import javax.swing.*; public class Amortization6 implements ActionListener, ItemListener { JTextField principal,length,rate; JComboBox terms; JScrollPane scroller; JRadioButton jrb1 = new JRadioButton("Enter your data"); JRadioButton jrb2 = new JRadioButton("Use predefined"); ButtonGroup bg = new ButtonGroup(); JLabel termText = new JLabel("Term"); JFrame frame = new JFrame("Amortization Calculator"); int i=0; Object source; int selected; ItemEvent e1; JButton calcButton = new JButton("Calculate"); JButton reset = new JButton("Reset"); String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"}; Object[][] data = { {"0","0","0","0","0","0"} }; public void itemStateChanged(ItemEvent e) //handling event of radio buttons for selection and deselection { source = e.getItemSelectable();e1=e; if (source == jrb1) { if(i==1) {frame.setVisible(false);Amortization6 am1 = new Amortization6();am1.createAndShowGUI();} if (e.getStateChange() == ItemEvent.SELECTED) /////////////////////////////////////determining if first radio button selected { length.setEditable(true); ////////////////////////////////////making user input editable rate.setEditable(true); terms.setVisible(false); ////////////////////////////////////making LABEL INVISIBLE termText.setVisible(false); ////////////////////////////////////making drop down list invisible } else if (e.getStateChange() == ItemEvent.DESELECTED) /////////////////////////////////////determining if first radio button deselected { length.setEditable(false); rate.setEditable(false); } } else if (source == jrb2) { if (e.getStateChange() == ItemEvent.SELECTED) /////////////////////////////////////determining if second radio button selected { length.setEditable(false); rate.setEditable(false); terms.setVisible(true); termText.setVisible(true); } else if (e.getStateChange() == ItemEvent.DESELECTED) /////////////////////////////////////determining if second radio button deselected { length.setEditable(true); rate.setEditable(true); } } } public void calculate() { int months = 84; double interest = 0.0535,balance; DecimalFormat monetary = new DecimalFormat("$#,###.00"); //sets the format of output try { try{ balance = Double.parseDouble(principal.getText()); //convert text bos data to double data type }catch(Exception e) { JOptionPane.showMessageDialog(null,"Invalid input detected. Enter correct values. \nOriginal error "+e); return; } System.out.println(balance); if((source == jrb1)&&(e1.getStateChange() == ItemEvent.SELECTED)) /////////////////////////////////////determining which radio button is selected { try{ months = (Integer.parseInt(length.getText())*12); interest = (Double.parseDouble(rate.getText())/100); }catch(Exception e) { JOptionPane.showMessageDialog(null,"Invalid input detected. Enter correct values. \nOriginal error "+e); return; } } else if((source == jrb2)&&(e1.getStateChange() == ItemEvent.SELECTED)) /////////////////////////////////////determining which radio button is selected { switch (terms.getSelectedIndex()) // operation on the basis of user selection 7/15/30 years and set interest rate { case 0: months = 84; interest = 0.0535; break; case 1: months = 180; interest = 0.055; break; case 2: default: months = 360; interest = 0.0575; break; } } String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"}; // Data Grid headings Object[][] data = new Object[months][6]; double monthlyInterest = interest / 12.0; double totalInterest = 0.0; //calculating interests and moth payments double monthlyPayment = balance * ((monthlyInterest * Math.pow(1.0 + monthlyInterest, months)) / (Math.pow(1.0 + monthlyInterest, months) - 1.0)); for (int j = 0; j < months; j++) { double interestThisPeriod = balance * monthlyInterest; totalInterest = totalInterest + interestThisPeriod; balance = balance - monthlyPayment + interestThisPeriod; data[j][0] = ""+(j+1); data[j][1] = monetary.format(monthlyPayment); data[j][2] = monetary.format(monthlyPayment-interestThisPeriod); data[j][3] = monetary.format(interestThisPeriod); data[j][4] = monetary.format(totalInterest); data[j][5] = monetary.format(balance); } JTable table = new JTable(data, columnNames); scroller.setViewportView(table); } catch (Exception e) {} } public void addComponentsToPane(Container contentPane) //All view { // Any number of rows and 2 columns contentPane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); jrb1.addItemListener(this); jrb2.addItemListener(this); bg.add(jrb1); bg.add(jrb2); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; contentPane.add(new JLabel("Principal:"), c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; principal = new JTextField(); contentPane.add(principal, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 1; contentPane.add(jrb1, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; contentPane.add(jrb2, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 2; contentPane.add(new JLabel("Enter Lenght in Years:"), c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 2; length = new JTextField(); contentPane.add(length, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 3; contentPane.add(new JLabel("Enter interest rate:"), c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 3; rate = new JTextField(); contentPane.add(rate, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 6; contentPane.add(termText, c); terms = new JComboBox(); terms.addItem("7 years at 5.35%"); terms.addItem("15 years at 5.5%"); terms.addItem("30 years at 5.75%"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 6; contentPane.add(terms, c); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 7; c.gridwidth = 2; JPanel jp = new JPanel(); jp.add(calcButton);jp.add(reset); calcButton.addActionListener(this); reset.addActionListener(this); contentPane.add(jp, c); scroller = new JScrollPane(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 8; c.gridwidth = 2; c.gridheight = 2; JTable table = new JTable(data, columnNames); table = new JTable(data, columnNames); scroller.setViewportView(table); contentPane.add(scroller, c); } public void createAndShowGUI() { //JFrame frame = new JFrame("Amortization Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane and components in GridLayout addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { final Amortization6 am = new Amortization6(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { am.createAndShowGUI(); } }); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equalsIgnoreCase("Reset")) { principal.setText(""); length.setText(""); rate.setText(""); JTable table = new JTable(data, columnNames); table = new JTable(data, columnNames); scroller.setViewportView(table); return; } i=1; calculate(); } }