2016-12-27 70 views
0

我一直在研究一個包含項目列表的小控制檯應用程序,並且當按下一個數字時,相關項目應計算爲總數(完成後)。在c#控制檯應用程序中按下按鈕時添加項目

問題在於如何將這些項目添加到列表視圖並在控制檯應用程序中顯示它們。這是我到目前爲止有

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace SuperMarcado 
{ 
    class MainClass 
    { 
     public static void Main(string[] args) 
     { 

      ShoppingCart myCart = new ShoppingCart(); 

      Product[] shopProducts = new Product[] 
      { 
       new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4), 
       new Product("Bread", 2, "Grain Bread", "27/11/2016", 8), 
       new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1), 
       new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5), 
       new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6) 
      }; 

      Shop shop = new Shop(shopProducts); 
      shop.DisplayProducts(); 
      Console.WriteLine("------------------------------------------------------------"); 

      myCart.printY = Console.CursorTop; 
      myCart.Display(); 
      ConsoleKeyInfo input; 


      while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape) 
      { 
       if (!Char.IsDigit(input.KeyChar)) 
        return; 
       int index = Convert.ToInt16(input.KeyChar.ToString()) - 1; 

       if (index < 1 || index > shop.products.Length) 
        return; 

       myCart.AddProduct(shop.products[index]); 

       shop.DecreaseAmount(shop.products[index]); 

       shop.DisplayProducts(); 

       myCart.Display(); 



       int userInput = 0; 
       do 
       { 

        userInput = ShoppingCart(); 

       } while (userInput != 5); 
      } 

     } 
     static public int ShoppingCart() 
     { 
      Console.WriteLine("Your cart"); 
      Console.WriteLine(); 
      var result = Console.ReadLine(); 
      return Convert.ToInt32(result); 
     } 


    } 
} 
+1

什麼你的問題到底是什麼? –

+0

如何添加商品並製作購物車,並在數字按下時隨時顯示。 –

+0

這就是你想要它做的。你沒有說明什麼是做錯了什麼,或者它沒有做什麼,你堅持。 –

回答

0

在第一次拿到,你的問題是第二個循環,刪除它,它應該工作打算

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace SuperMarcado 
{ 
class MainClass 
{ 
    public static void Main(string[] args) 
    { 

     ShoppingCart myCart = new ShoppingCart(); 

     Product[] shopProducts = new Product[] 
     { 
      new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4), 
      new Product("Bread", 2, "Grain Bread", "27/11/2016", 8), 
      new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1), 
      new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5), 
      new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6) 
     }; 

     Shop shop = new Shop(shopProducts); 
     shop.DisplayProducts(); 
     Console.WriteLine("------------------------------------------------------------"); 

     myCart.printY = Console.CursorTop; 
     myCart.Display(); 
     ConsoleKeyInfo input; 


     while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape) 
     { 
      if (!Char.IsDigit(input.KeyChar)) 
       return; 
      int index = Convert.ToInt16(input.KeyChar.ToString()) - 1; 

      if (index < 1 || index > shop.products.Length) 
       return; 

      myCart.AddProduct(shop.products[index]); 

      shop.DecreaseAmount(shop.products[index]); 

      shop.DisplayProducts(); 

      myCart.Display(); 



      //int userInput = 0; 
      //do 
      //{ 

      // userInput = ShoppingCart(); 

      //} while (userInput != 5); 
     } 

    } 

    //static public int ShoppingCart() 
    //{ 
    // Console.WriteLine("Your cart"); 
    // Console.WriteLine(); 
    // var result = Console.ReadLine(); 
    // return Convert.ToInt32(result); 
    //} 
} 
} 

我的購物類

using System; 
using System.Collections.Generic; 

namespace SuperMarcado 
{ 
internal class ShoppingCart 
{ 
    internal int printY; 
    internal List<Product> productList; 

    public ShoppingCart() 
    { 
     productList = new List<Product>(); 
    } 

    internal void Display() 
    { 
     if (productList.Count == 0) 
      Console.WriteLine("------------------ EMPTY CART------------------ "); 

     foreach (var prod in productList) 
      Console.WriteLine(prod.Name + " value: " + prod.Value); 
    } 

    internal void AddProduct(Product product) 
    { 
     productList.Add(product); 
    } 
} 
} 
+0

但不顯示所選項目... –

+0

然後您需要在您的類_ShoppingCart_中實現您的方法_Display_,方法爲迭代由_AddProduct_添加的產品 –

相關問題