Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top