1

I have asked this question before and followed the feedback as best as I could but I am still having one problem with storing the info that the user enters into the array.

Here is the first attempt: OOP Java: Creating a stock inventory program

So I need to have in total three classes(That's required). The Stock, stock inventory and then the user interface. The purpose of this program is to ask the user to input the company's name, stock rating, price and the number of shares. Of course, I then have to do other things. I think I am okay with the rest, the problem is the stockInterface, the last bit of code that I post below.

public class Stock {

private String companyName;
private String stockRating;
private int price;
private int numberOfShares;

public String getCompanyName() {
    return companyName;
}

public int getStockRating() {
    return stockRating;
}

public String getPrice() {
    return price;
}

public int getNumberOfShares() {
    return numberOfShares;
}

public Stock(String companyName, String stockRating, int price, int numberOfShares) {
    super();
    this.companyName = companyName;
    this.stockRating = stockRating;
    this.price = price;
    this.numberOfShares = numberOfShares;
}

import java.util.*;

public class StockInvetory {

private static final int INVENTORY_SIZE = 12;
private Stock [] stocks;

public StockInvetory() {
    stocks = new Stock [INVENTORY_SIZE];

}


public class StockInterface() {
    private static StockInventory stockPortfolio;

        public static void main (String [] args){

    System.out.println ("Stock's name:");
    String stockName = console.next();

    System.out.println ("Stock's rating");
    String stockRating= console.next();

    System.out.println ("Stock's price:");
    int stockPrice = console.nextInt();

    System.out.println ("Numbers of shares: ");
    int numberShares= console.nextInt();

          stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares);
    }

This piece of code doesn't work.

stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares)

Can somebody please show me the proper way to store the info into the array? Thank you very much.

Community
  • 1
  • 1
ScoutBlade
  • 125
  • 1
  • 6
  • 13
  • 2
    You'll want the functionality inside a method, probably in `main`. And you'll want to actually instantiate a `StockInventory`. – Dave Newton Aug 28 '12 at 22:47
  • Please rearrange your code, right now, it makes no sense inside StockInterface class. You probably did something wrong putting the code here... – IPValverde Aug 28 '12 at 22:51
  • It probably doesn't make sense to restrict the inventory size to 12....that company is going to go out of business unless they are able to buy, sell and introduce new products. Use java.util.ArrayList which can grow and shrink dynamically – Nicholas Albion Aug 28 '12 at 23:01
  • Thank you guys for your comments. This is just some code that I made up in place of the real code that I am not allowed to post on the internet. So there are bound to be something that doesn't really make sense. My problem is how to put whatever info the user enter into the array, so that I can do other stuff with it. – ScoutBlade Aug 28 '12 at 23:17

2 Answers2

2

Lots of compile errors...

You have defined stockRating as a String but yet return it as an int:

public int getStockRating() {
    return stockRating;
}

The same is true for price.

You have extra parenthesis here:

public class StockInterface() {
                           ^

Also in StockInventory, there are multiple statements in the class block They belong in a method.

console is not instantiated. stockPortfolio is assigned as an array entry, yet it is a single object, and assigned to the Stock which is not a matching type.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thank you for finding those errors. This is not my real code, I had to make up some code really quick because I am not allowed to posted my real code. That explains so many errors. – ScoutBlade Aug 28 '12 at 23:09
1

So you've declared the stockPortfolio as an instance of StockInventory. StockInventory is a class not an array, so you can't use stockPortfolio [0] = ... because stockPortfolio is an instance of the class. You have a private member in StockInventory that is an array of Stock class instances. What you need is an accessor method to be able to manipulate it. So change StockInventory as follows:

public class StockInvetory {
/*
  All the code you have now ...
*/
  public Stock [] getStocks(){
      return stocks;
  }

  public setStocks(Stock [] value){
     //maybe some checking here ...
      stocks = value;
  }
}

Now just a slight change in using the class. You need to use the accessor methods as follows:

public class StockInterface {
 /*
 What you have just the following line changes ...
 */
   stockPortfolio.getStocks()[0] = new Stock(stockName, stockRatings, stockPrice, numberShares);
}

I am assuming you are happy with the way you are initializing the array and that you have decided arrays are better than more dynamic data structures in collections for your specific project. If this is not true have a look at Java Collections they may bring you more joy.

GrantVS
  • 2,073
  • 1
  • 24
  • 27