该程序目前正在运行,但我想使其更像面向对象,如使用构造函数等设计,但我不知道从哪里开始以及如何使其工作。我想了解您如何完成此任务的见解和示例?这是一个示例UML,我正在尝试做什么。
原创设计
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(" ", "");
}
使用构造函数的新设计方法
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);
}
答案 0 :(得分: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() {......}
}
答案 1 :(得分:0)
我认为原始设计很有意义。我的意思是ISBN除了被检查之外别无其他目的。我认为将其实现为实例类没有任何好处。
但是出于学术目的,你可能会问自己几个问题: - 你想实现ISBN本身吗?创建一个ISBN类,其中包含其值的属性和检查方法(这是James Curran的解决方案) - 你想知道你检查ISBN的事实吗?创建一个ISBNChecker类,其中包含执行检查的方法 - 两个?将从Console输入创建ISBN实例,并将其作为参数传递给ISBNChecker实例。
通过实现,我的意思是“创建一个实例类”