2016-04-19 12 views
0

私はこのコードを書いたので、自動販売機の複数のキャンディーから選択するようにユーザーに依頼したいと思います。現在、このコードは1つのキャンディーバーを表しています。私は配列を宣言する必要があります知っているが、私はキャンディーバーを選択し、選択を返すようにユーザーに依頼する方法がわからない:C#.NET:配列を追加してユーザーに入力を求める方法は?

namespace CandybarVendingMachine 
{ 

    public class VendingMachine 
    { 
     // private instance variables 
     private int _numQuarters, _numCandyBars; 

     // Public noarg constructor 
     public VendingMachine() 
     { 
      // Set the candy bar cost in quarters. 
      _numQuarters = 3; 

      // Set initial number of candy bars. 
      _numCandyBars = 5; 
     } 

     // Public read only properties: 

     public int NumQuarters 
     { 
      get { return _numQuarters; } 
     } 

     public int NumCandyBars 
     { 
      get { return _numCandyBars; } 
     } 

     // Public methods: 

     public string DepositQuarter() 
     { 
      _numQuarters++; 
      return "Quarter deposited."; 
     } 

     public string SelectCandy() 
     { 
      if (_numQuarters >= 3 && _numCandyBars > 0) 
      { 
       _numQuarters = _numQuarters -3; 
       _numCandyBars--; 
       return "Candy bar dispensed"; 
      } 
      else if (_numCandyBars > 0) 
      { 
       return "Not enough quarters to buy candy bar."; 
      } 
      else 
      { 
       return "No candy bars in machine."; 
      } 
     } 

     public override string ToString() 
     { 

      return "Total Quarters: " + _numQuarters + " Total Bars:" + _numCandyBars; 
     } 
    } 

} 

私は配列

private int[] _numCandyBars; 

public VendingMachine() 
{ 
//Number of quarters require to buy candybar 
_numQuarters = 3; 

//Candybar array contains 3 candy bars 
_numCandyBars = new int[3]; 
_numCandyBars[0] = 5; 
_numCandyBars[1] = 4; 
_numCandyBars[2] = 3; 

} 

を取得する初期化することができます知っています私はキャンディーバーを選択するようにユーザーに頼むことができますか?

+0

ユーザーはどのように回答を入力すると思われますか?それらをコンソールに入力したいのですか、あるいはデータを入力する別の方法がありますか? –

+0

今のところユーザーはコンソールにそれを入力する必要がありますが、このアプリケーションの2番目の部分はC#WPFで実装され、ユーザーは3つのキャンディの画像を見ることができ、各キャンディの下にある – Desi4u

+0

あなたはhttps://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspxを見てきましたか?私にとっては、これを実装する場所と方法は不明です。あなたはどこかでメインの方法を持っていますか? –

答えて

0

ここで私は本当に素早く(私は退屈していた)突っ込んだものです。私はそれをテストしていないが、それは動作するはずです。あなたが本当に必要とするのは、その実装を書くことだけです。ユーザとあなたのためのメニューを印刷する機能が必要です;アイテムをスロットに追加するコードを書く必要があります。残りはほとんどあなたのためにそこにあるはずです。私はそれをさらに強化していくつもりですが、以下のいずれかまたはすべてを自由に使用してください。

namespace VendingMachine 
{ 
    public class Vend 
    { 
     private string[] VALID_ROWS = new string[] { "A", "B", "C", "D", "E", "F" }; 
     private string[] VALID_COLS = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }; 

     public Vend() 
     { 
      ProductSlot = new Dictionary<string, Dictionary<string, Item>>(); 
      foreach(string row in VALID_ROWS) 
      { 
       ProductSlot.Add(row, new Dictionary<string,Item>()); 
       foreach(string col in VALID_COLS) 
        ProductSlot[row].Add(col, null); 
      } 
     } 

     public void AddProduct(string row, string col, Item item) 
     { 
      if (!VALID_ROWS.Contains(row)) throw new ArgumentOutOfRangeException("row", string.Format("The row provided: `{0}` is not valid.", row)); 
      if (!VALID_COLS.Contains(col)) throw new ArgumentOutOfRangeException("col", string.Format("The column provided: `{0}` is not valid.", col)); 
      ProductSlot[row][col] = item; 
     } 

     public double GetPrice(string row, string col) 
     { 
      double price = GetProduct(row, col).Price; 
      return price; 
     } 

     public string GetProductName(string row, string col) 
     { 
      string name = GetProduct(row, col).ProductName; 
      return name; 
     } 

     public int GetQuantity(string row, string col) 
     { 
      int qty = GetProduct(row, col).Quantity; 
      return qty; 
     } 

     public Item PurchaseItem(string row, string col) 
     { 
      Item item = GetProduct(row, col); 
      if (item.Quantity < 1) throw new OutOfStockException("This item is out of stock, please select another item."); 
      if (CreditMoney < item.Price) throw new InsufficientFundsException("There is not enough money to purchase this item."); 
      item.Quantity--; 
      CreditMoney -= item.Price; 
      return item; 
     } 

     public string PrintProductList() 
     { 
      StringBuilder sb = new StringBuilder(); 
      foreach (string row in VALID_ROWS) 
      { 
       foreach (string col in VALID_COLS) 
       { 
        Item item = GetProduct(row, col); 
        if (item == null) continue; 
        sb.AppendLine(string.Format("\t{0} : {1} @ {2} ea.\t{3}", string.Concat(row, col), item.Quantity.ToString(), item.Price.ToString("C2"), item.ProductName)); 
       } 
      } 
      return sb.ToString(); 
     } 

     public Item GetProduct(string row, string col) 
     { 
      if (!VALID_ROWS.Contains(row)) throw new ArgumentOutOfRangeException("row", string.Format("The row provided: `{0}` is not valid.", row)); 
      if (!VALID_COLS.Contains(col)) throw new ArgumentOutOfRangeException("col", string.Format("The column provided: `{0}` is not valid.", col)); 
      Item item = ProductSlot[row][col]; 
      return item; 
     } 

     double _creditMoney = 0.00; 
     public double CreditMoney 
     { 
      get { return _creditMoney; } 
      set 
      { 
       if (value < 0) throw new ArgumentOutOfRangeException("CreditMoney", "CreditMoney cannot be reduced to below $0.00."); 
       _creditMoney = value; 
      } 
     } 
     public Dictionary<string, Dictionary<string, Item>> ProductSlot { get; set; } 

     public class Item 
     { 
      public Item() { } 
      public Item(string name, double price, int qty) 
      { 
       ProductName = name; 
       Price = price; 
       Quantity = qty; 
      } 
      private int _quantity = 0; 
      public int Quantity 
      { 
       get { return _quantity; } 
       set 
       { 
        if (value < 0) throw new ArgumentOutOfRangeException("Quantity", "Quantity cannot be less than zero."); 
        _quantity = value; 
       } 
      } 
      public string ProductName { get; set; } 
      public double Price { get; set; } 
     } 

     public class InsufficientFundsException : Exception 
     { 
      public InsufficientFundsException() { } 
      public InsufficientFundsException(string message) 
       : base(message) { } 
      public InsufficientFundsException(string message, Exception innerException) 
       : base(message, innerException) { } 

     } 

     public class OutOfStockException : Exception 
     { 
      public OutOfStockException() { } 
      public OutOfStockException(string message) 
       : base(message) { } 
      public OutOfStockException(string message, Exception innerException) 
       : base(message, innerException) { } 

     } 
    } 
} 
関連する問題