1

The program works in its current for but I would like to make it more object oriented like its design using constructors and such but I don't know where to start and how to make it work. I would like your insight and examples on how you would accomplish this task? Here is a sample UML for what I am trying to do. UML

Original Design

  public static class IsbnConsole
{
   public static void Main(string[] args)
    {
        Console.Write("Enter a valid 10 digit ISBN Number ");
        string isbn = checkIsbnClass.DestabilizeIsbn(Console.ReadLine()); // Normalizes the input and puts it on string "str"
        if (isbn.Length > 10 || isbn.Length < 9) // If the string length is greather than 10, or smaller than 9
        {
            Console.WriteLine("The number you have entered is not a valid ISBN try again."); // Print invalid number
            Console.ReadLine();
        }
        else if (isbn.Length == 10) // If the length is 10
        {
            if (checkIsbnClass.CheckNumber(isbn)) // If function CheckNum return "true"...
                Console.WriteLine("The number you have entered is a Valid ISBN");

            else // If it returns "false"...
                Console.WriteLine("The number you have entered is not a valid ISBN try again.");
                Console.ReadLine();
        }
        else // Else (If the number is NOT greater than 10 or smaller than 9, NOR is it 10 -> If the number is 9)
        {
            Console.WriteLine("The Check digit that corresponds to this ISBN number is " + checkIsbnClass.CheckIsbn(isbn) + "."); // Print the checksum digit
            Console.ReadLine();
        }

  public static class checkIsbnClass
{ 

public static string CheckIsbn(string isbn) // Calculates the 10th digit of a 9-digits partial ISBN number
    {
        int sum = 0;
        for (int i = 0; i < 9; i++) // For each number...
        {
            sum += int.Parse(isbn[i].ToString()) * (i + 1); // ...Multiply the number by it's location in the string
        }
        if ((sum % 11) == 10) // If the remainder equals to 10...
        {
            return "x"; // Output X
        }
        else // If it does not equal to 10...
        {
            return (sum % 11).ToString(); // Output the number
        }
    }
  public static bool CheckNumber(string isbn) // Checks if the checksum digit is correct
    {
        if (isbn[9].ToString() == CheckIsbn(isbn)) // If the 10th digit of the number is the same as the calculated digit...
            return true;
        else // If they're not the same...
            return false;
    }
  public static string DestabilizeIsbn(string isbn) // replace the string
  {
      return isbn.Replace("-", "").Replace(" ", "");
  }

Newly Designed Method Using Constructors

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

        }

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);
    }
8
  • Your last almost duplicate question was closed because it was impossible to answer, this one isn't much better. "Using constructors and getters/setters" does not make a design more or less object oriented. You can easily make your functions instance (instead of static), add getters and setters, and add constructors without improving or even substantially altering the design. What are you actually trying to accomplish? WHY do you want to "add constructors"? Commented Sep 7, 2010 at 14:02
  • I am trying to use this same code but make it use constructors, pretty much I want to learn another way of doing what I have already accomplished. Ex. public class isbn { //attributes private string isbnNum; //method public string GetIsbn() { return this.isbnNum; } Commented Sep 7, 2010 at 14:05
  • Sorry for the duplicate but I don't know how to explain it very well I would like to take my original code and change the functions to use a constructor and I would like to know if it is possible to use setters and getters to get the same result? Commented Sep 7, 2010 at 14:07
  • Sure, it's possible. The little snippet in your comment is a start. Is there some way we can help besides literally rewriting the whole thing for you? (I think that people are also hesitant because, outside of homework, having multiple classes for this functionality in C# is big overkill and would be a worse design.) Commented Sep 7, 2010 at 14:11
  • 2
    Can you explain why you want to make anything "more object-oriented"? Being "OO" is not in of itself a good thing; OO is goodness largely because it promotes lowering the costs of designing and maintaining large-scale versionable software implemented by large teams. I would not take the risk of breaking existing, working, debugged code in order to change its style; that's unlikely to delight customers. If the code already works and people know how to use it, don't change it. Commented Sep 7, 2010 at 14:59

2 Answers 2

2
public static class IsbnConsole 
{ 
   public static void Main(string[] args) 
    { 
        Console.Write("Enter a valid 10 digit ISBN Number "); 
        string isbn = checkIsbnClass.DestabilizeIsbn(Console.ReadLine()); // Normalizes the input and puts it on string "str" 

        Isbn isbn = new Isbn(Console.In)
        if (!isbn.CheckLength())   
            Console.WriteLine("The number you have entered is not a valid ISBN try again."); // Print invalid number 
        } 
        else if (isbn.HasCheckDigit)
        { 
            if (isbn.CheckNumber(isbn)) 
                Console.WriteLine("The number you have entered is a Valid ISBN"); 
            else
                Console.WriteLine("The number you have entered is not a valid ISBN try again."); // Print invalid number 
        } 
        else 
        { 
            Console.WriteLine("The Check digit that corresponds to this ISBN number is " + isbn.GetCheckDigit(isbn) + "."); // Print the checksum digit 
        } 
        Console.ReadLine(); 
}

public class Isbn
{
   public Isbn(TextReader cin)
   {
        /// do stuff here.
   }

   public bool CheckLength()
   {
        /// do stuff here.
   }

   public bool HasCheckDigit {  get {    ..... } }
   public int  GetCheckDigit() {..... }
   public bool CheckNumber() {......}
}
Sign up to request clarification or add additional context in comments.

Comments

0

I do think the original design makes sense. I mean the ISBN has no other purpose than to be checked. I don't see any benefit in trying to materialize it as an instance class.

But just for academic purposes, you may ask yourself several questions here: - Do you want to materialize the ISBN itself ? Create an ISBN class containing properties for its value and methods for the checks (this is James Curran's solution) - Do you want to materialize the fact that you check an ISBN ? Create an ISBNChecker class that will contain methods to perform the checks - Both ? an ISBN instance will be created from the Console input and pass as a parameter to the ISBNChecker instance.

By materialize, I mean "create an instance class"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.