Home > Net >  User input looping and adding all inputs
User input looping and adding all inputs

Time:11-29

I needed to get the total of all the user's input products but I can't do it. I need to ask the user to choose the product and its quantity and then ask the user if she would like to add more products or pay it. The user will enter product again and its quantity. After it the program will ask again if add or pay. When the user choose pay it was supposed to output the total amount of products. Then lastly the program will ask the user if she/he would like to use the program again, if not, it'll exit.

int choice, quanti, decide, total, price;
string w = "WELCOME ";
Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
Console.WriteLine(w);
Console.WriteLine("");
System.Threading.Thread.Sleep(2000); //time delay

string p = "HERE'S OUR MERCHANDISES! ";
Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
Console.WriteLine(p);
System.Threading.Thread.Sleep(2000);//time delay

string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings", 
                        "[3]RED VELVET Summer Package"};

for (int g = 0; g < products.Length; g  )
{
    Console.WriteLine(products[g]);
}

for (int i = 0; i < products.Length; i  )
{
    Console.Write("Pick your product: ");
    choice = int.Parse(Console.ReadLine());
    switch (choice)
    {
        case 1:
            Console.WriteLine("BLACKPINK Lightstick 1500php");

            break;
        case 2:
            Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");

            break;
        case 3:
            Console.WriteLine("RED VELVET Summer Package 980php");

            break;
    }
    Console.WriteLine("Quantity of product: ");
    quanti = int.Parse(Console.ReadLine());
    System.Threading.Thread.Sleep(2000);//time delay
    Console.WriteLine("[1] Add more products \t [2] Pay: ");
    decide = int.Parse(Console.ReadLine());

    if (decide == 2)
    {
        decide  ;
        if (choice == 1)
        {
            price = 1500;
        }
        else if (choice == 2)
        {
            price = 920;
        }

        else if (choice == 3)
        {
            price = 980;
        }
        else
        {
            break;
        }

        total = choice * price * quanti;
        Console.Write(total);
    }
    else
    {
        Console.Write("Pick your product: ");
        choice = int.Parse(Console.ReadLine());
        switch (choice)
        {
            case 1:
                Console.WriteLine("BLACKPINK Lightstick 1500php");
                price = 1500;
                break;
            case 2:
                Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");
                price = 920;
                break;
            case 3:
                Console.WriteLine("RED VELVET Summer Package 980php");
                price = 700;
                break;

        }
        Console.WriteLine("Quantity of product: ");
        quanti = int.Parse(Console.ReadLine());
        System.Threading.Thread.Sleep(2000);//time delay
        Console.WriteLine("[1] Add more products \t [2] Pay: ");
        decide = int.Parse(Console.ReadLine());
        if (decide == 2)
        {
            if (choice == 1)
            {
                price = 1500;
            }
            else if (choice == 2)
            {
                price = 920;
            }
            else if (choice == 3)
            {
                price = 980;
            }
            else
            {
                break;
            }

            total = price * quanti;
            Console.Write(total);
        }
    }
}

CodePudding user response:

check if this is what you expect.

int choice, quanti, decide, total, price;
        while (true)
        {
            Console.Clear();
            string w = "WELCOME ";
            Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(w);
            Console.WriteLine("");
            System.Threading.Thread.Sleep(2000); //time delay

            string p = "HERE'S OUR MERCHANDISES! ";
            Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(p);
            System.Threading.Thread.Sleep(2000);//time delay

            string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings",
                    "[3]RED VELVET Summer Package"};

            for (int g = 0; g < products.Length; g  )
            {
                Console.WriteLine(products[g]);
            }

            Dictionary<int, int> ProductList = new Dictionary<int, int>();
            while (true)
            {
                {
                    Console.Write("Pick your product: ");
                    choice = int.Parse(Console.ReadLine());
                    switch (choice)
                    {
                        case 1:
                            Console.WriteLine("BLACKPINK Lightstick 1500php");
                            break;
                        case 2:
                            Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");

                            break;
                        case 3:
                            Console.WriteLine("RED VELVET Summer Package 980php");

                            break;
                    }
                    Console.WriteLine("Quantity of product: ");
                    quanti = int.Parse(Console.ReadLine());
                    if (!ProductList.ContainsKey(choice))
                        ProductList.Add(choice, quanti);
                    else
                        ProductList[choice]  = quanti;
                    System.Threading.Thread.Sleep(2000);//time delay
                    Console.WriteLine("[1] Add more products \t [2] Pay: ");
                    decide = int.Parse(Console.ReadLine());

                    if (decide == 2)
                        break;
                }
            }
            total = 0;
            foreach (int key in ProductList.Keys)
                switch (key)
                {
                    case 1:
                        total  = ProductList[key] * 1500;
                        break;
                    case 2:
                        total  = ProductList[key] * 920;
                        break;
                    case 3:
                        total  = ProductList[key] * 980;
                        break;
                    default:
                        total  = 0;
                        break;
                };
            Console.WriteLine("To Pay: "   total.ToString());

            Console.WriteLine("Another shopping? [1]Yes [2] No");
            choice = int.Parse(Console.ReadLine());
            if (choice == 2)
                break;
        }
    }

CodePudding user response:

struct product
    {
        public string Name;
        public float Price;
        public float Discount;
    }
    static void Main(string[] args)
    {


        int choice, quanti, decide;
        float total;
        Dictionary<int,product> products=new Dictionary<int, product>();
        products.Add(1,new product() { Name = "BLACKPINK Lightstick", Price = 1500, Discount = 0 });
        products.Add(2, new product() { Name = "DREAMCATCHER Seasons Greetings", Price = 920, Discount = 0 });
        products.Add(3, new product() { Name = "RED VELVET Summer Package", Price = 980, Discount = 0 });
        while (true)
        {
            Console.Clear();
            string w = "WELCOME ";
            Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(w);
            Console.WriteLine("");
            System.Threading.Thread.Sleep(2000); //time delay
            string p = "HERE'S OUR MERCHANDISES! ";
            Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(p);
            System.Threading.Thread.Sleep(2000);//time delay
            foreach (int key in products.Keys)
            {
                Console.WriteLine("[" key.ToString() "]" products[key].Name);
            }

            Dictionary<int, int> ProductList = new Dictionary<int, int>();
            while (true)
            {
                {
                    Console.Write("Pick your product: ");
                    choice = int.Parse(Console.ReadLine());
                    Console.WriteLine(products[choice].Name " " products[choice].Price.ToString() "php");
                    Console.WriteLine("Quantity of product: ");
                    quanti = int.Parse(Console.ReadLine());
                    if (!ProductList.ContainsKey(choice))
                        ProductList.Add(choice, quanti);
                    else
                        ProductList[choice]  = quanti;
                    System.Threading.Thread.Sleep(2000);//time delay
                    Console.WriteLine("[1] Add more products \t [2] Pay: ");
                    decide = int.Parse(Console.ReadLine());

                    if (decide == 2)
                        break;
                }
            }
            total = 0;
            Console.WriteLine("------------------------------------");
            foreach (int key in ProductList.Keys)
            {
                total  = products[key].Price*ProductList[key];
                Console.WriteLine(products[key].Name.PadRight(40,' ') ProductList[key].ToString().PadLeft(3,' ')  " * " products[key].Price.ToString().PadLeft(10,' '));
                Console.WriteLine((products[key].Price * ProductList[key]).ToString().PadLeft(56, ' '));
            }
            Console.WriteLine(("To Pay: "   total.ToString()).PadLeft(56,' '));
            Console.WriteLine("Cash: ");
            var cash = float.Parse(Console.ReadLine());
            Console.WriteLine(("Change: "   (cash - total).ToString()).PadLeft(56, ' '));

            Console.WriteLine("Another shopping? [1]Yes [2] No");
            choice = int.Parse(Console.ReadLine());
            if (choice == 2)
                break;
        }
    }
  • Related