自动售货机的 C# 列表框和值

C# Listboxes and values for a vending machine

这个 post 或多或少与我之前的 post 相关:

我不确定如何表达我的标题,对于任何混淆,我们深表歉意。我的任务是编写一个基本的自动售货机。

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

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

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

此外,如果硬币的价值大于所选项目的价值,那么额外的行会显示每个硬币的数量。在这种情况下,它应该说 0.10 中的 1 和 0.20 中的 1。

我一辈子都想不出如何解决这个问题。我是编程的新手,这是今晚要交的作业。

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

正如我在之前的 post 中帮助过你一样,我想我也会在这里帮助你。一旦你理解了逻辑,这就很简单了。我已经编写了可以执行所需操作的应用程序。它可能需要一些整理,但它是完全可操作的。

这是你的主要内容:

 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>