0

I've had one C# class two semesters ago, now as part of a UML/Object Oriented Analysis and Design class (with a programming prerequisite) I'm trying to take CRC cards and design specs and finalizing the code. However, between one C# class and one Java class, my syntax has all gone to hell.

Here's what I have so far.

Class definition

public class MortgageData
	{
		private int mID;
		private double mPrincipal;
		private double mAPR;
		private int mTerm;
		private double mPayment;

		public MortgageData()
		{
			//do nothing
		}
		public MortgageData(int id, double principal, double apr, int term)
		{
			this.ID = id;
			this.Principal = principal;
			this.APR = apr;
			this.Term = term;
		}
		public int ID
		{
			get
			{
				return mID;
			}
			set
			{
				mID = value;
			}
		}
		public double Principal
		{
			get
			{
				return mPrincipal;
			}
			set
			{
				if (value >= PredefinedMortgages.MIN_PV && value <= PredefinedMortgages.MAX_PV)
				{
					mPrincipal = value;
				}
				else
				{
					mPrincipal = PredefinedMortgages.DEFAULT_PV;
				}
			}
		}
		public double APR
		{
			get
			{
				return mAPR;
			}
			set
			{
				if (value >= PredefinedMortgages.MIN_APR && value <= PredefinedMortgages.MAX_APR)
				{
					mAPR = value;
				}
				else
				{
					mAPR = PredefinedMortgages.DEFAULT_APR;
				}
			}
		}
		public int Term
		{
			get
			{
				return mTerm;
			}
			set
			{
				if(value >= PredefinedMortgages.MIN_TERM && value <= PredefinedMortgages.MAX_TERM)
				{
					mTerm = value;
				}
				else
				{
					mTerm = PredefinedMortgages.DEFAULT_TERM;
				}
			}
		}
		public double Payment
		{
			get
			{
				calculateMortgagePayment();
				return mPayment;
			}
		}
		public string DataString
		{
			get
			{
				return Term + " years @ " + APR + "%";
			}
		}
		public string FullDataString
		{
			get
			{
				return string.Format("{0:c}, Term {1} years @ {2} %", Principal, Term, APR);
			}
		}
		private void calculateMortgagePayment()
		{
			double ir;
			int np;

			try
			{
				ir = APR/12/100;
				np = Term * 12;
				mPayment = (mPrincipal * ir)/(1 - Math.Pow(1 + ir, -np));
			}
			catch (System.InvalidOperationException ex)
			{
				InputUtilities.showErrorMessage(ex, "Unable to calculate mortgage payment");
			}
		}
	}
}

And here's the code I'm working on:

private void findMortgagePayment()
		{
			string prompt;
			bool validPV = false;
			bool validAPR = false;
			bool validTerm = false;
			double principal = 0.0;
			double apr = 0.0 ;
			int term = 0;
            
			try
			{
				//***********************************************************************
//implement input validation and set local variables 				//***********************************************************************

//  This code did not throw any errors, so I assume this was correct

                if (validatePrincipalAmount())
                {
                    principal = Double.Parse(txtMortgageAmount.Text);
                    validPV = true;
                }
                else
                    txtMortgageAmount.Clear();

                if (validateInterestRate())
                {
                    apr = Double.Parse(txtInterestRate.Text);
                    validAPR = true;
                }
                else txtInterestRate.Clear();

                if (validateTerm())
                {
                    term = int.Parse(txtMortgageTerm.Text);
                    validTerm = true;
                }
                else txtMortgageTerm.Clear();          

                
                



                    if (validPV && validAPR && validTerm)
                    {
                        //***********************************************************************
                        //declare and create a new mortgage data object  
                        //invoke this class's displayPayment method
                        //invoke this class's displayAmoritizationTable method
                        //***********************************************************************
                        MortgageData mortgage = new MortgageData();                       

                        mortgage.displayPayment();                       
                           
                        mortgage.displayAmoritizationTable();   

 }
                    else
                    {
                        lblAmount.Text = "";
                        toolTip1.SetToolTip(lblAmount, "");
                        toolTip1.SetToolTip(rtfAmortTable, "");
                        if (!validPV)
                        {
                            setInputErrorField(txtMortgageAmount);
                        }
                        else if (!validAPR)
                        {
                            setInputErrorField(txtInterestRate);
                        }
                        else
                        {
                            setInputErrorField(txtMortgageTerm);
                        }
                    }
			}
			catch (Exception ex)
			{
				prompt = "Please re-enter the mortgage information and try again";
				InputUtilities.showErrorMessage(ex, prompt);
				clearFields();
			}

		}
		private void setInputErrorField(TextBox txtBox)
		{
			string prompt = "Please re-enter the " + txtBox.Tag + " information";
			InputUtilities.showErrorMessage(prompt, "Invalid " + txtBox.Tag);
			txtBox.Focus();
			txtBox.SelectAll();
		}
		private void displayPayment(MortgageData mortgage)
		{
			lblAmount.Text = String.Format("{0:c}", mortgage.Payment);
			toolTip1.SetToolTip(lblAmount, "Monthly payment is: " + lblAmount.Text);
		}
		private void displayAmoritizationTable(MortgageData mortgage)
		{
			string tableOutput = "";

			//***********************************************************************
			//create AmortizationTable here
			//retreive the Amoritization table
			//***********************************************************************

			rtfAmortTable.Text = tableOutput;
			toolTip1.SetToolTip(rtfAmortTable, "Amortization Table for " + mortgage.FullDataString);
		}


		#endregion

		#region "Form Setup"

		private void MortgageInformationInput_Load(object sender, System.EventArgs e)
		{
			SetDefaultMortgageInformation();
		}

		private void SetDefaultMortgageInformation()
		{
			string tlTip;
			
			//set principle value defaults and tool tips
			tlTip = "Set Principle value to a number greater than " + PredefinedMortgages.MIN_PV + " and less than or equal to " + PredefinedMortgages.MAX_PV;
			toolTip1.SetToolTip(txtMortgageAmount, tlTip);

			//set interest defaults and tool tips
			tlTip = "Set APR value to a number greater than or equal to " + PredefinedMortgages.MIN_APR + " and less than or equal to " + PredefinedMortgages.MAX_APR;
			toolTip1.SetToolTip(txtInterestRate, tlTip);

			//set term defaults and tool tips
			tlTip = "Set Term value to a number greater than " + PredefinedMortgages.MIN_TERM + " and less than or equal to " + PredefinedMortgages.MAX_TERM;
			toolTip1.SetToolTip(txtMortgageTerm, tlTip);

			radDefault0.Text = PredefinedMortgages.DefaultMortgageData[0].DataString;
			tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[0].FullDataString;
			toolTip1.SetToolTip(radDefault0, tlTip);

			radDefault1.Text = PredefinedMortgages.DefaultMortgageData[1].DataString;
			tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[1].FullDataString;
			toolTip1.SetToolTip(radDefault1, tlTip);

			radDefault2.Text = PredefinedMortgages.DefaultMortgageData[2].DataString;
			tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[2].FullDataString;
			toolTip1.SetToolTip(radDefault2, tlTip);

			radDefault0.Checked = true;

		}
		#endregion

		#region "Field Utility Operations"
			
		private void clearFields()
		{
			try
			{
				txtMortgageAmount.Clear();
				txtInterestRate.Clear();
				txtMortgageTerm.Clear();
				rtfAmortTable.Clear();
				clearErrorProvider();

				clearPayment();
			}
			catch(System.Exception ex)
			{
				InputUtilities.showErrorMessage(ex, "Delete loan field information and try again");
			}
			finally
			{
				txtMortgageAmount.Focus();
			}
		}
		private void clearErrorProvider()
		{
			try
			{
				errorProvider1.SetError(txtMortgageAmount, "");
				errorProvider1.SetError(txtInterestRate, "");
				errorProvider1.SetError(txtMortgageTerm, "");
				errorProvider1.SetError(rtfAmortTable, "");
			}
			catch(System.Exception ex)
			{
				InputUtilities.clearException(ex);
			}
		}
		private void clearPayment()
		{
			lblAmount.Text = "";
			toolTip1.SetToolTip(lblAmount, "");
		}

		#endregion

		#region "Predefined Mortgage Operations"

		
		private void radDefault0_CheckedChanged(object sender, System.EventArgs e)
		{
			if (radDefault0.Checked)
			{
				setMortgageInformation(0);
			}
		}
		private void radDefault1_CheckedChanged(object sender, System.EventArgs e)
		{
			if (radDefault1.Checked)
			{
				setMortgageInformation(1);
			}
		}

		private void radDefault2_CheckedChanged(object sender, System.EventArgs e)
		{
			if (radDefault2.Checked)
			{
				setMortgageInformation(2);
			}
		}

		private void setMortgageInformation(int selectedMortgage)
		{
			try
			{
				clearFields();
				if ((selectedMortgage >= PredefinedMortgages.FirstMortgageNumber) && (selectedMortgage <= PredefinedMortgages.LastMortgageNumber))
				{
					txtMortgageAmount.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Principal.ToString();
					txtInterestRate.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].APR.ToString();
					txtMortgageTerm.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Term.ToString();
				}
				else
				{
					txtMortgageAmount.Text = PredefinedMortgages.DEFAULT_PV.ToString();
					txtInterestRate.Text = PredefinedMortgages.DEFAULT_PV.ToString();
					txtMortgageTerm.Text = PredefinedMortgages.DEFAULT_TERM.ToString();
				}
				clearErrorProvider();
			}
			catch(System.Exception  ex)
			{
				InputUtilities.showErrorMessage(ex, "Unable to set mortgage default mortgage information.");
			}
		}

		#endregion

		#region "Input Validation"

		private bool nonNumberEntered;

		private void txtMortgageAmount_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = !validatePrincipalAmount();
		}

		private bool validatePrincipalAmount()
		{
			return validateInputField(txtMortgageAmount, PredefinedMortgages.MIN_PV, PredefinedMortgages.MAX_PV);
		}
		
		private void txtInterestRate_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = !validateInterestRate();
		}
		private bool validateInterestRate()
		{
			return validateInputField(txtInterestRate, PredefinedMortgages.MIN_APR, PredefinedMortgages.MAX_APR);
		}

		private bool validateTerm()
		{
			return validateInputField(txtMortgageTerm, (double) PredefinedMortgages.MIN_TERM, (double) PredefinedMortgages.MAX_TERM);
		}

		private bool validateInputField(TextBox txtBox, double min, double max)
		{
			double val;
			string errorMessage;
			bool valid;

			errorMessage = "Enter a number greater than or equal to " + min
				+ " and less than or equal to " + max
				+ " in the " + txtBox.Tag;

			try
			{
				errorProvider1.SetError(txtBox, "");
				if (txtBox.Text == "")
				{
					errorProvider1.SetError(txtBox, errorMessage);
					valid = false;
				}
				else
				{
					val = Double.Parse(txtBox.Text);
					if (val < min || val > max)
					{
						errorProvider1.SetError(txtBox, errorMessage);
						valid = false;
					}
					else
					{
						errorProvider1.SetError(txtBox, "");
						valid = true;
					}
				}
			}
			catch(Exception ex)
			{
				InputUtilities.showErrorMessage(ex, errorMessage);
				valid = false;
				txtBox.SelectAll();
			}
			return valid;
		}

		private void txtInputField_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			//stop the character from being entered if not a navigation key or a number
			if (nonNumberEntered == true) 
			{
				e.Handled = true;
			}
		}

		private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			TextBox txtBox;
			//if a navigation key do not react
			try
			{
				txtBox = (TextBox)(sender);
				if (InputUtilities.isNavigationKey(e))
				{
					errorProvider1.SetError(txtBox, "");
					//allow navigation keys to be entered
					nonNumberEntered = false;
					e.Handled = false;
				}
				else if (InputUtilities.isNumericKey(e))
				{
					errorProvider1.SetError(txtBox, "");
					nonNumberEntered = false;
					e.Handled = false;
				}
				else
				{
					string errorMessage = "Please enter only numeric values for the " + txtBox.Tag;
					errorProvider1.SetError(txtBox, errorMessage);
					//stop value form being entered since it is non-numeric
					nonNumberEntered = true;
					e.Handled = true;
				}
			}
			catch (Exception ex)
			{
				//do nothing, catch the error later before calculations are done
				InputUtilities.clearException(ex);
			}
		}
		#endregion

		
	}
}
                  
                       

                    }

I'm in a java/c# syntax fog and just can't get this to work.

I get this error:
Error 1 'MortgageCalculator_Complete.MortgageData' does not contain a definition for 'displayPayment' and no extension method 'displayPayment' accepting a first argument of type 'MortgageCalculator_Complete.MortgageData' could be found (are you missing a using directive or an assembly reference?)

Help!!
I'm at lines 53-57 of the second code block.

a nudge in the right direction would be peachy!

Thanks

Jim

Edited by revjim44: clarification

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
1

What it's trying to tell you is that the class MortgageData doesn't have a method named 'displayPayment'. Since it doesn't have one, you can't call it.

Now I'm not sure what class your second block of code is in, but it does have a displayPayment method and if that was the one you intended to call, then you just need displayPayment(mortgage); .

You'll have the same problem on the next line.

Votes + Comments
Good Eye! :) This is where his problem lies!!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.