验证包含的10位ISBN号码 [英] Validate a 10 digit ISBN Number ERD Included

查看:472
本文介绍了验证包含的10位ISBN号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个程序,检查ISBN号码,看看它是否有正确的校验位,如果没有校验位,它将添加一个。我有一个想法,如何将工作,我只是不能弄清楚如何编码,如在彼此继承的类。
这是一个类的例子,不会被分级,只是为了熟悉我们的设计到一个工作程序。这是我现在所想到的你这是一个简单的控制台程序。





代码已更新 public class isbn
{// attributes
private string isbnNum;
//方法
public string GetIsbn()
{
return this.isbnNum;
}
//构造函数
public isbn()
{
Console.Write(输入您的ISBN号码);
this.isbnNum = Console.ReadLine();

} //结束默认构造函数

//方法
public string displayISBN()
{

返回这个。 GetIsbn();

}


public static void Main(string [] args)
{
//创建ISBN / book class

isbn myFavoriteBook = new isbn();

//包含检查有效性的方法
bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());

//打印出有效性的结果。
Console.WriteLine(string.Format(Your book {0} a valid ISBN,
isValid?has:没有));

Console.ReadLine();

} // end main

这是校验码在类中提供,我们只需要网格化它来工作。我知道这是在校验数字类中,我不知道是如何将它整合到代码中。



代码更新 >

  public static class CheckDigit 
{// attributes
public static string NormalizeIsbn(string isbn)
{
return isbn.Replace( - ,).Replace(,);
}
public static bool CheckIsbn(string isbn)//检查ISBN有效性的公式
{
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);
}


解决方案

code> CheckDigit 是ISBN号的业务规则验证器。



在这种情况下:

  public static class CheckDigit 
{
public static bool CheckIsbn(string isbn)
{
//实现就像你的问题。
}
}

现在写一个新的应用程序)使用两个类。

  class MyConsoleApp 
{
static void Main(string [] args)
{
//创建ISBN / book类的新实例。您将被提示为构造函数的
//部分。
isbn myFavoriteBook = new isbn();

//新类包含检查有效性的方法
bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());

//写出有效性的结果。
Console.WriteLine(string.Format(Your book {0} a valid ISBN,
isValid?has:没有));

Console.ReadLine();
}
}

这是发生了什么:




  • 我们创建一个新的isbn / book实例。它的构造函数有Console.Readline命令要求用户输入。然后将用户的条目存储到 this.isbnNum

  • 我们的新静态类 CheckDigit 只是任何给定字符串的验证器。它确定发送的参数是否是有效的ISBN号。它会返回一个bool。我们发送了 isbn.GetIsbn(),这是用户输入的。

  • 从<$ c $



因此,真正的用户可以在控制台中以一个句子很好地显示CheckIsbn()有两个主要类 - isbn CheckDigit



这里是 http://pastebin.com/9VeGvshU =nofollow>整个控制台应用程序在一个文件。粘贴到您的应用程序中,您可以看到发生了什么。



这是您要找的帮助?



更新




  • CheckIsbn 真的只有一件事 - 返回第9个字符是X还是其他数字。它不修改来自用户的ISBN,因为它是今天。如果您想要维护该格式(删除破折号,空格),否则修改输入的ISBN,则可以将ISBN指定为 out 参数。



如果想要用户输入的ISBN,请重新定义方法,以保留方法中所做的任何更改 CheckIsbn

  public static bool CheckIsbn(out string isbn)


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);
    }

解决方案

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 the isbn.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 an out 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) 

这篇关于验证包含的10位ISBN号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