using System;
using System.Collections.Generic;

using System.Text;

namespace Project1
{
    class Class1
    {
        public static void Main(String[] args)
        {
            int b = 0;
            string stop = "";
            SharePortfolio share = new SharePortfolio();
            string shareName = "";
            int sharePrice = 0;
            string doYouWant2AddShare = "";
            string whatShareDoYouWant2see = "";
            string doYouWant2changeSharePrice = "";
            string doYouWant2RaiseShare = "";
            int toRaiseShare = 0;
            int toSubtractShare = 0;
            bool swap = false;
            bool exit = false;
            /////////////feeding share info///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            while (true)
            {
                swap = false;
                while (swap!=true)
                {
                    Console.WriteLine("Build an investment portfolio by punching out the share names and the share prices:");

                    Console.WriteLine("d(share No." + b + ")What would be the share name?");
                    shareName = Console.ReadLine();
                    Console.WriteLine("(share No." + b + ")What would be the share price");
                    sharePrice = int.Parse(Console.ReadLine());
                    share.add(shareName, sharePrice); // why when i send all this information to the class. it isnt saved in the array

                    Console.WriteLine("jhjhIf you want to stop.type-Stop");
                    stop = Console.ReadLine();
                    if (stop == "Stop")
                    {
                        swap = true;
                    }

                }

                /////////////End of feeding share info///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                /////////////Withdrawing a share/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                Console.WriteLine("On what share do you want to have information?");
                whatShareDoYouWant2see = Console.ReadLine();
                if (share[whatShareDoYouWant2see] == 0)
                {
                    Console.WriteLine("The share type doesnt exist. Would you like to add it?(YES/NO)");
                    doYouWant2AddShare = Console.ReadLine();
                    if (doYouWant2AddShare == "Yes")
                    {
                        Console.WriteLine("(share No." + b + ")What would be the share name?");
                        shareName = Console.ReadLine();
                        Console.WriteLine("(share No." + b + ")What would be the share price");
                        sharePrice = int.Parse(Console.ReadLine());
                        share.add(shareName, sharePrice);
                    }
                }
                else
                {
                    Console.WriteLine("The shares name: " + whatShareDoYouWant2see + "The Shares price: " + share[whatShareDoYouWant2see]);
                }
                /////////////End of Withdrawing a share///////////////////////////////////////////////////////////////////////////////////////////////////////
                //////////////////////
                /////////////Share price change//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                Console.WriteLine("Do you want to change its price?(YES/NO)");
                doYouWant2changeSharePrice = Console.ReadLine();
                if (doYouWant2changeSharePrice == "Yes")
                {
                    Console.WriteLine("Do you want to raise the share?");
                    doYouWant2RaiseShare = Console.ReadLine();
                    if (doYouWant2RaiseShare == "Yes")
                    {
                        Console.WriteLine("By how much would you like to raise it?");
                        toRaiseShare = int.Parse(Console.ReadLine());
                        share[whatShareDoYouWant2see] += toRaiseShare;


                    }
                    else
                    {
                        Console.WriteLine("By how much would you like to subtract it?");
                        toSubtractShare = int.Parse(Console.ReadLine());
                        share[whatShareDoYouWant2see] -= toSubtractShare;

                    }
                }

                /////////////End of Share price change//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                
                Console.WriteLine("If you want to stop and go to the share adding menu.type-Stop.. or if you want to exit completely type: exit;");
                stop = Console.ReadLine();
                if (stop == "Stop")
                {
                    swap = false;
                }
                else if (stop == "exit")
                {
                    exit = true;
                    break;
                }
                b++;
            }
              }
            
        }
    
    class SharePortfolio
    {
        public static int size = 4;
        string[] shareName = new string[size];
        int[] sharePrice = new int[size];
        int index;
        public const int Delta = 2;
        public const int Increase_By = 4;
        public void add(string name, int price)
        {
            this.shareName[index] = name;
            this.sharePrice[index] = price;
            index++;

            if ((size - index) == Delta)
            {
                shareName = new string[index + Increase_By];
                sharePrice = new int[index + Increase_By];
            }

        }

        public int this[string name]
        {
            get
            {
                for (int i = 0; i < index; i++)
                {
                    if (name == shareName[i])
                    {
                        return sharePrice[i];
                    }
                }
                return 0;
            }
            set
            {
                for (int i = 0; i < index; i++)
                {
                    if (name == shareName[i])
                    {
                        sharePrice[i] = value;
                    }
                }
                add(name, value);
            }
        }

    }
}

what this code should do, is take input from the user (the share's name and price), then store it in the array in the class and then retreive the info from the array.

Everything works, but the retrieving of multiple numbers from the array doesnt work well.

could you help me and tell me , why cant i store and retreive info from teh array in the class?

Recommended Answers

All 3 Replies

In your Add method, what do you think this code does, because it doesn't:

if ((size - index) == Delta) {
    shareName = new string[index + Increase_By];
    sharePrice = new int[index + Increase_By];
}

If this code ever executes you lose all the information you previously had.

ooh really!! i thought i was using dynamic arrays.

so should i remove that code completely?

edit.i tried it wont save even when i remove that code..

damn. why does it refuse to save"!!?!?

it still doesnt work, even after i remove the code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.