【问题标题】:C# Listboxes and values for a vending machine自动售货机的 C# 列表框和值
【发布时间】:2018-11-25 13:09:38
【问题描述】:

这篇文章或多或少地链接到我之前的帖子:C# adding value/index to items in an array and keeping track in Console how often they have been clicked

我不知道如何表达我的标题,如有任何混淆,我深表歉意。我的任务是编写一台基本的自动售货机。

所以我现在有两个列表框。 ListBox 1 附加了一个枚举。在这个枚举中有 4 个项目,每个项目都有一个价格。 Item1 的价格为 1.7 欧元(枚举中的索引为 170),Item2 的价格为 2 欧元(枚举中的索引为 200)等等。我已经对此进行了编码。

选择其中一个项目时,出现ListBox2。这包含您可以单击以支付所选项目的硬币。硬币有 0.10、0.20、0.30、0.50、1.00 和 2.00。我已经对这些列表框及其枚举/数组进行了编码。

我应该做(并且努力做)是创建一个数组(至少我是这么认为的),每当我点击这些硬币中的任何一个时,它都会计数。如果我选择了 Item1,它的价格是 1.70 欧元(所以 index 是 170),那么如果我点击 0.50 4 次,它会出现在 2.00(index of 200),这意味着 Item1 的值已经满足。将出现一个标签,提及已达到该值,并且还会显示已选择了多少硬币(以及每种硬币的数量)。

此外,如果硬币的价值大于所选物品的价值,则会在附加行中显示您将获得多少每枚硬币。在这种情况下,应该说 1 of 0.10 和 1 of 0.20。

我终其一生都想不出如何解决这个问题。我是编程的初学者,这是今晚的作业。

我希望有人能帮我解决这个问题。

【问题讨论】:

  • 忘了说,这是一个 .wpf 文件。

标签: c# arrays enums listbox


【解决方案1】:

正如我在上一篇文章中帮助过你一样,我想我也会在这里帮助你。一旦你理解了逻辑,这很简单。我已经编写了可以满足要求的应用程序。它可能需要一些整理,但它是完全可操作的。

这是你的主要内容:

 public partial class MainWindow : Window
    {
        List<KeyValuePair<string, int>> tracking = new List<KeyValuePair<string, int>>();
        List<KeyValuePair<string, int>> changeGiven = new List<KeyValuePair<string, int>>();
        List<decimal> coinItems = new List<decimal>();

        decimal valueSelected;
        decimal coinSoFar = 0;
        Hashtable coins = new Hashtable();
        Hashtable prices = new Hashtable();

        public MainWindow()
        {
            InitializeComponent();

            coins.Add("010", 0.10m);
            coins.Add("020", 0.20m);
            coins.Add("030", 0.30m);
            coins.Add("040", 0.40m);
            coins.Add("050", 0.50m);

            coinItems.Add(Convert.ToDecimal(coins["010"]));
            coinItems.Add(Convert.ToDecimal(coins["020"]));
            coinItems.Add(Convert.ToDecimal(coins["030"]));
            coinItems.Add(Convert.ToDecimal(coins["040"]));
            coinItems.Add(Convert.ToDecimal(coins["050"]));

            lbTodoList.ItemsSource = coinItems;

            prices.Add("100", 1.10m);
            prices.Add("120", 1.20m);
            prices.Add("130", 1.30m);
            prices.Add("140", 1.40m);
            prices.Add("150", 1.50m);
            prices.Add("160", 1.60m);
            prices.Add("170", 1.70m);

            List<decimal> priceItems = new List<decimal>();
            priceItems.Add(Convert.ToDecimal(prices["100"]));
            priceItems.Add(Convert.ToDecimal(prices["120"]));
            priceItems.Add(Convert.ToDecimal(prices["130"]));
            priceItems.Add(Convert.ToDecimal(prices["140"]));
            priceItems.Add(Convert.ToDecimal(prices["150"]));
            priceItems.Add(Convert.ToDecimal(prices["160"]));
            priceItems.Add(Convert.ToDecimal(prices["170"]));

            lbPrice.ItemsSource = priceItems;
        }

        private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = this.lbTodoList.SelectedItem?.ToString();
            if (item != null)
            {
                if (!tracking.Select(x => x.Key == item).Any())
                {
                    tracking.Add(new KeyValuePair<string, int>(item, 1));
                    Console.WriteLine(item + " has been selected once");
                }
                else
                {
                    var currentItem = tracking.SingleOrDefault(x => x.Key == item);
                    var value = currentItem.Value;
                    tracking.Remove(currentItem);
                    value++;
                    tracking.Add(new KeyValuePair<string, int>(item, value));
                }

                var getIndex = item.Replace(".", "");
                coinSoFar += Convert.ToDecimal(coins[getIndex]);

                if (coinSoFar >= valueSelected)
                {
                    StringBuilder coinsInserted = new StringBuilder();
                    foreach (var coinTracked in tracking)
                    {
                        coinsInserted.Append(coinTracked.Key + " x " + coinTracked.Value + "; ");
                    }

                    var difference = coinSoFar - valueSelected;

                    int coinDifferenceIndex = 0;
                    StringBuilder coinsReturned = new StringBuilder();
                    while (difference != 0)
                    {
                        var currentCoin = coinItems[coinDifferenceIndex];
                        difference -= currentCoin;

                        var currentItem = changeGiven.SingleOrDefault(x => x.Key == currentCoin.ToString());
                        if (currentItem.Key != null)
                        {
                            var value = currentItem.Value;
                            changeGiven.Remove(currentItem);
                            value++;
                            changeGiven.Add(new KeyValuePair<string, int>(currentItem.Key, value));
                        }
                        else
                        {
                            changeGiven.Add(new KeyValuePair<string, int>(currentCoin.ToString(), 1));
                        }

                        if (difference > currentCoin)
                        {
                            coinDifferenceIndex++;
                        }
                        else
                        {
                            coinDifferenceIndex = 0;
                        }
                    }

                    foreach (var coin in changeGiven)
                    {
                        coinsReturned.Append(coin.Key + " x " + coin.Value + "; ");
                    }

                    if(changeGiven.Any())
                    {
                        this.lblEnoughMoneyReached.Content = "Cost value has been met. Coins inserted: " + coinsInserted + ".Change given: " + coinsReturned;
                    }
                    else
                    {
                        this.lblEnoughMoneyReached.Content = "Cost value has been met. Coins inserted: " + coinsInserted;
                    }  
                }
            }
            this.lbTodoList.SelectedItem = null;
        }

        private void lbPrice_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            valueSelected = Convert.ToDecimal(this.lbPrice.SelectedItem?.ToString());
        }
    }

您的 XAML 应该如下所示:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="300"/>
            <RowDefinition Height="300"/>
            <RowDefinition Height="300"/>
            <RowDefinition Height="300"/>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" Grid.Column="0" Name="lbTodoList" HorizontalAlignment="Left" Height="154" Margin="145,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbTodoList_SelectionChanged">
        </ListBox>

        <ListBox Grid.Row="0" Grid.Column="0" Name="lbPrice" HorizontalAlignment="Left" Height="154" Margin="525,56,0,0" VerticalAlignment="Top" Width="277" SelectionChanged="lbPrice_SelectionChanged">
        </ListBox>

        <Label Grid.Row="1" Grid.Column="0" Name="lblEnoughMoneyReached" HorizontalAlignment="Left" Height="154" VerticalAlignment="Top"></Label>

    </Grid>
</Window>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多