0

我在让我的程序检查之前的输入并进行比较时遇到问题,因此用户无法复制订单号。现在该程序将一直运行并按照我想要的方式运行,但它接受重复的订单号,而我希望它通知用户并要求他们重新输入。问题似乎是我的 check[y] 数组在比较时不包含任何值。如何让这个数组包含先前输入的订单号,以便显示错误消息。我只是要发布整个代码,这样你就可以看到我到目前为止所做的事情。其他人建议使用 List 类型,但我是学生,还没有学过。我相信我应该使用 Equals 方法。任何帮助,将不胜感激。

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

2 回答 2

2

我有几分钟的时间,所以我更改了答案,向您展示了一种可以完成的方法。但是,如果您只是复制/粘贴此内容,您将对自己造成伤害(您的老师可能会说出来)。查看解决方案,看看它与您的解决方案有何不同。我犹豫是否要发布完整的解决方案,但我认为这可能是您找出自己做错了什么的好方法。

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;
        }
    }
}
于 2013-04-17T20:41:46.177 回答
0

当我比较它时,我的 check[y] 数组不包含任何值。如何让这个数组包含之前输入的订单号

如果你想check[]包含订单号,它需要是订单号的类型(int在这种情况下是一个)。因此,每当您向 中添加新订单时orderArray,也将其编号添加到check数组中。然后,您可以针对较早的数字进行测试。

如果这不能解决您的问题,请添加一个后续问题作为评论,告诉我们您尝试了什么,我们可以从那里开始。

于 2013-04-17T20:16:41.853 回答