What I need suggestions for is the “Equals” method located at the near the bottom of the code. To note, the Equals method MUST be contained within the Order class and I cannot use auto-implemented properties in case your wondering why I didn’t use them lol. The function of the equals method is to search ALL current order numbers (There is currently 1 User and 3 automated) for a duplicate. I cannot figure out how I could do this without user input or without the use of a bunch of “if” statements. Any suggestions would be awesome, thanks.
~~ The other two methods at the bottom are no finished~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment3
{
class Program
{
static void Main(string[] args)
{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
// USER CONTROLLED
int ordNum;
Console.Write("Enter Order Number: ");
ordNum = Convert.ToInt16(Console.ReadLine());
string custName;
Console.Write("Enter Customer Name: ");
custName = Console.ReadLine();
int quantity;
Console.Write("Enter Quantity: ");
quantity = Convert.ToInt32(Console.ReadLine());
Order firstOrder = new Order(ordNum, custName, quantity);
Console.WriteLine("Customer: {0}\nOrder Number: {1}\nQuantity" +
"Ordered: {2}\nTotal Price: {3}", firstOrder.Customer, firstOrder.OrderNum, firstOrder.QuantityOrd, firstOrder.Total);
// USER CONTROLLED
// AUTOMATED
// FIRST
int firstOrdNum = 678123;
string firstName ="P Jenkins";
int firstQuantity = 35;
Order firstAutomated = new Order(firstOrdNum, firstName, firstQuantity); // first Instance of Order
// END OF FIRST
// SECOND
int secondOrdNum = 678123;
string secondName = "L Jenkins";
int secondQuantity = 35;
Order secondAutomated = new Order(secondOrdNum, secondName, secondQuantity);
// END OF SECOND
// THIRD
int thirdOrdNum = 49284;
string thirdName = "McDonalds";
int thirdQuantity = 78;
Order thirdAutomated = new Order(thirdOrdNum, thirdName, thirdQuantity);
// END OF THIRD
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
}
class Order
{
private int orderNum;
private string customer;
private int quantityOrd;
public const double amt = 19.95;
private double totalPrice;
// PROPERTIES TO ACCES PRIVATE DATA
public int OrderNum // CHECK
{
get
{
return orderNum;
}
set
{
orderNum = value;
}
}
public string Customer // CHECK
{
get
{
return customer;
}
set
{
customer = value;
}
}
public int QuantityOrd // CHECK
{
get
{
return quantityOrd;
}
set
{
quantityOrd = value;
CalcTotalPrice();
}
}
public double Total // CHECK
{
get
{
return totalPrice;
}
}
// CALCULATE TOTAL
private void CalcTotalPrice()
{
totalPrice = QuantityOrd * amt;
}
// EQUALS METHOD
public void Equals(int ordNum1, int ordNum2)
{
Console.WriteLine("The two orders by P Jenkens (Order Number: {0}) and L Jenkens (Order Number: {1})" +
"are the same order!", ordNum1, ordNum2);
}
public void GetHashCode(string customer, double hashCode)
{
Console.WriteLine("The Hash Code of Customer {0} is {1}", customer, hashCode);
}
public void ToString()
{
}
// CONSTRUCTOR TO ACCEPT VALUES
public Order(int ordNum, string cust, int qntOrd)
{
OrderNum = ordNum;
Customer = cust;
QuantityOrd = qntOrd;
}
}
Sounds like each
Ordershould have a unique Identifier.Checking if two orders are the same order, would be comparing their Id’s.
Checking if two orders have the same inner state, is like a normal Equals.
If you want to access the orders using their Id’s, you should save them in a dictionary.
The general idea would be something like this, although a lot is missing here: