I'm writing a program that displays a fast food menu and it allows the user to select an item. Then, the user enters the quantity of that item, and can continue selecting items with specific quantities until done. What I'm having trouble with is finding out how to calculate a running total. I am new to c# so I only know the basics. What is the best way to keep track of the running total while using multiple methods? In addition, I'm open to any criticism in my code. Thank you in advance.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
bool ordering = true;
string userInput;
double itemPrice;
double itemQuantity;
double subTol;
string response;
void pricing()
{
Console.Write("Enter option 1, 2, 3: ");
userInput = Console.ReadLine();
switch (userInput)
{
case "1":
itemPrice = 3.00;
Console.WriteLine("You have picked a burger.");
break;
case "2":
itemPrice = 1.50;
Console.WriteLine("You have picked fries.");
break;
case "3":
itemPrice = 1.00;
Console.WriteLine("You have picked a soda.");
break;
default:
Console.WriteLine("That is not on our menu.");
pricing();
break;
}
}
void quantity()
{
Console.Write("Enter quantity: ");
itemQuantity = Convert.ToDouble(Console.ReadLine());
}
void subTotal()
{
subTol = itemQuantity * itemPrice;
Console.WriteLine();
Console.WriteLine("Your Subtotal: " subTol);
}
while (ordering)
{
Console.WriteLine("What would you like from our menu?");
Console.WriteLine("\n1. Burger ($3.00) \n2. Fries ($1.50) \n3. Soda ($1.00)");
Console.WriteLine();
pricing();
quantity();
subTotal();
Console.Write("Would you like anything else? Y/N: ");
response = Console.ReadLine();
response = response.ToUpper();
if (response == "Y")
{
ordering = true;
}
else
{
ordering = false;
Console.WriteLine("Enjoy your meal!");
}
}
}
}
}
CodePudding user response:
First of all, for the
If(response==“Y”)
Part you can actually just say
ordering = response == “Y”;
Also, this would only accept uppercase y. String.EqualsIgnoreCase() or something similar would probably do a better job
ordering = response.equalsIgnoreCase(“Y”);
You could keep a list of how many of each item the person has ordered, like
Int burgers = 0;
and so on.
Then for the total, you could get 3*burgers 1.5*fries
and so on. However, if you want custom orders, using a struct might be better. For example, a struct could contain a type(burger or fries) and a price, depending on how many additions they have. You could also just add the price of each item to a total every time you order. This seems like a fun project, and I hope I could help!
CodePudding user response:
For keeping track of total, the best approach would be to have a list of ordered items with their quantities, where you go and put each order from the user.
When you want to calculate the subtotal or total, you just go and compute from the list.
Here are some insights on how you can accomplish that, with some changes to the code you shared. Check the comments for details and reasoning.
namespace ConsoleApp1{
// creating record classes to store structured data
record MenuItem(string Product, decimal Price);
record Order(MenuItem Item, int Quantity);
class Program {
static void Main(string[] args) {
// better to keep your menu options & prices in a central place,
// instead of hardcoding them everywhere
List<MenuItem> menu = new() {
new ("Burger", 3.00M),
new ("Fries", 1.50M),
new ("Soda", 1.00M),
};
// this is a list to store the ordered items so far
List<Order> orders = new();
// moved most of the procedures to separate functions
do {
PrintMenu(menu);
var item = InputItem(menu);
var quantity = InputQuantity();
orders.Add(new Order(item, quantity));
PrintSubTotal(orders);
} while (CheckIfContinue());
PrintTotal(orders);
ByeBye();
}
static void PrintMenu(List<MenuItem> menu) {
// print each menu entry in a loop
Console.WriteLine("What would you like from our menu?");
for (int i = 0; i < menu.Count; i ) {
// adding 1 to the index for printing, because in c# indexes start at 0
Console.WriteLine($"{i 1}: {menu[i].Product} (${menu[i].Price})");
}
Console.WriteLine();
}
static MenuItem InputItem(List<MenuItem> menu) {
// enter a loop, will be broken only if the input is correct
while (true) {
Console.Write($"Enter option: 1 to {menu.Count}: ");
if (int.TryParse(Console.ReadLine(), out int i) && i > 0 && i <= menu.Count) {
// must subtract 1 from the index, because indexes start from 0 in c#
Console.WriteLine($"You have picked {i}: {menu[i - 1].Product}.\n");
// if the input is a valid int
return menu[i - 1];
}
Console.WriteLine("That is not in our menu.\n");
}
}
static int InputQuantity() {
// same thing here, will repeat the loop unless the input is valid
while (true) {
Console.Write("Enter quantity: ");
if (int.TryParse(Console.ReadLine(), out int i) && i > 0) {
return i;
}
Console.WriteLine("Invalid quantity.\n");
}
}
static void PrintSubTotal(List<Order> orders) {
// using LINQ to iterate the list of orders sum up the prices x quantities
Console.WriteLine($"Your Subtotal: ${orders.Sum(o => o.Item.Price * o.Quantity)}\n");
}
static bool CheckIfContinue() {
Console.Write("Would you like anything else? Y/N: ");
// will return True if the input is y
return Console.ReadLine()?.Trim().ToUpper() == "Y";
}
static void PrintTotal(List<Order> orders) {
Console.WriteLine($"Your Order:");
foreach (var order in orders) {
// subtotal for each order item
Console.WriteLine($"{order.Item.Product, -10} x{order.Quantity, -4}: ${order.Item.Price * order.Quantity}");
}
// total for the whole order
Console.WriteLine($"---------- Total: ${orders.Sum(o => o.Item.Price * o.Quantity)}\n");
}
static void ByeBye() {
Console.WriteLine("Enjoy your meal!");
}
}
}