C# Help Due Tonight

A word from our sponsor:

Printer-friendly version

Author: 

I am having trouble debugging my program I can't get the Exception Handling part to work without breaking my Validation method, would love the help

Hugs :)
Michelle

// Class: CSCI 1630
// Date: 04/05/2016
/* Purpose:
*/
/*
Exercise 3 Notes: MortgageApplication is an application class; its Main() method uses two
variables for storing the name and credit score when the program acquires its input. After
collecting the inputs, the program uses the two values as arguments for a method that determines
whether they qualify for a mortgage or not.

ArguementException is an existing class that derives from Exception; you use it when one or more
of a method's arguements do not fall within an expected range. Create an application for
Merrydale Mortgage Company named MortgageApplication containing variables that can hold an applicant's
name and credit score. Within the classs, create a method that accepts a parameter for the credit
score and returns true or false, indicating whether the applicant is eligible for a mortgage.
If the score is not between 300 and 850, it is invalid, and the method should thrown an ArguementException.
An application is accepted when the credit score is valid and is atleast 650. In the Main() method,
continuosly prompt the user for applicant data, pass it to the method, and then display a message
indicating whether the applicant is accepted, rejected, or has an invalid score.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MortgageApplicationProject
{
public class MortgageApplication
{
public static void Main(string[] args)
{
int creditScore = 0;
string applicantName = "";
char userInput = 'n';

Console.WriteLine("** CSCC Mortgage Company **");
Console.Write("\nWould you like to run this program [y, n]? ");
userInput = Convert.ToChar(Console.ReadLine());

while(!(userInput == 'N' || userInput == 'n'))
{
Console.Write("Please enter the applicant's name: ");
applicantName = Console.ReadLine();
Console.Write("Enter credit score: ");
creditScore = Convert.ToInt32(Console.ReadLine());

try
{
Applicant applicant = new Applicant(applicantName, creditScore, false);
Console.WriteLine("{0} is {1}", applicantName, applicant.Status);
}

catch (ArgumentException anyException)
{
Console.WriteLine(anyException.Message);
}

Console.Write("Would you like to run this program [y, n]? ");
userInput = Convert.ToChar(Console.ReadLine());

}

Console.WriteLine("Please press the key to terminate the program.");
Console.ReadLine();
}
}
}

// Class: CSCI 1630
// Date: 04/05/2016
/* Purpose:
*/
/*
Exercise 3 Notes: MortgageApplication is an application class; its Main() method uses two
variables for storing the name and credit score when the program acquires its input. After
collecting the inputs, the program uses the two values as arguments for a method that determines
whether they qualify for a mortgage or not.

ArguementException is an existing class that derives from Exception; you use it when one or more
of a method's arguements do not fall within an expected range. Create an application for
Merrydale Mortgage Company named MortgageApplication containing variables that can hold an applicant's
name and credit score. Within the classs, create a method that accepts a parameter for the credit
score and returns true or false, indicating whether the applicant is eligible for a mortgage.
If the score is not between 300 and 850, it is invalid, and the method should thrown an ArguementException.
An application is accepted when the credit score is valid and is atleast 650. In the Main() method,
continuosly prompt the user for applicant data, pass it to the method, and then display a message
indicating whether the applicant is accepted, rejected, or has an invalid score.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MortgageApplicationProject
{
public class Applicant
{
public Applicant(string name, int creditscore, bool status)
{
Name = name;
CreditScore = creditscore;
Status = ValidateCreditScore(creditscore);
}

public string Name { get; private set; }
public double CreditScore { get; private set; }
public bool Status { get; private set;}

public bool ValidateCreditScore(int creditscore)
{
try
{
if (creditscore < 300 || creditscore > 850)
{
Console.WriteLine("test");
Console.ReadLine();
throw new ArgumentException("Value does not fall within the expected range.");
}

if (creditscore >= 650)
{
return true;
}

else
{
return false;
}
}

catch(ArgumentException anyException)
{
//public const string message = "Value does not fall within the expected range.";

//throw new ArgumentException("Value does not fall within the expected range.");

Console.WriteLine(anyException.Message);
}

}
}
}

Comments

Just me

Melanie Brown's picture

I would have written the test as

if !(creditscore > 300 && creditscore < 850) {
...exception;
}

because if creditscore == 3 then it's not greater than 850. Because of your logical OR, if only one condition is true, the statement is true.

Melanie

c#

its supposed to be over 850 or less than 300

hugs :)
Michelle SidheElf Amaianna

Logic correct as is

The comparison there is checking for failure conditions, so the or conditional is appropriate. For an and conditional to work there, it would need to be written to test for a successful condition.

In my opinion

the issue is in ValidateCreditScore. Remove the try catch code. When you catch the exception and try to throw it again is where the trouble is. Have the code throw it and have the calling code worry about catching the exception.

Have a good night, heading to bed.

A humble dev,
DrComp

Do you have to have the method in the constructor?

If I were writing this, I would not call the validation method in the class constructor. I would make the method public and have the calling program call the method on the constructed object. Don't put try/catch blocks inside the Applicant class either; let the exception get caught at the calling program level.

BCTS - The one stop shop for everything under the sun?

If someone wants to volunteer to clean up the DB that I'm in the middle of migrating from Oracle to SQLServer then feel free...
Ok, I'm joking but the range of skills available through here is just fantastic.

Samantha

Work-Swap

Piper's picture

I'll fix your database if you re-code all the contributed drupal modules we use to be PHP7.0 or HHVM compliant :P

-Piper


"She was like a butterfly, full of color and vibrancy when she chose to open her wings, yet hardly visible when she closed them."
— Geraldine Brooks