2014-09-28 19 views
-1

Brief:我試圖創建一個基本的應用程序,用戶輸入股票中的手機數量,如果銷售人員銷售手機,庫存水平應該相應下降。一旦達到門檻,應該通知銷售人員。 到目前爲止,我已經創建了一個應用程序,一旦它達到門檻,它會通知銷售人員,但我想要的是,在每次銷售後,它應該通知有多少電話剩餘庫存。如何使用Delegates和EventHandlers檢查庫存量?

問題:如何在每次銷售後檢查輸出庫存中的remaining quantity

例如:如果我有4部手機庫存,如果我賣1,應該說 只有3個手機留在股票,然後2,然後1,然後一條消息,你已經達到了臨界值。

這裏是我的代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Enter the number of phone in stock:"); 
     int s = Convert.ToInt32(Console.ReadLine()); 
     Counter c = new Counter(s); 
     c.ThresholdReached += c_ThresholdReached;  
     for (int w = 0; w < s; w++) 
      { 
       Console.WriteLine("Do you want to buy a phone? Press 'Y' for Yes and Press 'N' for No"); 
       if (Console.ReadKey(true).KeyChar == 'y') 
       { 
         Console.WriteLine("A phone has been sold"); 
         c.Sub(1); 
        // how should I check the remaining stock left now? 
       } 
      } 

    } 
    static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e) 
    { 
     Console.WriteLine("Total {0} phones were sold at {1}.", e.Threshold, 
     e.TimeReached); 
     Console.ReadKey(); 
     Environment.Exit(0); 
    } 
} 
class Counter 
{ 
    private int threshold; 
    private int total; 
    public Counter(int passedThreshold) 
    { 
     threshold = passedThreshold; 
    } 
    public void Sub(int x) 
    { 
     total += x; 
     if (total >= threshold) 
     { 
      ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); 
      args.Threshold = threshold; 
      args.TimeReached = DateTime.Now; 
      OnThresholdReached(args); 
     } 
    } 
    protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) 
    { 
     ThresholdReachedEventHandler handler = ThresholdReached; 
     if (handler != null) 
     { 
      handler(this, e); 
     } 
    } 
    public event ThresholdReachedEventHandler ThresholdReached; 
} 
public class ThresholdReachedEventArgs : EventArgs 
{ 
    public int Threshold { get; set; } 
    public DateTime TimeReached { get; set; } 
} 
    public delegate void ThresholdReachedEventHandler(Object sender, 
ThresholdReachedEventArgs e); 
} 
+0

你的「問題」是一個陳述。你的問題是什麼? – 2014-09-28 15:14:02

+0

你的櫃檯類只是重要的,它不能跟蹤有多少物品存貨。所以當然你不能得到這個信息。你必須先改善班級。 – 2014-09-28 18:11:56

+0

@PeterRitchie **每次銷售後,如何在剩餘電話中顯示剩餘電話的庫存量?** – djinc 2014-09-28 19:16:14

回答

0

這是你如何可以顯示剩餘的手機。如下所示更改counter類。

class Counter 
{ 
    //make them public properties 
    public int Threshold { get; set; } 
    public int Total { get; set; } 

    //rest of the code in counter class.... 

main方法中,使用簡單的Math。下面劃線下c.Sub(1);

Console.WriteLine("total phones left {0}", (c.Threshold - c.Total)); 

我希望這會幫助你。

相關問題