Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>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.</p> </blockquote> <p>Your array is like a <code>list</code> 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 <code>Stock</code> object.</p> <pre><code>public class Stock { private String companyName; private String stockRating; private int price; private int numberOfShares; </code></pre> <p>Those are all stored in each Stock object, and can be accessed by the <em>getter</em> methods you defined.</p> <p><code>int stockdata = stocks[4].getPrice();</code></p> <p>However, to initialize your <code>Stock</code> array, you want to create a new <code>Stock</code> object for each area, like so:</p> <pre><code>for(int i = 0; i &lt; INVENTORY_SIZE; i++) { stocks[i] = new Stock(foo, bar, lorem, ipsum); } </code></pre> <p>The variables used here are just placeholders, so you can create your parameters by reading from the console like you're doing above.</p> <p><a href="http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/" rel="nofollow">These design principles for OOP should help you understand the relationship between <em>data</em> and <em>containers</em>.</a></p> <p>For the second part of your question, you can look at just comparing one of the data values, like <code>companyName</code>, or <a href="http://docs.oracle.com/javase/tutorial/java/concepts/interface.html" rel="nofollow">implementing an interface</a> like <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html" rel="nofollow">Comparable</a>.</p> <pre><code>for(Stock s : stocks) { if(stockName.equals(s.getCompanyName()) { // ERROR! } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload