Tuesday, October 23, 2012

Loops arrays, while

here is the code for the day. I may return to it to add comments later">


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shopping
{
    class Program
    {
        //we need to create two arrays
        //one for the items
        //one for the prices
        //we will prompt the user how many items
        //they want to enter
        //Then we will enter items and prices
        //into the two arrays
        //we will sum the prices and add tax
        //we will display a summary information
        //then we will ask if they want to enter 
        //another list. If they say yes we
        //will do it again, if no we will exit
        int number=0;

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Display();
        }

        private void Display()
        {
            //loop the program as long as the user says yes
            string choice = "yes";
            while (choice == "yes" || choice == "y")
            {
                //this changes the console background to blue
                Console.BackgroundColor = ConsoleColor.DarkBlue;
                Console.Clear();
                
                //call methods
                GetNumberOfItems();
                CreateShoppingArray();
                CreatePricesArray();
                FillArrays();
                Console.WriteLine("do you want to contine with another list--yes or no");
                choice = Console.ReadLine();
                choice = choice.ToLower();
            }
            //Console.ReadKey();
        }

        //get the number of items
        private void GetNumberOfItems()
        {
            Console.WriteLine("How many items do you want to enter?");
            number = int.Parse(Console.ReadLine());
         
        }
        //create the two arrays
        private string[] CreateShoppingArray()
        {
            string[] items = new string[number];
            return items;
        }

        private double[] CreatePricesArray()
        {
            double[] prices = new double[number];
            return prices;
        }

        //this prompts the user and fills the arrays
        private void FillArrays()
        {
            string[] shoppingItems = CreateShoppingArray();
            double[] itemPrices = CreatePricesArray();

            for (int i = 0; i < shoppingItems.Length; i++)
            {
                Console.WriteLine("Enter the item");
                shoppingItems[i] = Console.ReadLine();
                Console.WriteLine("Enter the item price");
                itemPrices[i] = double.Parse(Console.ReadLine());
              
            }//end for
            CalculateResults(shoppingItems, itemPrices);
        }//end fill array

        private void CalculateResults(string[] shoppingList, double[] itemCost)
        {
            Console.WriteLine("*****************************\n");
            double total = 0;
            for (int i = 0; i < itemCost.Length; i++)
            {
                //same as total=total + itemCost[];
                total += itemCost[i];
            }

            Console.WriteLine("Your Items");
            for(int i=0;i< shoppingList.Length;i++)
            {
                Console.WriteLine(shoppingList[i] + "\t\t" + itemCost[i].ToString("c"));
            }

            Console.WriteLine("Total {0:C}", total);

        }
    }
}


Here is the earlier code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WhileLoops
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            //uncomment the method you want to run
            //comment the ones you don't want to run

            p.ForLoopReview();
            p.WhileLoopExample();
            p.DoLoopExample();
            Console.ReadKey();
        }


        private void ForLoopReview()
        {
            string[] languages=new string[4];
            languages[0] = "C++";
            languages[1] = "C#";
            languages[2] = "Java";
            languages[3] = "php";

            for (int i = 0; i < languages.Length; i++)
            {
                Console.WriteLine(languages[i]);
            }//end for
        }//end for loop review

        private void WhileLoopExample()
        {
            int x = 6;
            Console.WriteLine("While Loop OutPut");
            while (x < 6)
            {
                Console.WriteLine(x);
                x++;
            }
        }//end while loop example

        private void DoLoopExample()
        {
            int x = 6;
            Console.WriteLine("Do loop output");
            do
            {
                Console.WriteLine(x);
                x++;
            } while (x < 6);
        }
    }//end class
}//end namespace

No comments:

Post a Comment