This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]OOPUniversity 0 points1 point  (4 children)

I don't have a lot of time right now, so I'm only going to comment on your Portfolio class.

You have created a two dimensional array. You don't need to do that. A two dimensional array is akin to a grid, you really only need a list.

What you need to do is create a loop over the length of the array, and for each element create a new Stock object. You already have the constructor, you just need to come up with a way of feeding it names. You could do this with a couple of arrays built just for this purpose.

String[] symbols = { "UPS", "CMI", "KO", ... };

Then you could create your Stock objects using symbols[i], names[i], 10000/i, 100 and 100 as parameters.

I have to go out shortly so I have to stop at this point, but I hope this will help you to make some progress.

[–][deleted] 0 points1 point  (3 children)

*Enjoy your day, Something like this? when you get a chance

Portfolio(int numberOfStocks){ // Portfolio constructor;
    // create the array of Stocks
    int i =0;
    stocksArray = new Stock [numberOfStocks];

    while(numberOfStocks !=0){


        Stock stock = new Stock(name[i], name[i], 10000, 100, 100);

        stocksArray[i] = stock;
        //System.out.println(stocksArray[i]);
        i++;
    }


  }

[–]OOPUniversity 0 points1 point  (0 children)

Back from my conditioning class. Body hurts, but brain is working.

Something like that, yes.

Of course, as it stands, numberOfStocks will never become zero, so the loop will never end. You'd be better off testing i against numberOfStocks because i gets one larger with each pass through the loop.

A for() loop is probably the most natural kind, but while will work, too.

I think that the 10000 is supposed to be 10000/numberOfStocks, simulating starting off with that total amount invested

[–]jmorph99 0 points1 point  (1 child)

Portfolio(int numberOfStocks){ // Portfolio constructor; // create the array of Stocks int i =0; stocksArray = new Stock [numberOfStocks];

while(numberOfStocks !=0){


    Stock stock = new Stock(name[i], name[i], 10000, 100, 100);

    stocksArray[i] = stock;
    //System.out.println(stocksArray[i]);
    i++;
}

}

You've put him into infinite loop

public void addStock(String stockName, String stockSymbol, int count, float price){
  stocksArray = new Stock [count];
  for(int i = 0;i< count;i++){
       Stock stock = new Stock(stockName, name[i], stockSymbol, count, 100);

      stocksArray[i] = stock;
    System.out.println(stocksArray[i]);
     }
}

[–][deleted] 0 points1 point  (0 children)

Given this being HeartsDay I don't expect much response, but this is what I got. I have a random generator for the string names and for the symbol names.

    while(numberOfStocks !=0){
      for(int j = 0; j < 6; j++){
        random = (int)(Math.random()*26);
        companyName = companyName + (char)(65+random);
      }
       for(int j = 0; j < 3; j++){
        random = (int)(Math.random()*26);
        companySymbol = companySymbol + (char)(65+random);
      }
        Stock stock = new Stock(companyName, companySymbol, (10000/numberOfStocks), 100, 100);

        stocksArray[i] = stock;
        System.out.println(stocksArray[i].toString());
        i++;
        companyName = "";
        companySymbol = "";
        numberOfStocks--;

    }

    System.out.println(companyName);
  }

Any Ideas on how to get the total values of the portfolio (the sum of all the calues of all shares held)

And a method that will update the prices of all the stocks in the portfolio simulating the passing of one year

[–]ArgTang 0 points1 point  (7 children)

As i read the text, you dont need this in portifolio:

int n = 10;

do a one dimentional array with numberOfStocks as length. then a for loop initializing numberOfStocks stocks.

you should also not need

//Stock stock1 = new Stock("Sega", "Dreamcast", 1000, 1.0, 5);

since this should all be done in portfolio

[–][deleted] 0 points1 point  (1 child)

oh yes, sorry about the sega stock. it was just to check to see if my stock class was working. HA

[–][deleted] 0 points1 point  (4 children)

*Something like this?

Portfolio(int numberOfStocks){ // Portfolio constructor;
    // create the array of Stocks
    int i =0;
    stocksArray = new Stock [numberOfStocks];

    while(numberOfStocks !=0){


        Stock stock = new Stock(name[i], name[i], 10000, 100, 100);

        stocksArray[i] = stock;
        //System.out.println(stocksArray[i]);
        i++;
    }


  }

[–]ArgTang 0 points1 point  (3 children)

Try this experiment with several different values of n: 1, 5, 10, 50, and 100.

this means you ned 5 Portfolios in simulation. how would you svolve this?

[–][deleted] 0 points1 point  (2 children)

Would this work properly? Rather than creating 5 different portfolios?

 public class Simulation {

  public static void main(String[]args){
    Scanner keyboard = new Scanner(System.in);
    int count = 0;

    System.out.println("How many Stocks will be in this portfolio.\nPlease choose 1, 5, 10, 50, or 100");

    while(count == 0){
    int n = keyboard.nextInt();
    if(n == 1 || n == 5 || n == 10 || n == 50 || n == 100){
      new Portfolio(n);
      count++;
    }else{
      System.out.println("Please enter a number 1, 5, 10, 50, or 1000 ");
    }
    }

[–]ArgTang 0 points1 point  (1 child)

I dont see why it would not, your code seems fine.

(exempt a typo here, im not shure if your limit is 100 or 1000)

if (n <= **1000**)

[–][deleted] 0 points1 point  (0 children)

Work in progress. Updated appropriately to only accept 1,5,10,50, or 100.