I am trying to figure out a way to search and check an array while filling it up. I want to use the overloaded equals method I have created from another class.
Here is the overloaded equals method in the public class Order : IComparable
public override bool Equals(object obj)
{
Order temp = (Order)obj;
if (orderNumber == temp.orderNumber)
return true;
else
return false;
}
in main, I try to create the 2 arrays so I can compare 1 to the other and check for dups as I go through with a nested for loop but it is not working. It just continues filling the array as if it never saw the second for loop
public static void Main()
{
//Create an array of five ShippedOrder objects.
ShippedOrder[] myShippedOrder = new ShippedOrder[5];
ShippedOrder[] checkDup = new ShippedOrder[5];
//Prompt the user for values for each Orders object; do NOT allow duplicate order numbers and force the user to reenter
// the order when a duplicate order number is entered.
for (int x = 0; x < myShippedOrder.Length; ++x)
{
int y = 0;
myShippedOrder[x] = new ShippedOrder();
checkDup[y] = new ShippedOrder();
Console.Write("Please enter the order number for order {0}: ", x + 1);
checkDup[y].orderNumber = Convert.ToInt32(Console.ReadLine());
for (y = 0; y < checkDup.Length; ++y)
{
if (checkDup[y] != null && myShippedOrder[x].Equals(checkDup[y]))
{
Console.WriteLine("Sorry, this order number has already been entered, please try again");
--x;
break;
}
else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;
}
Console.Write("Please enter the customers name for order {0}: ", x + 1);
myShippedOrder[x].customerName = Console.ReadLine();
Console.Write("Please enter the quantity that was ordered for order {0}: ", x + 1);
myShippedOrder[x].quantityOrdered = Convert.ToInt32(Console.ReadLine());
myShippedOrder[x].totalPrice = myShippedOrder[x].quantityOrdered * PRICEEACH + ShippedOrder.SHIPFEE;
}
any suggestions on how to use the overloaded equal method in the order class to help in finding duplicate orderNumbers if they exist and kick back to the user they entered an invalid order and to try again with that spot in the array?