简体   繁体   English

将用户输入与先前输入的值进行比较

[英]Comparing user input to previously entered values

I am having a problem getting my program to check the input previous and compare it so the user cannot duplicate an order number. 我在让我的程序检查之前的输入并进行比较时遇到问题,因此用户无法复制订单号。 Right now the program will run all the way through and look the way I want, but it accepts duplicate order numbers while I would like it to inform the user and ask them to reenter. 现在程序将一直运行并按我想要的方式运行,但它接受重复的订单号,而我希望它通知用户并要求他们重新输入。 The problem seems to be that my check[y] array does not contain any value when I compare it. 问题似乎是我的check [y]数组在比较它时不包含任何值。 How do I get this array to contain the previously entered order numbers so it will display the error message. 如何让此数组包含先前输入的订单号,以便显示错误消息。 I am just going to post the entire code so you can see what I have done so far. 我将发布整个代码,以便您可以看到我到目前为止所做的工作。 Others have suggested the List type, but I am a student and have not learned this yet. 其他人建议列表类型,但我是学生,还没有学到这一点。 I believe I am supposed to use the Equals method. 我相信我应该使用Equals方法。 Any help would be appreciated. 任何帮助,将不胜感激。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

namespace Assignment6_Hergott
{

class Order : IComparable <Order>
{

    public int orderNumber { get; set; }
    public string customerName { get; set; }
    public int quanityOrdered { get; set; }
    public double total;
    public const double priceEach = 19.95;

    public Order()
    {
    }
    public Order(int number, string name, int quanity)
    {
        number = orderNumber;
        name = customerName;
        quanity = quanityOrdered;
    }

    public double totalPrice
    {
        get
        {
            return total;
        }
    }

    public int CompareTo(Order o)
    {
        return this.orderNumber.CompareTo(o.orderNumber);
    }

    public override bool Equals(Object e)
    {
        bool equal;
        Order temp = (Order)e;
        if (orderNumber == temp.orderNumber)
            equal = true;
        else
            equal = false;
        return equal;

    }
    public override int GetHashCode()
    {
        return Convert.ToInt32(orderNumber);
    }
    public override string ToString()
    {
    return "ShippedOrder " + orderNumber + " " + customerName + " " + quanityOrdered + 
    " @ " + priceEach + " each.";

    }

}
class ShippedOrder : Order
{

    public const int shippingFee = 4;

    public override string ToString()
    {
   return base.ToString() + " Shipping is " + shippingFee + " Total is " + totalPrice;
    }
}
class Program
{
    static void Main(string[] args)
    {

        double sum = 0;
        ShippedOrder[] orderArray = new ShippedOrder[5];
        ShippedOrder[] check = new ShippedOrder[5];
        bool wrong = true;
        for (int x = 0; x < orderArray.Length; ++x)
        {

            orderArray[x] = new ShippedOrder(); 

            Console.Write("Enter order number: ");
            orderArray[x].orderNumber = Convert.ToInt32(Console.ReadLine());

            for (int y = 0; y < x; y++)
            {
                check[y] = new ShippedOrder();

                if (orderArray[x].Equals(check[y]))
                    wrong = false;
                    while (!wrong)
                    {
                        Console.WriteLine("Sorry, the order number {0} is a duplicate. 
\nPlease reenter: ", orderArray[x].orderNumber);
                        for (y = 0; y < x; y++)
                        {
                        if (orderArray[x].Equals(check[y]))
                            wrong = false;

                        }
                        check[y] = orderArray[x];
                    }
            }


            Console.Write("Enter cusomer name: ");
            orderArray[x].customerName = Console.ReadLine();

            Console.Write("Enter quanity: ");
            orderArray[x].quanityOrdered = Convert.ToInt32(Console.ReadLine());

            orderArray[x].total = orderArray[x].quanityOrdered * Order.priceEach + 
ShippedOrder.shippingFee;

            sum += orderArray[x].total;

        }
        Array.Sort(orderArray);
        for (int x = 0; x < orderArray.Length; x++)
        {
            Console.WriteLine(orderArray[x].ToString());
        }
        Console.WriteLine();
        Console.WriteLine("Total for all orders is {0:c} ", sum);
    }
}
}

I had a few minutes so I changed my answer to show you one way it could be done. 我有几分钟的时间,所以我改变了答案,向你展示了可以做到的一种方式。 If you just copy/paste this you'll be doing yourself a disservice, though (and your instructor will probably be able to tell). 如果你只是复制/粘贴这个,那么你自己也会受到伤害(而且你的教练可能会告诉你)。 Take a look at the solution and see how it differs from yours. 看看解决方案,看看它与你的不同之处。 I hesitated to post a full solution but I thought this might be an okay way for you to figure out what you'd done wrong. 我犹豫是要发布一个完整的解决方案,但我认为这可能是一个好的方法让你弄清楚你做错了什么。

namespace ConsoleApplication2
{
    using System;
    using System.Linq;

