Answer to Question #210140 in C# for Ali

Question #210140

Create a console app for Food ordering mechanism:

Features must be implemented:

1.Customer sign up/sign in

2.Show food menu to customer

3.Customer can select more than 1 food item at a time

4.Store the order in a collection like array

5.Calculate bill and display at the end.


1
Expert's answer
2021-06-24T12:06:53-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;




namespace Q210140
{


    class Food
    {
        private string name;
        private double price;


        public Food() { }
        public Food(string name, double price)
        {
            this.name = name;
            this.price = price;
        }


        public string Name
        {
            get { return name; }
            set { name = value; }
        }




        public double Price
        {
            get { return price; }
            set { price = value; }
        }
    }




    class User {
        private string userName;
        private string password;


        public User() { }
        public User(string userName, string password) {
            this.userName = userName;
            this.password = password;
        }


        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        


        public string Password
        {
            get { return password; }
            set { password = value; }
        }




        public bool login(string userName, string password)
        {
            return (this.userName == userName) && (this.password == password);
        }
    }
    class Program
    {


        static int login(User []users,int totalUsers,string userName, string password ) {
            for (int i = 0; i < totalUsers; i++) {
                if (users[i].login(userName, password)) {
                    return i;
                }
            }
            return -1;
        }
        static void Main(string[] args)
        {
            Food[] foods = { new Food("1. Butter, 1 lb – Meijer – $2.59", 2.59),
                             new Food("2. Cheese, 8 oz block – Walmart – $1.74",1.74),
                           new Food("3. Milk, 1 gallon – Walmart – $1.78",1.78),
                           new Food("4. Eggs, 1 dozen – Walmart – $0.78",0.78),
                           new Food("5. Apples, 3 lbs – Aldi – $2.99",2.99)};


            User []users = new User[100];
            int totalUsers = 0;


            Food[] selectedFoods = new Food[5];
            int totalSelectedFoods=0;


            int ch = -1;
            while (ch != 3)
            {
                Console.WriteLine("1. Sign up");
                Console.WriteLine("2. Sign in");
                Console.WriteLine("3. Exit");
                Console.Write("Your choice: ");
                int.TryParse(Console.ReadLine(), out ch);


                if (ch == 1)
                {
                    Console.Write("Enter user name: ");
                    string userName = Console.ReadLine();
                    Console.Write("Enter user password: ");
                    string password = Console.ReadLine();
                    users[totalUsers] = new User(userName, password);
                    totalUsers++;
                }
                else if (ch == 2)
                {
                    Console.Write("Enter user name: ");
                    string userName = Console.ReadLine();
                    Console.Write("Enter user password: ");
                    string password = Console.ReadLine();
                    int index=login(users,totalUsers,userName, password);
                    if (index != -1)
                    {
                        string answer = "y";
                        while (answer == "y")
                        {


                            for (int i = 0; i < foods.Length; i++)
                            {
                                Console.WriteLine(foods[i].Name);
                            }
                            int selectedFood = -1;
                            while (selectedFood < 1 || selectedFood > 5)
                            {
                                Console.Write("Your choice: ");
                                if (!int.TryParse(Console.ReadLine(), out selectedFood))
                                {
                                    selectedFood = -1;
                                }
                            }
                            selectedFoods[totalSelectedFoods] = foods[selectedFood];
                            totalSelectedFoods++;
                            Console.WriteLine("\nThe food has been added.\n");




                            Console.Write("Do you want to add the other food? y/n: ");
                            answer = Console.ReadLine();
                        }
                        //Calculate bill and display at the end.
                        Console.WriteLine("\nSelected foods:\n");
                        double totalPrice=0;
                        for (int i = 0; i < totalSelectedFoods; i++)
                        {
                            Console.WriteLine(selectedFoods[i].Name);
                            totalPrice+=selectedFoods[i].Price;
                        }


                        Console.WriteLine("Total price: {0}\n", totalPrice);
                    }
                    else {
                        Console.WriteLine("\nWrong username and password.\n");
                    }
                }
                else if (ch == 3)
                {
                    //exit
                }
                else
                {
                    Console.WriteLine("\nWrong menu item.\n\n");
                }
            }


           
        }
    }
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS