有没有办法将AddToStock
方法内部的while循环放入单独的ValidateInput
方法中?我也附上了ReadInteger
代码。(类似于ReadDecimal/ReadString/ReadDate(
AddToStock代码:
private static void AddToStock()
{
//Get item id
int itemid = ReadInteger("nItem ID:");
//Confirm item id is greater than 0
while (itemid <= 0)
{
Console.WriteLine("Item ID Cannot Be Less Than 1, Please Try Again");
//Get item id
itemid = ReadInteger("Item ID:");
}
//Get item name
string itemname = ReadString("Item Name:");
//While item name input empty
while (string.IsNullOrWhiteSpace(itemname))
{
Console.WriteLine("You Didn't Enter An Item Name, Please Try Again");
itemname = ReadString("Item Name:");
}
//Get item quantity
int itemquantity = ReadInteger("Quantity:");
//Confirm item quantity is greater than 0
while (itemquantity <= 0)
{
Console.WriteLine("Quantity Cannot Be Less Than 1, Please Try Again");
//Get item quantity
itemquantity = ReadInteger("Quantity:");
}
//Get item price
decimal itemprice = ReadDecimal("Price Paid:");
//Confirm item price is greater than 0
while (itemprice <= 0)
{
Console.WriteLine("Item Price Cannot Be Less Than Or Equal To £0.00, Please Try Again");
//Get item price
itemprice = ReadDecimal("Item Price:");
}
//Get item date added
DateTime itemdate = ReadDate("Date Added:");
//Add item to stock
Employee_UI.AddToStock(itemid, itemname, itemprice, itemquantity, itemdate);
Console.WriteLine("nItem Added To Stock!");
}
ReadInteger代码:
private static int ReadInteger(string prompt)
{
while (true)
{
Console.WriteLine(prompt);
try
{
Console.Write("> ");
return Convert.ToInt32(Console.ReadLine());
}
//If input not integer
catch (Exception)
{
Console.WriteLine("Couldn't Understand That As A Number, Please Try Again");
}
}
}
非常感谢您的帮助!这一切还是新事物:(
不需要创建额外的方法。您可以简单地修改ReadInteger
方法来检查输入整数是否在范围内。
注意:与其使用Convert.ToInt32()
并捕获异常,不如遵循标准的方式,即使用int.TryParse()
方法。
试试这样的东西:
private static int ReadInteger(string prompt, int minimum = int.MinValue)
{
while (true)
{
Console.WriteLine(prompt);
Console.Write("> ");
int value;
if (!int.TryParse(Console.ReadLine(), out value))
{
Console.WriteLine("Couldn't Understand That As A Number, Please Try Again");
}
else if (value < minimum)
{
Console.WriteLine($"The number Cannot Be Less Than {minimum}. Please Try Again");
}
else
{
return value;
}
}
}
用法:
int itemid = ReadInteger("nItem ID:", minimum: 1);
然后,您可以对其他ReadXXXX
方法遵循相同的逻辑。
将内容验证与内容格式错误分开是个好主意。如果你想在内容验证和输入之间进行分离,我会按照如下方式进行。。只需在静态类中完全分离验证即可。在类中,您可以以通用方式测试要验证的类型。。当相同类型的所有字段的条件相似时,类似
public static class StockInputFieldValidator
{
public static bool ValidateItem<T>(object o)
{
if (typeof(T) == typeof(int)) return ((int) o > 0);
if (typeof(T) == typeof(float)) return ((float) o > 0);
if (typeof(T) == typeof(decimal)) return ((decimal) o > 0);
if (typeof(T) == typeof(string)) return !string.IsNullOrWhiteSpace((string) o);
return true;
}
}
public static class ValidatedStockInputReader
{
static void ReadItem(string caption, out string s)
{
while (true)
{
Console.Write(caption + ">");
string result = Console.ReadLine();
if (StockInputFieldValidator.ValidateItem<string>(result))
{
s = result;
return;
}
Console.WriteLine("Please provide valid "+result?.GetType().Name+ " input for " + caption);
}
}
static void ReadItem(string caption, out DateTime s)
{
while (true)
{
Console.Write(caption + ">");
if (DateTime.TryParse(Console.ReadLine(), out DateTime result))
if (StockInputFieldValidator.ValidateItem<DateTime>(result))
{
s = result;
return;
}
else Console.WriteLine("Invalid value for " + caption);
Console.WriteLine("Please provide valid "+ result.GetType().Name+" input for " + caption);
}
}
static void ReadItem(string caption, out int s)
{
while (true)
{
Console.Write(caption + ">");
if (int.TryParse(Console.ReadLine(), out int result))
if (StockInputFieldValidator.ValidateItem<int>(result))
{
s = result;
return;
}
else Console.WriteLine("Invalid value for " + caption);
Console.WriteLine("Please provide valid " + result.GetType().Name + " input for " + caption);
}
}
static void ReadItem(string caption, out decimal s)
{
while (true)
{
Console.Write(caption + ">");
if (decimal.TryParse(Console.ReadLine(), out decimal result))
if (StockInputFieldValidator.ValidateItem<decimal>(result))
{
s = result;
return;
}
else Console.WriteLine("Invalid value for " + caption);
Console.WriteLine("Please provide valid " + result.GetType().Name + " input for " + caption);
}
}
public static void GetStockInput(out int citemid, out string citemname, out int citemquantity, out decimal citemprice, out DateTime citemdate)
{
ReadItem("Item ID:", out citemid);
Console.WriteLine("Input was itemid=" + citemid);
ReadItem("Item Name:", out citemname);
Console.WriteLine("Input was itemname=" + citemname);
ReadItem("Quantity:", out citemquantity);
Console.WriteLine("Input was quantity=" + citemquantity);
ReadItem("Price:", out citemprice);
Console.WriteLine("Input was itempricepaid=" + citemprice);
ReadItem("Date:", out citemdate);
Console.WriteLine("Input was itemdatetime=" + citemdate);
}
}