问题
I'm trying to make a program that check the ISBN number and sees if it has the correct check digit, if it doesn't have the check digit it will add one. I have an Idea of how it will work I just cant figure out how to code for it as in the classes inheriting from each other. This is an in class example that is not going to be graded it is just to familiarize us with getting our designs to a working program. here's what I have so far mind you this is a simple console program.
Code Updated
public class isbn
{ //attributes
private string isbnNum;
//method
public string GetIsbn()
{
return this.isbnNum;
}
//constructor
public isbn()
{
Console.Write("Enter Your ISBN Number: ");
this.isbnNum = Console.ReadLine();
}//end default constructor
//method
public string displayISBN()
{
return this.GetIsbn();
}
public static void Main(string[] args)
{
//create a new instance of the ISBN/book class
isbn myFavoriteBook = new isbn();
//contains the method for checking validity
bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());
//print out the results of the validity.
Console.WriteLine(string.Format("Your book {0} a valid ISBN",
isValid ? "has" : "doesn't have"));
Console.ReadLine();
}//end main
This is the check digit code the professor provided in class we just have to mesh it up to get it to work. I know this goes in the check digit class what I don't know is how to incorporate it into code.
Code Updated
public static class CheckDigit
{ // attributes
public static string NormalizeIsbn(string isbn)
{
return isbn.Replace("-", "").Replace(" ", "");
}
public static bool CheckIsbn(string isbn) // formula to check ISBN's validity
{
if (isbn == null)
return false;
isbn = NormalizeIsbn (isbn);
if (isbn.Length != 10)
return false;
int result;
for (int i = 0; i < 9; i++)
if (!int.TryParse(isbn[i].ToString(), out result))
return false;
int sum = 0;
for (int i = 0; i < 9; i++)
sum += (i + 1) * int.Parse(isbn[i].ToString());
int remainder = sum % 11;
if (remainder == 10)
return isbn[9] == 'X';
else
return isbn[9] == (char)('0' + remainder);
}
回答1:
It sounds like the class CheckDigit
is the business rules validator for ISBN numbers.
In that case:
public static class CheckDigit
{
public static bool CheckIsbn(string isbn)
{
//implementation as in your question.
}
}
Now write a new application (here it's a console app) that uses both of your classes.
class MyConsoleApp
{
static void Main(string[] args)
{
//create a new instance of the ISBN/book class. you're prompted as part
//of the constructor.
isbn myFavoriteBook = new isbn();
//new class contains the method for checking validity
bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());
//write out the results of the validity.
Console.WriteLine(string.Format("Your book {0} a valid ISBN",
isValid ? "has" : "doesn't have"));
Console.ReadLine();
}
}
Here's what's happening:
- we create a new instance of isbn/book. Its constructor has the Console.Readline command to ask the user for input. It then stores the user's entry into
this.isbnNum
. - our new static class
CheckDigit
is simply a validator of any given string. It determines whether the argument sent is a valid ISBN number. It'll return a bool. We sent it theisbn.GetIsbn()
, which is what the user entered. - the bool returned from the
CheckIsbn()
is displayed nicely in a sentence for the user in the console.
So really there are 2 main classes - isbn
and the CheckDigit
. The other Main(string[] args)
can be removed from your code.
Here's the entire console app in one file. Paste into your application, and you can see what's happening.
Is that the help you were looking for? Either way, leave a comment, and we can get it sorted out for you.
Updates:
- The
CheckIsbn
really only does 1 thing - returns whether the 9th character is an X or some other number. It doesn't modify the ISBN from the user, as it stands today. If you wanted to maintain that formatting (removing dashes, spaces), and otherwise modify the input ISBN, then you could specify the ISBN as anout
parameter.
Redefine your method like this if you want the ISBN entered by the user, to retain any changes made within the method CheckIsbn
:
public static bool CheckIsbn(out string isbn)
来源:https://stackoverflow.com/questions/3595217/validate-a-10-digit-isbn-number-erd-included