В настоящее время я пытаюсь создать небольшую программу для торговых автоматов, в которой пользователь выбирает товар, спрашивает, сколько ему нужно этого товара, затем находит общую цену продавца и дает им правильное количество изменений. Я, кажется, не могу получить этот код для вывода общей цены вендора. Может кто-нибудь сказать мне, где я иду не так?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ugh
{
class Program
{
struct VendingItem
{
public string name;
public double price;
public string code;
}
static void Main(string[] args)
{
string selection = "";
double price = 0;
double finalCost = 0;
int numOfItem = 0;
bool Yorn = true;
Dictionary<string, VendingItem> snacks = new Dictionary<string, VendingItem>();
VendingItem v;
//snacks
v.name = "Mars Bar";
v.price = 1.30;
v.code = "A1";
snacks.Add(v.code, v);
v.name = "Milky Way";
v.price = 1.30;
v.code = "A2";
snacks.Add(v.code, v);
v.name = "Double Decker";
v.price = 1.30;
v.code = "A3";
snacks.Add(v.code, v);
v.name = "Kit Kat";
v.price = 1.30;
v.code = "A4";
snacks.Add(v.code, v);
v.name = "Dairy Milk";
v.price = 1.30;
v.code = "A5";
snacks.Add(v.code, v);
v.name = "Pringles Original";
v.price = 1.70;
v.code = "A6";
snacks.Add(v.code, v);
v.name = "Pringles Salt and Vinegar";
v.price = 1.70;
v.code = "A7";
snacks.Add(v.code, v);
v.name = "Pringles Cheese and Onion";
v.price = 1.70;
v.code = "A8";
snacks.Add(v.code, v);
v.name = "Pringles Texas BBQ";
v.price = 1.70;
v.code = "A9";
snacks.Add(v.code, v);
v.name = "Pringles Prawn Cocktail";
v.price = 1.70;
v.code = "A10";
snacks.Add(v.code, v);
//drinks
v.name = "Water";
v.price = 1.00;
v.code = "A11";
snacks.Add(v.code, v);
v.name = "Red Bull";
v.price = 1.35;
v.code = "A12";
snacks.Add(v.code, v);
v.name = "Monster";
v.price = 1.35;
v.code = "A13";
snacks.Add(v.code, v);
v.name = "Fanta Orange";
v.price = 1.20;
v.code = "A14";
snacks.Add(v.code, v);
v.name = "Fanta Fruit Twist";
v.price = 1.20;
v.code = "A15";
snacks.Add(v.code, v);
//outputting the name, code and price of each item in the list
foreach (KeyValuePair<string, VendingItem> item in snacks)
{
Console.WriteLine("Snack: {0} -\nCode: {1}\nPrice: £{2}\n\n", item.Value.name, item.Value.code, item.Value.price);
}
//calling the vendSelection method using a new string selection and the dictionary snacks as the parameters
vendSelection(selection, snacks);
//find out how many they would like
Console.WriteLine("How many of this item would you like?");
numOfItem = int.Parse(Console.ReadLine());
//get the price of item at location of user selection
foreach (KeyValuePair<string, VendingItem> item in snacks)
{
price = item.Value.price.CompareTo(item.Value.code.CompareTo(selection));
}
totalPrice(price, numOfItem);
Console.WriteLine(price);
Console.ReadLine();
}
//method for taking user input and comparing it to the code of the snack/drink
static string vendSelection(string x, Dictionary<string, VendingItem> snacks)
{
Console.WriteLine("Please enter the code that corresponds with the item you would like: ");
x = Console.ReadLine();
bool YorN = true;
while (YorN == true)
{
if (snacks.ContainsKey(x))
{
YorN = false;
}
else
{
Console.WriteLine("That is not an item in the list. Please enter another selection: ");
x = Console.ReadLine();
}
}
return x;
}
//method for calculating price
static double totalPrice(double price, int number)
{
return price * number;
}
}
}
Я пытаюсь получить вывод любой суммыбудет, но вместо этого все, что я получу, это номер элемента, который я ввел, кто-нибудь может предложить способ решить эту проблему?