Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:50:57+00:00 2026-06-04T23:50:57+00:00

I am fairly new to object oriented programming, so I am still having some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:50:58+00:00Added an answer on June 4, 2026 at 11:50 pm

    Once you read in the name, rating, price, and share count, you need to call the constructor on your class Stock to create an instance of the class and assign it to the next item in your stocks[] array.

    Like so:

    stocks[0] = new Stock( stockName, stockRating, stockPrice, numberShares);
    

    Then you’ll need to put the lines of code that you’re using to read from the console, plus my line that creates the new Stock object into a loop so that you can read in all 12 stocks:

    for( int i = 0; i < INVENTORY_SIZE; i++ )
    {
        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, stockRating, stockPrice, numberShares);
    }
    

    Now, this isn’t perfect, since it will require users to always enter a full set of 12 stocks, so you’ll need to figure out how to let the user abort out of the loop if they’re done, and you’ll still have to add that error checking you want, to ensure that no duplicates are entered, but it should initialize your individual objects and assign eachh one to the array elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to object oriented programming so some of the abstraction ideas are
Fairly new to .Net here and having trouble with a line of code. I
I'm fairly new to object-oriented programming, and I'm really interested in learning it correctly.
I am fairly new to C++ and especially object-oriented design of classes. I want
I am fairly new to to Scala and am still trying to develop a
I am fairly new to web programming, I have mainly used java to create
I am fairly new to using classes and the Object Oriented side of C++
I'm fairly new to programming but I've been reading some interesting discussions on StackOverflow
fairly new to C# here... ...What I'm trying to do is parse a bunch
I'm fairly new to Node.js and I am having some issues. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.