简体   繁体   中英

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. 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. 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. 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). So whenever you add a new order to the orderArray , also add its number to the check array. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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