Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:
7 years at 5.35%
15 years at 5.5%
30 years at 5.75%
Use an array for the mortgage data for the different loans. Read the interest rates to fill the array from a sequential file. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Add graphics in the form of a chart. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program. Here is what I have thus far:
import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import javax.swing.*; import javax.swing.text.*; import java.lang.Double.*; public class MortPayment_v3 extends Frame implements ActionListener { //declare an array for buttons private Button buttons[]; //declare panels for window layout private Panel fieldsPanel, buttonPanel, labelsPanel, displayPanel; //Create Variables private static double LoanAmt=0.0; private static double MonthlyPayment=0.0; private static double InterestRate=0.0; private static int Duration=0; //Clear Fields private boolean clearText=true; String selection=(""); //Create Array double[][] availLoans={{LoanAmt, 5.35, 7.00},{LoanAmt, 5.5, 15.00},{LoanAmt, 5.75, 30.00}}; //Format for 2 decimal places DecimalFormat pmtPattern=new DecimalFormat("##,###,###.00"); DecimalFormat dmlPattern=new DecimalFormat("#.####"); DecimalFormat intRatePattern=new DecimalFormat("##.###%"); DecimalFormat durPattern=new DecimalFormat("## yrs"); private JLabel LoanAmtLabel; //declare label for Mortgage Amount field private JLabel LoanSelectionMenuLabel; //declare label for Loan Selection menu private JLabel MonthlyPaymentLabel; //declare label for Monthly Payment field private static String LoanAmtString=(" Enter the Amount of Mortgage Here: "); //declare text for Loan Amount label private static String LoanSelectionString=(" Select the Term and Interest Rate: "); //declare text for Loan Selection menu private static String MonthlyPaymentString=(" Your Monthly Payment: ");//declare text for Monthly Payment label private static String AvailLoan1=("7 yrs @ 5.35%"); //declare first available loan for menu private static String AvailLoan2=("15 yrs @ 5.5%"); //declare second available loan for menu private static String AvailLoan3=("30 yrs @ 5.75%"); //declare third available loan for menu private static String Balance=("Loan Balance "); //declare string for Loan Balance column of Display Field private static String IntPaid=(" Interest Paid"); //declare string for Interest Paid column of Display Field private TextField LoanAmtField; private TextField MonthlyPaymentField; private TextArea DisplayTextArea; Choice LoanSelectionMenu=new Choice(); public MortPayment_v3() { //create labels for Frame LoanAmtLabel=new JLabel(LoanAmtString); LoanSelectionMenuLabel=new JLabel(LoanSelectionString); MonthlyPaymentLabel=new JLabel(MonthlyPaymentString); //Set labels to textfield LoanAmtLabel.setLabelFor(LoanAmtField); LoanSelectionMenuLabel.setLabelFor(LoanSelectionMenu); MonthlyPaymentLabel.setLabelFor(MonthlyPaymentField); LoanAmtField=new TextField(10); LoanAmtField.setEditable(true); MonthlyPaymentField=new TextField(10); MonthlyPaymentField.setEditable(false); DisplayTextArea = new TextArea (4,25); DisplayTextArea.setEditable(false); //construct panel fieldsPanel=new Panel(); buttonPanel=new Panel(); buttons=new Button[3]; labelsPanel=new Panel(); displayPanel=new Panel(); //construct buttons buttons[0]=new Button("Calculate Payment"); //Calculate Monthly Payment buttons[1]=new Button("Reset"); //Start Over buttons[2]=new Button("Exit Program"); //Exit program //Frame and Panel layouts setLayout (new BorderLayout()); fieldsPanel.setLayout(new GridLayout(4,1,20,15)); buttonPanel.setLayout(new GridLayout(1,3,5,5)); labelsPanel.setLayout(new GridLayout(4,1,10,10)); displayPanel.setLayout(new GridLayout(1,1,0,0)); fieldsPanel.add(LoanAmtField); fieldsPanel.add(LoanSelectionMenu); LoanSelectionMenu.addItem(AvailLoan1); LoanSelectionMenu.addItem(AvailLoan2); LoanSelectionMenu.addItem(AvailLoan3); fieldsPanel.add(MonthlyPaymentField); buttonPanel.add(buttons[0]); buttonPanel.add(buttons[1]); buttonPanel.add(buttons[2]); labelsPanel.add(LoanAmtLabel); labelsPanel.add(LoanSelectionMenuLabel); labelsPanel.add(MonthlyPaymentLabel); displayPanel.add(DisplayTextArea); //add ActionListener to the buttons buttons[0].addActionListener(this); buttons[1].addActionListener(this); buttons[2].addActionListener(this); //Adding the Layout add(fieldsPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); add(labelsPanel, BorderLayout.WEST); add(displayPanel, BorderLayout.EAST); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { //exit program System.exit(0); } } ); } public void actionPerformed(ActionEvent e) { if(e.getSource() == buttons[0]) { selection=LoanSelectionMenu.getSelectedItem(); if(selection.equals("7 yrs @ 5.35%")) { LoanAmt=new Double(LoanAmtField.getText().trim()).doubleValue();//parse LoanAmt to double InterestRate=availLoans[0][1]; int Duration=(int)availLoans[0][2]*12; //convert Duration to int (& months) for calculation double PeriodicInterestRate=(availLoans[0][1]/(12*100)); //declare periodic interest rate for formula calcMonthlyPayment(LoanAmt, PeriodicInterestRate, Duration); } if(selection.equals("15 yrs @ 5.5%")) { LoanAmt=new Double(LoanAmtField.getText().trim()).doubleValue();//parse LoanAmt to double InterestRate=availLoans[1][1]; int Duration=(int)availLoans[1][2]*12; //convert Duration to int (& months) for calculation double PeriodicInterestRate=(availLoans[1][1]/(12*100)); //declare periodic interest rate for formula calcMonthlyPayment(LoanAmt, PeriodicInterestRate, Duration); } if(selection.equals("30 yrs @ 5.75%")) { LoanAmt=new Double(LoanAmtField.getText().trim()).doubleValue();//parse LoanAmt to double InterestRate=availLoans[2][1]; int Duration=(int)availLoans[2][2]*12; //convert Duration to int (& months) for calculation double PeriodicInterestRate=(availLoans[2][1]/(12*100)); //declare periodic interest rate for formula calcMonthlyPayment(LoanAmt, PeriodicInterestRate, Duration); } } else if(e.getSource() == buttons[1]) { clearText=true; //set clear text flag to true for new calculation LoanAmtField.setText(""); //set text field to null string (clear) MonthlyPaymentField.setText(""); //set text field to null string (clear) // DisplayField.setText(""); //set text field to null string (clear) DisplayTextArea.setText(""); LoanAmtField.requestFocus(); //return cursor to LoanAmt field } else if(e.getSource() == buttons[2]) { System.exit(0); } } private void calcMonthlyPayment(double LoanAmt, double PeriodicInterestRate, int Duration) { //calculate Monthly Payment MonthlyPayment=(LoanAmt*PeriodicInterestRate)/(1-Math.pow(1+PeriodicInterestRate,-Duration)); LoanAmtField.setText(pmtPattern.format(LoanAmt)); MonthlyPaymentField.setText(pmtPattern.format(MonthlyPayment)); DisplayTextArea.append(Balance + IntPaid + "\n"); double LoanBalance=LoanAmt; double Interest=0.0; double MonthlyInterest=0.0; int PaymentNumber=0; do {//Start do/while loop PaymentNumber++; MonthlyInterest=LoanBalance*PeriodicInterestRate; Interest+=MonthlyInterest; LoanBalance -=MonthlyPayment-MonthlyInterest; DisplayTextArea.append(pmtPattern.format(LoanBalance)+" "+pmtPattern.format(Interest)+"\n"); System.out.println("After payment "+PaymentNumber+": Loan Balance = "+pmtPattern.format(LoanBalance)+"\tInterest to Date = "+pmtPattern.format(Interest)); } while(PaymentNumber<Duration); } public static void main(String[] args) { MortPayment_v3 frame=new MortPayment_v3(); frame.setTitle("Mortgage Calculator by M.Nunez "); frame.setBounds(200,200,600,200); frame.setVisible(true); } }
I also have a bar chart that I would like to implement into this program but lack the knowledge of how to implement this code into my working mortgage calculator. Please help me!!!!
// Program draws a bar chart. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; //for the class Rectangle2D import javax.swing.*; public class DrawBarChart extends JFrame { //to store the sales of four quarters, and the max among them private double one, two, three, four, max; // constructor prompts the users for the sales of four quarters public DrawBarChart() { //set title for the window super( "Bar Graph Drawing" ); String input = ""; //store the input values // reads the sales of first quarter input = JOptionPane.showInputDialog( this, "Enter sales in the first quarter:" ); //need to convert to numeric value for calculation one = Double.parseDouble( input ); // reads the sales of second quarter input = JOptionPane.showInputDialog( this, "Enter sales in the second quarter:" ); //need to convert to numeric value for calculation two = Double.parseDouble( input ); // reads the sales of third quarter input = JOptionPane.showInputDialog( this, "Enter sales in the third quarter:" ); //need to convert to numeric value for calculation three = Double.parseDouble( input ); // reads the sales of fourth quarter input = JOptionPane.showInputDialog( this, "Enter sales in the fourth quarter:" ); //need to convert to numeric value for calculation four = Double.parseDouble( input ); // find the maximum max = ( one > two ? one : two ); max = ( max > three ? max : three ); max = ( max > four ? max : four ); // set the size of the window and let it be visible setSize( 350, 450 ); setVisible( true ); } // Draw bars of the chart // scale each bar by the maximum value // The method 'paint' will be executed right away after the constructor. public void paint( Graphics g ) { super.paint( g ); // creates a 2D object to draw Graphics2D g2d = ( Graphics2D )g; // set color and draw the bar for the first quarter sales g2d.setColor( Color.red ); g2d.fill( new Rectangle2D.Double( 25, 250 - 200 * one / max, 50, 200 * one / max ) ); // set color and draw the bar for the second quarter sales g2d.setColor( Color.blue ); g2d.fill( new Rectangle2D.Double( 100, 250 - 200 * two / max, 50, 200 * two / max ) ); // set color and draw the bar for the third quarter sales g2d.setColor( Color.green ); g2d.fill( new Rectangle2D.Double( 175, 250 - 200 * three / max, 50, 200 * three / max ) ); // set color and draw the bar for the fourth quarter sales g2d.setColor( Color.yellow ); g2d.fill( new Rectangle2D.Double( 250, 250 - 200 * four / max, 50, 200 * four / max ) ); // set the legends of the bar chart g2d.setColor( Color.black ); g2d.drawString("Legend: ", 20, 280); g2d.setColor( Color.red ); g2d.fillRect(170, 300, 50, 10); g2d.setColor( Color.black ); g2d.drawString("First Quarter: " + one, 20, 310); g2d.setColor( Color.blue ); g2d.fillRect(170, 330, 50, 10); g2d.setColor( Color.black ); g2d.drawString("Second Quarter: " + two, 20, 340); g2d.setColor( Color.green ); g2d.fillRect(170, 360, 50, 10); g2d.setColor( Color.black ); g2d.drawString("Third Quarter: " + three, 20, 370); g2d.setColor( Color.yellow ); g2d.fillRect(170, 390, 50, 10); g2d.setColor( Color.black ); g2d.drawString("Fourth Quarter: " + four, 20, 400); } public static void main( String args[] ) { //create an object with type "DrawBarChart", so the constructor //of the class will be called DrawBarChart bar = new DrawBarChart(); bar.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); bar.setResizable(false); } }