Investhor GUI

Arman Majumder 0 Tallied Votes 170 Views Share

A program that compounds interest continuously to a specified premium, rate, and number of years. Displays the total work up in the GUI panel in a tree form.

// Arman Majumder
// Investhor GUI

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import java.io.*;
   
    public class InvesthorWindow extends JFrame
   {
      private JLabel yearLabel = new JLabel("Year");
      private JLabel principal1Label = new JLabel("Initial Principal");
      private JLabel interestLabel = new JLabel("Interest");
      private JLabel principal2Label = new JLabel("Final Principal");
   
       public InvesthorWindow(String name, double principal, double rate, int year)
      {     
         JPanel investment = new JPanel(new GridLayout(1, 2, 4, 8));
         investment.add(yearLabel);
         investment.add(principal1Label);
         investment.add(interestLabel);
         investment.add(principal2Label);	
      
         Container container = getContentPane();
         container.add(investment, BorderLayout.CENTER);
         JPanel compound = new JPanel(new GridLayout((year), 1, 1, 1));
         
         double inter = 0;
         for(int count = 1; count<= year; count++)
         {
            JLabel time = new JLabel("Year " + count);
            JLabel initial = new JLabel(String.format("$%,.2f", principal));
            inter -=  principal;
            principal = principal * Math.pow((1 + (rate/400)),(4*count));  
            inter +=  principal; 
            JLabel interest = new JLabel(String.format("$%,.2f", inter));
            JLabel fnlAmt = new JLabel(String.format("$%,.2f", principal));
         
            compound.add(time);
            compound.add(initial);
            compound.add(interest);
            compound.add(fnlAmt);	
         } 
         container.add(compound, BorderLayout.SOUTH);
      }
      
       public static void main (String[] args)
      {	
         String name =  JOptionPane.showInputDialog(null,
            "Enter your full name", "Input", JOptionPane.QUESTION_MESSAGE);  
            
         String money = JOptionPane.showInputDialog(null,
            "Enter your initial principal", "Input", JOptionPane.QUESTION_MESSAGE);
         try
         {
            double principal = Double.parseDouble(money);
         }
             catch(Exception e)
            {
               JOptionPane.showMessageDialog(null, "ERROR: principal input syntax");
            }
         double principal = Double.parseDouble(money);
            
         String intRate = JOptionPane.showInputDialog(null,
            "Enter your interest rate", "Input", JOptionPane.QUESTION_MESSAGE);      
         try
         {
            double rate = Double.parseDouble(intRate);
         }
             catch(Exception e)
            {
               JOptionPane.showMessageDialog(null, "ERROR: rate input syntax");
            }
         double rate = Double.parseDouble(intRate);
      
         String time = JOptionPane.showInputDialog(null,
            "Enter your year", "Input", JOptionPane.QUESTION_MESSAGE);  
         try
         {
            int year = Integer.parseInt(time);
         }
             catch(Exception e)
            {
               JOptionPane.showMessageDialog(null, "ERROR: year input syntax");
            }
         int year = Integer.parseInt(time);
      
         InvesthorWindow investhor = new InvesthorWindow(name, principal, rate, year);
         investhor.setTitle("Investhor");
         investhor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         investhor.pack();
         investhor.setVisible(true);
      }
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.