Domanda

C#. I'm having a problem with a school related project, I'm supposed to write a program that acts like a e-store/cart.

I get a list of items for sale and their prices; trout: $10, salmon: $20, beef: $50, tomato: $5 etc... but the user has to be able to choose what he wants and how many until he finishes his order by pressing "S". Then the program prints the following items the user bought along with quantity, individual price and total price of everything. how would i go around doing this since when the user presses "S" they don't run the other variables for the store items. i was thinking to set this kind of code:

string unknownItem1 = Console.ReadLine();
Console.WriteLine("how many?");
string unknownItemQuantity1 = Console.ReadLine();

string unknownItem2 = Console.ReadLine();
Console.WriteLine("how many?");
string unknownItemQuantity2 = Console.ReadLine();

string unknownItem3 = Console.ReadLine();
Console.WriteLine("how many?");
string unknownItemQuantity3 = Console.ReadLine();

string unknownItem4 = Console.ReadLine();
Console.WriteLine("how many?");
string unknownItemQuantity4 = Console.ReadLine();

if () {//if user input equals "S" finish order.(idk how to do this since there is no  variable for it)

}

if anyone has any idea how to do this please help, all answers are really helpful to me. Thanks

-Realitiez

È stato utile?

Soluzione 3

You can solve this by adding items in an array. Use array index for calculating price of an item. Out put of your program will be like this:

Enter item number
1. Eggs
2. Bread

///////////////////////////////////////////

string[,] items = new string[,] {{"Eggs","10"}, {"Bread","15"}};
string input;
int price=0;
int quantity;
int index;

do {
input = Console.ReadLine();
Console.WriteLine("Enter quantity: ");
index = Convert.ToInt32(input)+1;
quantity = Convert.ToInt32(Console.ReadLine());
price += quantity * Convert.ToInt32(items[index,1]); // accessing price of item

}while(input != "S");

Console.WriteLine("Your total: "+price);

Altri suggerimenti

Try doing a do while loop which loop condition checks the readLine() value and if it detects an S than it stops prompting the user. Hope it helps!

Looking for something like this?

while ((line = Console.ReadLine()) != "S")
{
  //Continues while input doesn't equal to "S"
}

if(unknownqty.ToUpper() == "S") return;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top