OOP Java: Creating a stock inventory program

ScoutBlade picture ScoutBlade · Jun 2, 2012 · Viewed 38.9k times · Source

I am fairly new to object oriented programming, so I am still having some trouble grasping some of the basic concepts. So here I am trying to create a basic inventory program to keep track of stocks. Each stock contains couple details: company name, stock rating (AAA, AAa, Aaa, stuff like that), purchase price and numbers of shares. The program will ask user to input these details through command line prompt. And users can only input at most 12 stocks. If the user enters a stock twice, it will print out an error. And if the user has inputted one stock twice, it will also print out an error message.

Here is what I have done so far: Stock object

 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;
}

Now, I am trying to create stock inventory program

import java.util.*;

public class StockInvetory {

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

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

}

private static void StockInventory() {
       for (int i = 0; i<INVENTORY_SIZE; i++){
         Scanner console = new Scanner(System.in);

    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();

          stocks [i]= new Stock(stockName, stockRatings, stockPrice, numberShares);
    }

public static void main (String [] args){
    StockInventory();



}

}

So my questions are the following:

The first stock object program should be okay, my problem is the stock inventory program. With this code, private Stock [] stocks, does it mean that the stock inventory program will store the info into an array? If it is, how do I store all the details associated with a particular stock, company name, price, etcs together. Do I have to create a multi-dimensional array in order of keep track of all the details? Like one row contains all the details about one stock, and second row contains details about another stock. And to determine if the user has entered one stock twice, do I use "equalsIgnoreCase" to do the comparison? I know I probably need to create another method for the stock Inventory.

Thank you very much in advance for your assistance.

Answer

David B picture David B · Jun 2, 2012

private Stock [] stocks, does it mean that the stock inventory program will store the info into an array? If it is, how do I store all the details associated with a particular stock, company name, price, etcs together.

Your array is like a list of stocks. But just because its one object, doesn't mean it only contains one piece of data. It can hold Strings, ints, and other user-defined data types. Therefore, you can store pieces of data relating to the stock inside the Stock object.

public class Stock {

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

Those are all stored in each Stock object, and can be accessed by the getter methods you defined.

int stockdata = stocks[4].getPrice();

However, to initialize your Stock array, you want to create a new Stock object for each area, like so:

for(int i = 0; i < INVENTORY_SIZE; i++) {
    stocks[i] = new Stock(foo, bar, lorem, ipsum);
}

The variables used here are just placeholders, so you can create your parameters by reading from the console like you're doing above.

These design principles for OOP should help you understand the relationship between data and containers.

For the second part of your question, you can look at just comparing one of the data values, like companyName, or implementing an interface like Comparable.

for(Stock s : stocks) {
    if(stockName.equals(s.getCompanyName()) {
       // ERROR!
    }
}