2012-06-12 94 views
0

我需要的建議是位於代碼底部附近的「Equals」方法。需要注意的是,Equals方法必須包含在Order類中,並且我不能使用自動實現的屬性,以免奇怪爲什麼我不使用它們。 equals方法的功能是搜索當前的所有訂單號(當前有1個用戶和3個自動化)以供複製。我無法弄清楚如何在沒有用戶輸入或不使用大量「if」語句的情況下做到這一點。任何建議都會很棒,謝謝。 ~~底部的其他兩種方法沒有完成的~~方法:搜索類實例

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

} 
+0

除了我的答案,你應該閱讀有關[AutoProperties](http://msdn.microsoft.com/ EN-US /庫/ bb384054.aspx)。 – SimpleVar

回答

0

聽起來像每個Order應該有一個唯一的標識符。 檢查兩個訂單是否是相同的訂單,將比較他們的ID。 檢查兩個訂單是否具有相同的內部狀態,就像一個正常的等於。

如果您想使用他們的ID訪問訂單,您應該將它們保存在字典中。

一般的想法是這樣的,雖然很多是缺少這裏:

public class Order 
{ 
    ... 

    private static Dictionary<int, Order> _orders = new Dictionary<int, Order>(); 

    public static Order Get(int id) 
    { 
     return this._order[id]; 
    } 

    private static object _idLocker = new object(); 
    private static int _nextId = 0; 
    public readonly int Id; 

    public Order() 
    { 
     // Just to make sure that the identifiers are unique, 
     // even if you have threads messing around. 
     lock (_idLocker) 
     { 
      this.Id = _nextId; 
      _nextId++; 
     } 

     _orders.Add(this.Id, this); 
    } 

    public Order(int id) 
    { 
     this.Id = id; 
     _orders.Add(this.Id, this); 
    } 

    public static bool Equals(int id1, int id2) 
    { 
     return _orders[id1].Equals(_orders[id2]); 
    } 

    public bool Equals(Order otherOrder) 
    { 
     // Check whether you have the same inner state as the other order 
    } 
} 
+0

感謝您的代碼片段。我更多地看它,它似乎是一個可以我需要它=) – JRoy