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

时间:2018-11-25 13:09:38

标签: c# arrays enums listbox

此帖子与我以前的帖子或多或少相关:C# adding value/index to items in an array and keeping track in Console how often they have been clicked

我不确定如何用我的标题写词,对于任何混乱,我们深表歉意。我的任务是为基本的自动售货机编码。

所以我现在有两个列表框。列表框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欧元(所以索引是170),那么如果我单击0.50 4次,它将显示为2.00(索引为200),这意味着已经满足了Item1的值。标签上似乎会提到已达到该值,并且还会显示已选择了多少个硬币(以及每个硬币有多少个)。

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

我一生都无法解决该问题。我是编程的总初学者,这是由于今晚的作业。

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

1 个答案:

答案 0 :(得分:0)

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

这是您的主要内容:

 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>