2016-04-19 4 views
0

Ich habe diesen Code geschrieben und jetzt möchte ich den Benutzer bitten, aus mehreren Süßigkeiten aus einem Automaten auszuwählen. Derzeit stellt dieser Code nur einen Schokoriegel dar. Ich weiß, ich habe ein Array zu erklären, aber ich weiß nicht, wie die Benutzer zu fragen, einen Schokoriegel zu wählen und die Auswahl zurück:C#. NET: Wie Array hinzufügen und Benutzer für die Eingabe fragen?

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

} 

Ich weiß, ich get Array

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; 

} 

initialisieren Ich muss wissen, wie kann ich den Benutzer bitten, einen Schokoriegel auszuwählen?

+0

Wie erwarten Sie, dass Benutzer die Antwort eingeben? Möchten Sie, dass sie in die Konsole eingegeben werden oder haben Sie eine andere Möglichkeit, die Daten einzugeben? –

+0

für jetzt Benutzer sollte es in der Konsole eingeben, aber der zweite Teil dieser Anwendung wird in C# WPF implementiert werden, wo Benutzer in der Lage sein wird, Bild von 3 Bonbons zu sehen und sie können auf die Schaltfläche unter jeder Süßigkeit klicken, um diese Süßigkeiten zu wählen. – Desi4u

+0

Haben Sie in https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx geschaut? Für mich ist unklar, wo/wie Sie dies implementieren möchten. Hast du irgendwo eine Hauptmethode? –

Antwort

0

Hier ist etwas, was ich wirklich schnell pickte (mir war langweilig). Ich habe es nicht getestet, aber es sollte funktionieren. Alles, was Sie wirklich damit tun müssen, ist die Implementierung dafür schreiben. Sie benötigen eine Funktion, um das Menü für den Benutzer und Sie zu drucken, Sie müssen etwas Code schreiben, um Ihre Artikel den Slots hinzuzufügen. Der Rest sollte meistens für dich da sein. Ich kann es weiter verbessern, aber fühle mich frei, einige oder alle der folgenden Dinge zu benutzen.

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) { } 

     } 
    } 
} 
Verwandte Themen