    public class Order : IComparable<Order>
    {
        public const double PriceEach = 19.95;

        public Order()
        {
        }

        public Order(int number, string name, int quanity)
        {
            this.OrderNumber = number;
            this.CustomerName = name;
            this.QuanityOrdered = quanity;
        }

        public int OrderNumber { get; set; }

        public string CustomerName { get; set; }

        public int QuanityOrdered { get; set; }

        public int CompareTo(Order o)
        {
            return this.OrderNumber.CompareTo(o.OrderNumber);
        }

        public override bool Equals(object e)
        {
            int compareTo;
            int.TryParse(e.ToString(), out compareTo);
            return this.OrderNumber == compareTo;
        }

        public override int GetHashCode()
        {
            return Convert.ToInt32(this.OrderNumber);
        }

        public override string ToString()
        {
            return "Shipped order number " + this.OrderNumber + " for customer " + this.CustomerName + " " + this.QuanityOrdered +
            " @ $" + PriceEach + " each.";
        }
    }

    public class ShippedOrder : Order
    {
        public const int ShippingFee = 4;

        public double TotalPrice
        {
            get
            {
                return (this.QuanityOrdered * PriceEach) + ShippingFee;
            }
        }

        public override string ToString()
        {
            return base.ToString() + " Shipping is $" + ShippingFee + ". Total is $" + this.TotalPrice;
        }
    }

    public class Program
    {
        private static readonly int[] OrderNumbers = new int[5];
        private static readonly ShippedOrder[] ShippedOrders = new ShippedOrder[5];

        public static void Main(string[] args)
        {
            double sum = 0;

            for (var i = 0; i < OrderNumbers.Length; i++)
            {
                OrderNumbers[i] = InputOrderNumber();
                var name = InputCustomerName();
                var quantity = InputQuantity();
                ShippedOrders[i] = new ShippedOrder { CustomerName = name, QuanityOrdered = quantity, OrderNumber = OrderNumbers[i] };
                sum += ShippedOrders[i].TotalPrice;
            }

            Array.Sort(ShippedOrders);
            foreach (var t in ShippedOrders)
            {
                Console.WriteLine(t.ToString());
            }

            Console.WriteLine();
            Console.WriteLine("Total for all orders is {0:c} ", sum);
            Console.WriteLine();
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        private static int InputOrderNumber()
        {
            Console.Write("Enter order number: ");
            var parsedOrderNumber = InputNumber();

            if (ShippedOrders.Any(shippedOrder => shippedOrder != null && shippedOrder.OrderNumber.Equals(parsedOrderNumber)))
            {
                Console.WriteLine("Order number {0} is a duplicate.", parsedOrderNumber);
                return InputOrderNumber();
            }

            return parsedOrderNumber;
        }

        private static string InputCustomerName()
        {
            Console.Write("Enter customer name: ");
            var customerName = Console.ReadLine();
            if (customerName == null || string.IsNullOrEmpty(customerName.Trim()))
            {
                Console.WriteLine("Customer name may not be blank.");
                return InputCustomerName();
            }

            return customerName;
        }

        private static int InputQuantity()
        {
            Console.Write("Enter quantity: ");
            return InputNumber();
        }

        private static int InputNumber()
        {
            int parsedInput;
            var input = Console.ReadLine();
            if (!int.TryParse(input, out parsedInput))
            {
                Console.WriteLine("Enter a valid number.");
                return InputNumber();
            }

            return parsedInput;
        }
    }
}

my check[y] array does not contain any value when I compare it. 我的check [y]数组在比较时不包含任何值。 How do I get this array to contain the previously entered order numbers 如何使此数组包含先前输入的订单号

If you want check[] to contain order numbers, it needs to be of the type of order number (an int , in this case). 如果您希望check[]包含订单号,则它必须是订单号的类型(在本例中为int )。 So whenever you add a new order to the orderArray , also add its number to the check array. 因此,无论何时向orderArray添加新订单,还要将其编号添加到check数组中。 Then you can test against earlier numbers. 然后你可以测试早期的数字。

If this doesn't solve your problem, add a follow-up question as a comment telling us what you tried and we can go from there. 如果这不能解决您的问题,请添加一个后续问题作为评论,告诉我们您尝试了什么,我们可以从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM